From c6c524bc9ea0a11997960797f3cbd59bdc8f5d66 Mon Sep 17 00:00:00 2001 From: Rik Bauwens Date: Mon, 30 Dec 2024 13:02:10 +0100 Subject: [PATCH 1/2] feat: delay pandas / numpy imports --- pythonwhat/Test.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/pythonwhat/Test.py b/pythonwhat/Test.py index 353e9da..9126ac9 100644 --- a/pythonwhat/Test.py +++ b/pythonwhat/Test.py @@ -104,18 +104,30 @@ def test(self): def areinstance(x, y, tuple_of_classes): return isinstance(x, tuple_of_classes) and isinstance(y, tuple_of_classes) +def is_primitive(x): + return isinstance(x, (str, int, float, bool, type(None))) + +def is_collection_of_primitives(x): + return isinstance(x, (list, tuple, set)) and all(is_primitive(element) for element in x) # For equality of ndarrays, list, dicts, pd Series and pd DataFrames: # First try to the faster equality functions. If these don't pass, # Run the assertions that are typically slower. def is_equal(x, y): - import pandas as pd - from pandas.testing import assert_frame_equal, assert_series_equal - import numpy as np try: + if areinstance(x, y, (str, int, float, bool, type(None))): + return x == y + if is_collection_of_primitives(x): + return x == y if areinstance(x, y, (Exception,)): # Types of errors don't matter (this is debatable) return str(x) == str(y) + + # Delay importing pandas / numpy until absolutely necessary. This is important for performance in Pyodide. + import pandas as pd + from pandas.testing import assert_frame_equal, assert_series_equal + import numpy as np + if areinstance(x, y, (np.ndarray, dict, list, tuple)): np.testing.assert_equal(x, y) return True From ffe19df961cf4e261cc42905c0fb877f36bd17b2 Mon Sep 17 00:00:00 2001 From: Rik Bauwens Date: Mon, 30 Dec 2024 13:06:03 +0100 Subject: [PATCH 2/2] chore: bump version --- pythonwhat/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pythonwhat/__init__.py b/pythonwhat/__init__.py index 81c925a..2cb3bd0 100644 --- a/pythonwhat/__init__.py +++ b/pythonwhat/__init__.py @@ -1,3 +1,3 @@ -__version__ = "2.27.0" +__version__ = "2.28.0" from .test_exercise import test_exercise, allow_errors