Skip to content

Commit 3d92cd2

Browse files
committed
misc review cleanup
1 parent 60eb119 commit 3d92cd2

10 files changed

Lines changed: 47 additions & 31 deletions

File tree

‎paths_cli/parsing/tools.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import numpy as np
22

33
def custom_eval(obj, named_objs=None):
4+
"""Parse user input to allow simple math.
5+
6+
This allows certain user input to be treated as a simplified subset of
7+
Python. In particular, this is intended to allow simple arithmetic. It
8+
allows use of the modules numpy (as ``np``) and math, which provide
9+
potentially useful functions (e.g., ``cos``) as well as constants (e.g.,
10+
``pi``).
11+
"""
412
string = str(obj)
513
# TODO: check that the only attribute access comes from a whitelist
14+
# (parse the AST for that)
615
namespace = {
716
'np': __import__('numpy'),
817
'math': __import__('math'),

‎paths_cli/tests/parsing/test_tools.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import pytest
22
import numpy.testing as npt
3+
import numpy as np
4+
import math
35

46
from paths_cli.parsing.tools import *
57

68
@pytest.mark.parametrize('expr,expected', [
7-
('1+1', 2)
9+
('1+1', 2),
10+
('np.pi / 2', np.pi / 2),
11+
('math.cos(1.5)', math.cos(1.5)),
812
])
913
def test_custom_eval(expr, expected):
1014
npt.assert_allclose(custom_eval(expr), expected)

‎paths_cli/tests/utils.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import urllib.request
2+
3+
try:
4+
urllib.request.urlopen('https://www.google.com')
5+
except:
6+
HAS_INTERNET = False
7+
else:
8+
HAS_INTERNET = True
9+
10+
def assert_url(url):
11+
if not HAS_INTERNET:
12+
pytest.skip("Internet connection seems faulty")
13+
14+
# TODO: On a 404 this will raise a urllib.error.HTTPError. It would be
15+
# nice to give some better output to the user here.
16+
resp = urllib.request.urlopen(url)
17+
assert resp.status == 200
18+

‎paths_cli/tests/wizard/test_core.py‎

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,6 @@
1010
make_mock_wizard, make_mock_retry_wizard
1111
)
1212

13-
# def test_name():
14-
# wizard = make_mock_wizard('foo')
15-
# cv = CoordinateFunctionCV(lambda s: s.xyz[0][0])
16-
# assert not cv.is_named
17-
# result = name(wizard, cv, obj_type="CV", store_name="cvs")
18-
# assert result is cv
19-
# assert result.is_named
20-
# assert result.name == 'foo'
21-
22-
# def test_name_exists():
23-
# wizard = make_mock_retry_wizard(['foo', 'bar'])
24-
# wizard.cvs['foo'] = 'placeholder'
25-
# cv = CoordinateFunctionCV(lambda s: s.xyz[0][0])
26-
# assert not cv.is_named
27-
# result = name(wizard, cv, obj_type="CV", store_name="cvs")
28-
# assert result is cv
29-
# assert result.is_named
30-
# assert result.name == 'bar'
31-
# assert wizard.console.input.call_count == 2
32-
3313
@pytest.mark.parametrize('req,expected', [
3414
(('foo', 2, 2), '2'), (('foo', 2, float('inf')), 'at least 2'),
3515
(('foo', 0, 2), 'at most 2'),

‎paths_cli/tests/wizard/test_openmm.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
from unittest import mock
33

44
from paths_cli.tests.wizard.mock_wizard import mock_wizard
5+
from paths_cli.tests.utils import assert_url
56

67
from paths_cli.wizard.openmm import (
7-
_load_openmm_xml, _load_topology, openmm
8+
_load_openmm_xml, _load_topology, openmm, OPENMM_SERIALIZATION_URL
89
)
910

11+
def test_helper_url():
12+
assert_url(OPENMM_SERIALIZATION_URL)
1013

1114
@pytest.mark.parametrize('obj_type', ['system', 'integrator', 'foo'])
1215
def test_load_openmm_xml(ad_openmm, obj_type):

‎paths_cli/tests/wizard/test_tools.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def test_a_an(word, expected):
1212

1313
@pytest.mark.parametrize('user_inp,expected', [
1414
('y', True), ('n', False),
15-
# ('Y', True), ('N', False), ('yes', True), ('no', False)
15+
('Y', True), ('N', False), ('yes', True), ('no', False)
1616
])
1717
def test_yes_no(user_inp, expected):
1818
assert yes_no(user_inp) == expected

‎paths_cli/wizard/cvs.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ def _mdtraj_function_cv(wizard, cv_does_str, cv_user_prompt, func,
7878
indices = _get_atom_indices(wizard, topology, n_atoms=n_atoms,
7979
cv_user_str=cv_user_prompt)
8080
kwargs = {kwarg_name: indices}
81+
atoms_str = " ".join([str(topology.mdtraj.atom(i)) for i in indices[0]])
8182

8283
summary = ("Here's what we'll create:\n"
8384
f" Function: {func.__name__}\n"
84-
f" Atoms: {" ".join([str(topology.mdtraj.atom(i))
85-
for i in indices[0]])} \n"
86-
f" Topology: {repr(topology.mdtraj))}"
85+
f" Atoms: {atoms_str}\n"
86+
f" Topology: {repr(topology.mdtraj)}")
8787
wizard.say(summary)
8888

8989
return MDTrajFunctionCV(func, topology, period_min=period_min,

‎paths_cli/wizard/load_from_ops.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def list_items(wizard, user_input):
99
store = getattr(storage, store_name)
1010
names = [obj for obj in store if obj.is_named]
1111
outstr = "\n".join(['* ' + obj.name for obj in names])
12-
wizard.say(f"Here's what I found:\n\n{outstr})
12+
wizard.say(f"Here's what I found:\n\n{outstr}")
1313

1414
return list_items
1515

‎paths_cli/wizard/openmm.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
else:
99
HAS_OPENMM = True
1010

11+
OPENMM_SERIALIZATION_URL=(
12+
"http://docs.openmm.org/latest/api-python/generated/"
13+
"simtk.openmm.openmm.XmlSerializer.html"
14+
)
1115

1216
def _openmm_serialization_helper(wizard, user_input): # no-cov
1317
wizard.say("You can write OpenMM objects like systems and integrators "
1418
"to XML files using the XMLSerializer class. Learn more "
15-
"here: \n"
16-
"http://docs.openmm.org/latest/api-python/generated/"
17-
"simtk.openmm.openmm.XmlSerializer.html")
19+
f"here: \n{OPENMM_SERIALIZATION_URL}")
1820

1921

2022
@get_object

‎paths_cli/wizard/tools.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ def a_an(obj):
22
return "an" if obj[0].lower() in "aeiou" else "a"
33

44
def yes_no(char):
5-
return {'y': True, 'n': False}[char]
5+
return {'yes': True, 'no': False, 'y': True, 'n': False}[char.lower()]

0 commit comments

Comments
 (0)