Skip to content

Commit c6d233b

Browse files
authored
Merge pull request #1 from datacamp/check_numpy_array
Check numpy array
2 parents abed0ca + f20779e commit c6d233b

5 files changed

Lines changed: 87 additions & 3 deletions

File tree

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ deploy:
99
provider: pypi
1010
user: machow
1111
password:
12-
secure: rln5w0ZBtGmleGD1rqkVnRZoP3dMtmya3Nz8yFFu/zudBzrUPu6dG3CjneVRPiSqh0Y6MqnnHOKMf4F69HG34o2jybhdCVrZfhSLUPcHDobUq9z2WC/dfAx0B0oALDP3qtbs80MeLwvoyC+OkaFESDee7CX/R4nGUdp7CDeoi3faNjzytAT0PprBHrVqxZpUPC7TMBL1wvyAhEcyWM/b8s1WDTHwfIu6KkumcAOqQbNCUsSScOOCuFKN/NtKVFEn+7ZWZ9kk6uaCGw6K+8Po55Zf+W0CZxHBQMvcYfEegMV7HXBr4V+KKnZSedwoeu4PNFmQyV07Ew7VH4VoS2pt88AeQaQv9XGuM+AReq6tIK9OZ341nYhvBaNr3jDteiPM4WRoHlLNqsRP6qLXXWvVbFxcDBUZpbZJSK3Xje+P1LxULz2UaU5XrIKLr/kA2m/WkTgBwDIPexXbHMx18H7fy8V+DrQGFlaKCDTPRm2kn7M5YgeIS+JAxhyTkJg6bRTBkbegmY8UATPD2XKcz/BKwgLQTO9MStf7jOvq5o1YKfaDSEUKwXTgdMk/EW5SGEGjTysDBXlKP6/nf1wiU9dXrBfj7yBHdfyQFkXFsZBEU+vj+HCmGdFkAJ4T8uueqtBmZ9g6F63izbkF/ojPSU84ObI6yeu29BIEIRNsnng8tms=
12+
secure: fsSQjoVZw3vvbpfX06QlntVEnCbrkgkGtrvG4nvHCyhY/irXR99VRlaljfrcetGjZjiLPXY6w6mHS27WOaqxEK3XiWIsRscB05+RGWDUi+XDlql6jyYv/LbEb9ElCpghojV1f8AUvUAz31gEr/8bE5RIIqjeJXRma7clFpGIDFjS8YkvypUT5oG/4TQVikPvWiU7imVIkAigjV58bEeGFOBun8vBhYrYi7jd3Nx65V9HyQKPY7Tf+/t714Kcog9nn+cUyHnklEU4k65EIUc/jR2BuLgTKPtLDrOD58Q6ikGANe5ONBKIVWzhBOgH6lU6j8K6QADBiiXTsq0g8YC3/HqPu+3C5zGnrpP2Oq0BESBzx3Vp81iOKg7Y7Th3fpJo/G0rwGz8CTqmqINZpAhXC7LUN2udbUa9g65PxgatNLYdjG8PMOX+hCDGVgB/qu0bxas/HCdC29dYh1DU8tse7kiGFxL1c79jnSi3aUp2T0izTg16MVw5fGUaojY6MCGdK7lyPsIIwt01MIn6vh4wxp4xnX2vd2S2gMLx+yHemyqONFk42d0eoebEOagcPOZuVapejulAn5gkY7tle/cLL4L8y/MO3q69iJYoRUQYaFK8HCwPMHTNSCccVJwpeSBW7zoJzmz19EBeXzZTz8T4wm+6LdOJAVfJJswCZPIURro=
1313
on:
1414
tags: true
1515
distributions: sdist bdist_wheel
16-
repo: datacamp/sqlwhat-ext
16+
repo: datacamp/pythonwhat-ext
1717
skip_upload_docs: true

pythonwhat_ext.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
11
__version__ = '0.0.0'
2+
3+
from pythonwhat.check_syntax import state_dec, Ex
4+
import numpy as np
5+
6+
@state_dec
7+
def check_numpy_array(name, state = None):
8+
# is defined
9+
obj = Ex(state).check_object(name)
10+
11+
# is a numpy array
12+
obj.is_instance(np.ndarray)
13+
14+
# same shape
15+
obj.has_equal_value(expr_code = '{:s}.shape'.format(name))
16+
17+
# equal contents
18+
obj.has_equal_value()
19+
20+
# return object state for chaining
21+
return obj

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
name = 'pythonwhat-ext',
1515
version = version,
1616
py_modules= ['pythonwhat_ext'],
17-
install_requires = [],
17+
install_requires = ['pythonwhat>=2.7'],
1818
description = 'pythonwhat extensions - high level SCTs',
1919
author = 'Michael Chow',
2020
author_email = 'michael@datacamp.com',

tests/helpers.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from pythonwhat.State import State
2+
from pythonwhat.Reporter import Reporter
3+
4+
from IPython.core.interactiveshell import InteractiveShell
5+
6+
class MockProcess:
7+
"""Mock the processes used in pythonbackend to evaluate code."""
8+
def __init__(self, **kwargs):
9+
self.shell = InteractiveShell(user_ns = kwargs)
10+
11+
def executeTask(self, task):
12+
return task(self.shell)
13+
14+
def create_state(solution_code="", student_code="", pre_exercise_code="",
15+
student_process = None, solution_process = None,
16+
raw_student_output="",
17+
reporter = None):
18+
"""Create a state for testing SCTs.
19+
20+
Custom processes are required for SCTs that execute code (e.g. has_equal_value).
21+
See MockProcess above.
22+
"""
23+
if reporter is None: reporter = Reporter()
24+
Reporter.active_reporter = reporter
25+
state = State(full_student_code = student_code,
26+
full_solution_code = solution_code,
27+
**locals())
28+
29+
# only used to get converters :/
30+
State.root_state = state
31+
32+
return state
33+
34+
def create_x_state(stu_val, sol_val, **kwargs):
35+
s = create_state(student_process = MockProcess(x = stu_val),
36+
solution_process = MockProcess(x = sol_val),
37+
**kwargs)
38+
39+
return s

tests/test_check_numpy_array.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from pythonwhat_ext import check_numpy_array
2+
from pythonwhat.check_syntax import Ex
3+
from pythonwhat.Test import TestFail as TF
4+
5+
import numpy as np
6+
7+
import pytest
8+
from helpers import create_x_state
9+
10+
def test_check_numpy_array():
11+
state = create_x_state(np.array([1,2,3]), np.array([1,2,3]))
12+
check_numpy_array("x", state = state)
13+
14+
def test_check_numpy_array_fail():
15+
state = create_x_state(np.array([1,2,3]), np.array([1,2]))
16+
with pytest.raises(TF):
17+
check_numpy_array("x", state = state)
18+
19+
def test_ex_check_numpy_array():
20+
state = create_x_state(np.array([1,2,3]), np.array([1,2, 3]))
21+
Ex(state).multi(check_numpy_array('x'))
22+
23+
def test_ex_rshift_check_numpy_array_fail():
24+
state = create_x_state(np.array([1,2,3]), np.array([1,2]))
25+
with pytest.raises(TF): Ex(state) >> check_numpy_array('x')

0 commit comments

Comments
 (0)