Skip to content

Commit 42be017

Browse files
authored
Merge pull request #381 from datacamp/jh/run-python-files
Run code from Python files
2 parents 1140abb + 9f6935e commit 42be017

29 files changed

Lines changed: 517 additions & 99 deletions

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ _, ctxt = prep_context()
3434
globals().update(ctxt)
3535

3636
# initialize state with student and solution submission
37-
from pythonwhat.local import setup_state
37+
from pythonwhat.test_exercise import setup_state
3838
setup_state(stu_code = "x = 5", sol_code = "x = 4")
3939

4040
Ex().check_object('x')
@@ -64,4 +64,4 @@ Bugs? Questions? Suggestions? [Create an issue](https://github.com/datacamp/pyth
6464

6565

6666
## License
67-
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fdatacamp%2Fpythonwhat.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fdatacamp%2Fpythonwhat?ref=badge_large)
67+
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fdatacamp%2Fpythonwhat.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fdatacamp%2Fpythonwhat?ref=badge_large)

docs/reference.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,21 @@ Loops
7575
.. autofunction:: pythonwhat.checks.check_wrappers.check_dict_comp
7676
.. autofunction:: pythonwhat.checks.check_wrappers.check_generator_exp
7777

78-
State Management
78+
State management
7979
----------------
8080

8181
.. autofunction:: pythonwhat.checks.check_logic.override
8282
.. autofunction:: pythonwhat.checks.check_logic.disable_highlighting
8383
.. autofunction:: pythonwhat.checks.check_logic.set_context
8484
.. autofunction:: pythonwhat.checks.check_logic.set_env
8585

86+
Checking files
87+
--------------
88+
89+
.. autofunction:: pythonwhat.checks.check_wrappers.check_file
90+
.. autofunction:: pythonwhat.checks.check_wrappers.has_dir
91+
.. autofunction:: pythonwhat.local.run
92+
8693
Electives
8794
---------
8895

pythonwhat/State.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,24 @@ def has_different_processes(self):
169169
# play it safe (most common)
170170
return True
171171

172+
# todo: remove (deprecated)
172173
def assert_root(self, fun, extra_msg=""):
173174
if self.parent_state is not None:
174175
raise InstructorError(
175176
"`%s()` should only be called from the root state, `Ex()`. %s"
176177
% (fun, extra_msg)
177178
)
178179

180+
def assert_execution_root(self, fun, extra_msg=""):
181+
if self.parent_state is not None and not self.assert_creator_type("run"):
182+
raise InstructorError(
183+
"`%s()` should only be called focusing on a full script, following `Ex()` or `run()`. %s"
184+
% (fun, extra_msg)
185+
)
186+
187+
def assert_creator_type(self, type):
188+
return self.creator and self.creator.get("type") == type
189+
179190
def assert_is(self, klasses, fun, prev_fun):
180191
if self.__class__.__name__ not in klasses:
181192
raise InstructorError(

pythonwhat/checks/check_object.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def __init__(self, n):
158158
# Only do the assertion if PYTHONWHAT_V2_ONLY is set to '1'
159159
if v2_only():
160160
extra_msg = "If you want to check the value of an object in e.g. a for loop, use `has_equal_value(name = 'my_obj')` instead."
161-
state.assert_root("check_object", extra_msg=extra_msg)
161+
state.assert_execution_root("check_object", extra_msg=extra_msg)
162162

163163
if missing_msg is None:
164164
missing_msg = "Did you define the {{typestr}} `{{index}}` without errors?"

pythonwhat/checks/check_wrappers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pythonwhat.checks import check_object, check_logic, check_funcs, has_funcs
66
from pythonwhat.checks.check_function import check_function
77
from pythonwhat.checks.check_has_context import has_context
8+
from pythonwhat.local import run
89

910
from inspect import signature, Parameter
1011
from functools import partial, wraps
@@ -770,6 +771,7 @@ def rename_function(func, name):
770771

771772
scts["has_context"] = has_context
772773
scts["check_function"] = check_function
774+
scts["run"] = run
773775
scts["check_file"] = check_file
774776
scts["has_dir"] = has_dir
775777
scts["_debug"] = _debug

pythonwhat/checks/has_funcs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ def has_printout(
652652
"""
653653

654654
extra_msg = "If you want to check printouts done in e.g. a for loop, you have to use a `check_function('print')` chain instead."
655-
state.assert_root("has_printout", extra_msg=extra_msg)
655+
state.assert_execution_root("has_printout", extra_msg=extra_msg)
656656

657657
if not_printed_msg is None:
658658
not_printed_msg = (
@@ -747,7 +747,7 @@ def has_no_error(
747747
leave of the ``has_no_error()`` step.
748748
749749
"""
750-
state.assert_root("has_no_error")
750+
state.assert_execution_root("has_no_error")
751751

752752
if state.reporter.errors:
753753
_msg = state.build_message(

0 commit comments

Comments
 (0)