From 3e65138f8b82c565d408cc136e4a3e986348e220 Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Tue, 5 Jan 2021 13:38:10 +0100 Subject: [PATCH] new class ExerciseFunctionPandas --- demo-notebooks/exercise07-miscell.py | 6 ++++++ nbautoeval/__init__.py | 6 ++++-- nbautoeval/exercise_function.py | 30 +++++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/demo-notebooks/exercise07-miscell.py b/demo-notebooks/exercise07-miscell.py index 74414a44..899762bf 100644 --- a/demo-notebooks/exercise07-miscell.py +++ b/demo-notebooks/exercise07-miscell.py @@ -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] # *** diff --git a/nbautoeval/__init__.py b/nbautoeval/__init__.py index dbb15f94..e23505a9 100644 --- a/nbautoeval/__init__.py +++ b/nbautoeval/__init__.py @@ -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) diff --git a/nbautoeval/exercise_function.py b/nbautoeval/exercise_function.py index 07682bfb..ff693f73 100644 --- a/nbautoeval/exercise_function.py +++ b/nbautoeval/exercise_function.py @@ -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 @@ -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)