Skip to content

Commit

Permalink
Merge pull request #419 from datacamp/delay-import
Browse files Browse the repository at this point in the history
[CP-4190] feat: delay pandas / numpy imports
  • Loading branch information
rikbw authored Dec 30, 2024
2 parents 04720f5 + ffe19df commit f0bfbe3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
18 changes: 15 additions & 3 deletions pythonwhat/Test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pythonwhat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = "2.27.0"
__version__ = "2.28.0"

from .test_exercise import test_exercise, allow_errors

0 comments on commit f0bfbe3

Please sign in to comment.