Skip to content

Commit

Permalink
new class ExerciseFunctionPandas
Browse files Browse the repository at this point in the history
  • Loading branch information
parmentelat committed Jan 5, 2021
1 parent 8f8a278 commit 3e65138
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
6 changes: 6 additions & 0 deletions demo-notebooks/exercise07-miscell.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,11 @@ def longargs(x):
# %%
exo_longargs.correction(longargs)

# %% [markdown] cell_style="center"
# # pandas

# %% [markdown]
# As of jan 2021, the new class `ExerciseFunction` allows to deal with funtions that return a pandas `DataFrame` or `Series` - a demo sample would be great

# %% [markdown]
# ***
6 changes: 4 additions & 2 deletions nbautoeval/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
from .renderer import Renderer, PPrintRenderer, MultilineRenderer, ImshowRenderer
from .callrenderer import CallRenderer, PPrintCallRenderer, IsliceRenderer

from .exercise_function import ExerciseFunction, ExerciseFunctionNumpy
from .exercise_function import (
ExerciseFunction, ExerciseFunctionNumpy, ExerciseFunctionPandas)
from .exercise_regexp import ExerciseRegexp, ExerciseRegexpGroups
from .exercise_generator import ExerciseGenerator
from .exercise_class import ExerciseClass, ClassScenario, ClassExpression, ClassStatement
from .exercise_class import (
ExerciseClass, ClassScenario, ClassExpression, ClassStatement)

from .content import (TextContent, CodeContent, MathContent,
MarkdownContent, MarkdownMathContent)
Expand Down
30 changes: 27 additions & 3 deletions nbautoeval/exercise_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,10 @@ def validate(self, expected, result): # pylint: disable=r0201

return expected == result



# ExerciseFunctionNumpy - no hard requirement on pandas

# see this question on SO
# https://stackoverflow.com/questions/40659212/futurewarning-elementwise-comparison-failed-returning-scalar-but-in-the-futur

Expand Down Expand Up @@ -375,6 +379,26 @@ def validate(self, expected, result):
print("OOPS2", type(exc), exc)
return False

except Exception:
#print("ExerciseFunctionNumpy not defined ; numpy not installed ? ")
pass
except ModuleNotFoundError:
class ExerciseFunctionNumpy(ExerciseFunction):
def __init__(self, *args, **kwds):
print("WARNING: dummy ExerciseFunctionNumpy - numpy not installed ?")
super().__init__(*args, **kwds)


# ExerciseFunctionPandas - no hard requirement on pandas
try:
import pandas as pd

class ExerciseFunctionPandas(ExerciseFunction):
"""
This is suitable for functions that are expected to return
either a pandas DataFrame or Series object
"""
def validate(self, expected, result):
return expected.equals(result)
except ModuleNotFoundError:
class ExerciseFunctionPandas(ExerciseFunction):
def __init__(self, *args, **kwds):
print("WARNING: dummy ExerciseFunctionNumpy - numpy not installed ?")
super().__init__(*args, **kwds)

0 comments on commit 3e65138

Please sign in to comment.