Skip to content

Commit 6f5c99f

Browse files
committed
Improve state partial with offset
1 parent c5b9fc4 commit 6f5c99f

3 files changed

Lines changed: 21 additions & 15 deletions

File tree

pythonwhat/State.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
parser_dict,
77
)
88
from protowhat.State import State as ProtoState
9+
from protowhat.selectors import DispatcherInterface
910
from protowhat.Feedback import InstructorError
1011
from pythonwhat.Feedback import Feedback
1112
from pythonwhat import signatures
@@ -253,7 +254,7 @@ def get_dispatcher(self):
253254
return Dispatcher(self.pre_exercise_ast)
254255

255256

256-
class Dispatcher:
257+
class Dispatcher(DispatcherInterface):
257258
def __init__(self, pre_exercise_ast):
258259
self._parser_cache = dict()
259260
self.pre_exercise_mappings = self._getx(

pythonwhat/checks/check_wrappers.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -667,46 +667,51 @@ def __init__(self, i):
667667
# return full_partial
668668

669669

670-
def partial_with_offset(func, *partial_args, offset=1, **partial_kwargs):
671-
kwargs_partial = partial(func, **partial_kwargs)
670+
def partial_with_offset(offset=1):
671+
def bound_partial_with_offset(func, *partial_args, **partial_kwargs):
672+
kwargs_partial = partial(func, **partial_kwargs)
672673

673-
@wraps(func)
674-
def full_partial(*args, **kwargs):
675-
full_args = args[:offset] + partial_args + args[offset:]
676-
return kwargs_partial(*full_args, **kwargs)
674+
@wraps(func)
675+
def full_partial(*args, **kwargs):
676+
full_args = args[:offset] + partial_args + args[offset:]
677+
return kwargs_partial(*full_args, **kwargs)
677678

678-
return full_partial
679+
return full_partial
680+
return bound_partial_with_offset
681+
682+
683+
state_partial = partial_with_offset()
679684

680685

681686
def rename_function(func, name):
682687
# see functools.wraps
683688
func.__name__ = func.__qualname__ = name
684689

685690

686-
scts["has_equal_name"] = partial_with_offset(
691+
scts["has_equal_name"] = state_partial(
687692
has_equal_part,
688693
"name",
689694
msg="Make sure to use the correct {{name}}, was expecting {{sol_part[name]}}, instead got {{stu_part[name]}}.",
690695
)
691-
scts["is_default"] = partial_with_offset(
696+
scts["is_default"] = state_partial(
692697
has_equal_part,
693698
"is_default",
694699
msg="Make sure it {{ 'has' if sol_part.is_default else 'does not have'}} a default argument.",
695700
)
696701

697702
# include rest of wrappers
698703
for k, v in __PART_WRAPPERS__.items():
699-
check_fun = partial_with_offset(check_part, k, v)
704+
check_fun = state_partial(check_part, k, v)
700705
rename_function(check_fun, "check_" + k)
701706
scts[check_fun.__name__] = check_fun
702707

703708
for k, v in __PART_INDEX_WRAPPERS__.items():
704-
check_fun = partial_with_offset(check_part_index, k, part_msg=v)
709+
check_fun = state_partial(check_part_index, k, part_msg=v)
705710
rename_function(check_fun, "check_" + k)
706711
scts[check_fun.__name__] = check_fun
707712

708713
for k, v in __NODE_WRAPPERS__.items():
709-
check_fun = partial_with_offset(check_node, k + "s", typestr=v["typestr"])
714+
check_fun = state_partial(check_node, k + "s", typestr=v["typestr"])
710715
check_fun.__doc__ = Template(v["docstr"]).render(
711716
typestr="typestr: If specified, this overrides the standard way of referring to the construct you're zooming in on.",
712717
missing_msg="missing_msg: If specified, this overrides the automatically generated feedback message in case the construct could not be found.",

pythonwhat/probe.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from collections import OrderedDict
66
from pythonwhat import test_funcs
77
from pythonwhat.State import State
8-
from pythonwhat.checks.check_wrappers import partial_with_offset
8+
from pythonwhat.checks.check_wrappers import state_partial
99

1010
TEST_NAMES = [
1111
"test_mc",
@@ -121,7 +121,7 @@ def __iter__(self):
121121
def partial(self):
122122
"""Return partial of original function call"""
123123
ba = self.data["bound_args"]
124-
return partial_with_offset(self.data["func"], *ba.args[1:], **ba.kwargs)
124+
return state_partial(self.data["func"], *ba.args[1:], **ba.kwargs)
125125

126126
def update_child_calls(self):
127127
"""Replace child nodes on original function call with their partials"""

0 commit comments

Comments
 (0)