Skip to content

Commit f0bfbe3

Browse files
authoredDec 30, 2024
Merge pull request #419 from datacamp/delay-import
[CP-4190] feat: delay pandas / numpy imports
2 parents 04720f5 + ffe19df commit f0bfbe3

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed
 

‎pythonwhat/Test.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,30 @@ def test(self):
104104
def areinstance(x, y, tuple_of_classes):
105105
return isinstance(x, tuple_of_classes) and isinstance(y, tuple_of_classes)
106106

107+
def is_primitive(x):
108+
return isinstance(x, (str, int, float, bool, type(None)))
109+
110+
def is_collection_of_primitives(x):
111+
return isinstance(x, (list, tuple, set)) and all(is_primitive(element) for element in x)
107112

108113
# For equality of ndarrays, list, dicts, pd Series and pd DataFrames:
109114
# First try to the faster equality functions. If these don't pass,
110115
# Run the assertions that are typically slower.
111116
def is_equal(x, y):
112-
import pandas as pd
113-
from pandas.testing import assert_frame_equal, assert_series_equal
114-
import numpy as np
115117
try:
118+
if areinstance(x, y, (str, int, float, bool, type(None))):
119+
return x == y
120+
if is_collection_of_primitives(x):
121+
return x == y
116122
if areinstance(x, y, (Exception,)):
117123
# Types of errors don't matter (this is debatable)
118124
return str(x) == str(y)
125+
126+
# Delay importing pandas / numpy until absolutely necessary. This is important for performance in Pyodide.
127+
import pandas as pd
128+
from pandas.testing import assert_frame_equal, assert_series_equal
129+
import numpy as np
130+
119131
if areinstance(x, y, (np.ndarray, dict, list, tuple)):
120132
np.testing.assert_equal(x, y)
121133
return True

‎pythonwhat/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
__version__ = "2.27.0"
1+
__version__ = "2.28.0"
22

33
from .test_exercise import test_exercise, allow_errors

0 commit comments

Comments
 (0)