From e1e003c8b80442c48d29344f584b98bf64ef24c1 Mon Sep 17 00:00:00 2001 From: Edoardo Baldi Date: Fri, 9 May 2025 10:58:06 +0200 Subject: [PATCH 1/4] Fix datetime.UTC issue --- 12_functions_advanced.ipynb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/12_functions_advanced.ipynb b/12_functions_advanced.ipynb index b4659cf4..3950c49b 100644 --- a/12_functions_advanced.ipynb +++ b/12_functions_advanced.ipynb @@ -335,10 +335,10 @@ "metadata": {}, "outputs": [], "source": [ - "from datetime import datetime\n", + "from datetime import datetime, UTC\n", "\n", "\n", - "def log(msg, *, dt=datetime.utcnow()):\n", + "def log(msg, *, dt=datetime.now(UTC)):\n", " print(f\"{dt}: {msg}\")" ] }, @@ -362,9 +362,9 @@ "source": [ "from time import sleep\n", "\n", - "log(\"my first message\")\n", + "log(\"A first message\")\n", "sleep(5)\n", - "log(\"my first message\")" + "log(\"A second message\")" ] }, { @@ -395,7 +395,7 @@ "outputs": [], "source": [ "def log(msg, *, dt=None):\n", - " dt = dt or datetime.now(datetime.timezone.utc)\n", + " dt = dt or datetime.now(UTC)\n", " print(f\"{dt}: {msg}\")" ] }, @@ -408,9 +408,9 @@ }, "outputs": [], "source": [ - "log(\"my first message\")\n", + "log(\"A first message\")\n", "sleep(5)\n", - "log(\"my first message\")" + "log(\"A second message\")" ] }, { From 905f09a62a2a949e51ddb77a28c02b9a83c672dd Mon Sep 17 00:00:00 2001 From: Edoardo Baldi Date: Fri, 9 May 2025 11:06:47 +0200 Subject: [PATCH 2/4] Fix test to randomize with lambda --- tutorial/tests/test_12_functions_advanced.py | 41 +++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/tutorial/tests/test_12_functions_advanced.py b/tutorial/tests/test_12_functions_advanced.py index 4e7e11b5..98ea150c 100644 --- a/tutorial/tests/test_12_functions_advanced.py +++ b/tutorial/tests/test_12_functions_advanced.py @@ -29,7 +29,46 @@ def test_randomize_list( function_to_test: t.Callable, my_list: list[int], ): - assert function_to_test(my_list) == reference_randomize_list(my_list) + # We need to verify: + # 1. Content is preserved (same elements) + # 2. Randomization occurs (not sorted, not original order) + + # Skip randomization tests for trivial lists + if len(my_list) <= 1: + return + + # Run the function multiple times to ensure randomization + results = [] + for _ in range(3): # Run multiple times + # Use a copy to prevent the function from modifying the original + test_list = my_list.copy() + result = function_to_test(test_list) + + # 1. Check the same elements are present + assert set(result) == set(my_list) + # 2. Check the length is preserved + assert len(result) == len(my_list) + + results.append(result) + + # 3. Check that we get different orderings (randomization) + # For lists with enough elements, very unlikely to get the same result twice + if len(my_list) > 2: + # Check there's at least some variation in results + assert len({tuple(r) for r in results}) > 1, ( + "Function doesn't appear to randomize" + ) + + # Ensure it's not just returning sorted lists + sorted_list = sorted(my_list) + assert not all(r == sorted_list for r in results), ( + "Function appears to just sort the list" + ) + + # Ensure it's not just returning the original list + assert not all(r == my_list for r in results), ( + "Function appears to return the original list" + ) # From 23e3caca229945deb374c7e7557142ec08fedb61 Mon Sep 17 00:00:00 2001 From: Edoardo Baldi Date: Fri, 9 May 2025 11:28:23 +0200 Subject: [PATCH 3/4] Improved test of randomize with lambda --- tutorial/tests/test_12_functions_advanced.py | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/tutorial/tests/test_12_functions_advanced.py b/tutorial/tests/test_12_functions_advanced.py index 98ea150c..d622a8e8 100644 --- a/tutorial/tests/test_12_functions_advanced.py +++ b/tutorial/tests/test_12_functions_advanced.py @@ -56,18 +56,7 @@ def test_randomize_list( if len(my_list) > 2: # Check there's at least some variation in results assert len({tuple(r) for r in results}) > 1, ( - "Function doesn't appear to randomize" - ) - - # Ensure it's not just returning sorted lists - sorted_list = sorted(my_list) - assert not all(r == sorted_list for r in results), ( - "Function appears to just sort the list" - ) - - # Ensure it's not just returning the original list - assert not all(r == my_list for r in results), ( - "Function appears to return the original list" + "Function doesn't appear to randomize. You should not use random.seed()." ) From 4317b18a4779070f2e538d84d957a88e67ec034e Mon Sep 17 00:00:00 2001 From: Edoardo Baldi Date: Fri, 9 May 2025 11:32:36 +0200 Subject: [PATCH 4/4] [skip ci] Add explanation --- tutorial/tests/test_12_functions_advanced.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tutorial/tests/test_12_functions_advanced.py b/tutorial/tests/test_12_functions_advanced.py index d622a8e8..8881b98d 100644 --- a/tutorial/tests/test_12_functions_advanced.py +++ b/tutorial/tests/test_12_functions_advanced.py @@ -29,10 +29,6 @@ def test_randomize_list( function_to_test: t.Callable, my_list: list[int], ): - # We need to verify: - # 1. Content is preserved (same elements) - # 2. Randomization occurs (not sorted, not original order) - # Skip randomization tests for trivial lists if len(my_list) <= 1: return @@ -51,8 +47,10 @@ def test_randomize_list( results.append(result) - # 3. Check that we get different orderings (randomization) # For lists with enough elements, very unlikely to get the same result twice + # 1. Convert each of the results to tuples + # 2. {tuple(r) for r in results} create a set that deduplicates identical orderings + # 3. len(...) > 1 checks that we've seen at least two different orderings if len(my_list) > 2: # Check there's at least some variation in results assert len({tuple(r) for r in results}) > 1, (