Skip to content

Commit c17f3d1

Browse files
committed
more work toward using get_object
1 parent 4da1b9a commit c17f3d1

5 files changed

Lines changed: 89 additions & 84 deletions

File tree

‎paths_cli/wizard/cvs.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
else:
1313
HAS_MDTRAJ = True
1414

15-
def mdtraj_atom_helper(wizard, user_input, n_atoms):
15+
def mdtraj_atom_helper(wizard, user_input, n_atoms): # no-cov
1616
wizard.say("You should specify atom indices enclosed in double "
1717
"brackets, e.g, [" + str(list(range(n_atoms))) + "]")
1818
# TODO: implement the following:
@@ -28,6 +28,7 @@ def mdtraj_atom_helper(wizard, user_input, n_atoms):
2828
def _get_topology(wizard):
2929
from paths_cli.wizard.engines import engines
3030
topology = None
31+
# TODO: isn't this get_missing_object?
3132
while topology is None:
3233
if len(wizard.engines) == 0:
3334
# SHOULD NEVER GET HERE
@@ -48,10 +49,10 @@ def _get_topology(wizard):
4849
return topology
4950

5051
def _get_atom_indices(wizard, topology, n_atoms, cv_user_str):
51-
# TODO: move the logic here to parsing.tools
5252
arr = None
5353
helper = partial(mdtraj_atom_helper, n_atoms=n_atoms)
5454
while arr is None:
55+
# switch to get_custom_eval
5556
atoms_str = wizard.ask(f"Which atoms do you want to {cv_user_str}?",
5657
helper=helper)
5758
try:
@@ -62,6 +63,7 @@ def _get_atom_indices(wizard, topology, n_atoms, cv_user_str):
6263
continue
6364

6465
try:
66+
# move this logic to parsing.tools
6567
arr = np.array(indices)
6668
if arr.dtype != int:
6769
raise TypeError("Input is not integers")

‎paths_cli/wizard/load_from_ops.py‎

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
LABEL = "Load existing from OPS file"
22
from paths_cli.parameters import INPUT_FILE
3+
from paths_cli.wizard.core import get_object
34

45
def named_objs_helper(storage, store_name):
56
def list_items(wizard, user_input):
@@ -10,28 +11,35 @@ def list_items(wizard, user_input):
1011

1112
return list_items
1213

14+
@get_object
15+
def _get_ops_storage(wizard):
16+
filename = wizard.ask("What file can it be found in?",
17+
default='filename')
18+
try:
19+
storage = INPUT_FILE.get(filename)
20+
except Exception as e:
21+
wizard.exception(FILE_LOADING_ERROR_MSG, e)
22+
return
1323

14-
def load_from_ops(wizard, store_name, obj_name):
15-
wizard.say("Okay, we'll load it from an OPS file.")
16-
storage = None
17-
while storage is None:
18-
filename = wizard.ask("What file can it be found in?",
19-
default='filename')
24+
return storage
25+
26+
@get_object
27+
def _get_ops_object(wizard, storage, store_name, obj_name):
28+
name = wizard.ask(f"What's the name of the {obj_name} you want to "
29+
"load? (Type '?' to get a list of them)",
30+
helper=named_objs_helper(storage, store_name))
31+
if name:
2032
try:
21-
storage = INPUT_FILE.get(filename)
33+
obj = getattr(storage, store_name)[name]
2234
except Exception as e:
23-
wizard.exception(FILE_LOADING_ERROR_MSG, e)
24-
# TODO: error handling
35+
wizard.exception("Something went wrong when loading "
36+
f"{name}. Maybe check the spelling?", e)
37+
return
38+
else:
39+
return obj
2540

26-
obj = None
27-
while obj is None:
28-
name = wizard.ask(f"What's the name of the {obj_name} you want to "
29-
"load? (Type '?' to get a list of them)",
30-
helper=named_objs_helper(storage, store_name))
31-
if name:
32-
try:
33-
obj = getattr(storage, store_name)[name]
34-
except Exception as e:
35-
wizard.exception("Something went wrong when loading "
36-
f"{name}. Maybe check the spelling?", e)
41+
def load_from_ops(wizard, store_name, obj_name):
42+
wizard.say("Okay, we'll load it from an OPS file.")
43+
storage = _get_ops_storage(wizard)
44+
obj = _get_ops_object(wizard, storage, store_name, obj_name)
3745
return obj

‎paths_cli/wizard/shooting.py‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,9 @@ def _get_selector(wizard, selectors):
4949
if selectors is None:
5050
selectors = SHOOTING_SELECTORS
5151
selector = None
52-
while selector is None:
53-
sel = wizard.ask_enumerate("How do you want to select shooting "
54-
"points?", options=list(selectors.keys()))
55-
selector = selectors[sel](wizard)
56-
52+
sel = wizard.ask_enumerate("How do you want to select shooting "
53+
"points?", options=list(selectors.keys()))
54+
selector = selectors[sel](wizard)
5755
return selector
5856

5957
def one_way_shooting(wizard, selectors=None, engine=None):
@@ -112,12 +110,11 @@ def shooting(wizard, shooting_types=None, engine=None):
112110
if len(shooting_types) == 1:
113111
shooting_type = list(shooting_types.values())[0]
114112
else:
115-
while shooting_type is None:
116-
type_name = wizard.ask_enumerate(
117-
"Select the type of shooting move.",
118-
options=list(shooting_types.keys())
119-
)
120-
shooting_type = shooting_types[type_name]
113+
type_name = wizard.ask_enumerate(
114+
"Select the type of shooting move.",
115+
options=list(shooting_types.keys())
116+
)
117+
shooting_type = shooting_types[type_name]
121118

122119
shooting_strategy = shooting_type(wizard)
123120
return shooting_strategy

‎paths_cli/wizard/tps.py‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from functools import partial
1818

1919
def _select_states(wizard, state_type):
20+
# TODO: change this significantly ... I dislike in usage
2021
states = []
2122
do_another = True
2223
while do_another:
@@ -39,6 +40,7 @@ def _select_states(wizard, state_type):
3940
return state_objs
4041

4142
def _get_pathlength(wizard):
43+
# redo as get_custom_eval with int type
4244
pathlength = None
4345
while pathlength is None:
4446
len_str = wizard.ask("How long (in frames) do you want yout "
@@ -58,6 +60,7 @@ def flex_length_tps_network(wizard):
5860
return network
5961

6062
def fixed_length_tps_network(wizard):
63+
import openpathsampling as paths
6164
pathlength = _get_pathlength(wizard)
6265
initial_states = _select_states(wizard, 'initial')
6366
final_states = _select_states(wizard, 'final')
@@ -74,12 +77,11 @@ def tps_network(wizard):
7477
FIXED: paths.FixedLengthTPSNetwork,
7578
}
7679
network_type = None
77-
while network_type is None:
78-
network_type = wizard.ask_enumerate(
79-
"Do you want to do flexible path length TPS (recommended) "
80-
"or fixed path length TPS?", options=list(tps_types.keys())
81-
)
82-
network_class = tps_types[network_type]
80+
network_type = wizard.ask_enumerate(
81+
"Do you want to do flexible path length TPS (recommended) "
82+
"or fixed path length TPS?", options=list(tps_types.keys())
83+
)
84+
network_class = tps_types[network_type]
8385

8486
if network_type == FIXED:
8587
pathlength = _get_pathlength(wizard)
@@ -102,7 +104,7 @@ def _get_network(wizard):
102104
networks = list(wizard.networks.keys())
103105
sel = wizard.ask_enumerate("Which network would you like to use?",
104106
options=networks)
105-
network = wizard.networks[network]
107+
network = wizard.networks[sel]
106108
return network
107109

108110
def tps_scheme(wizard, network=None):
@@ -120,9 +122,6 @@ def tps_scheme(wizard, network=None):
120122
scheme.append(global_strategy)
121123
return scheme
122124

123-
def tps_finalize(wizard):
124-
pass
125-
126125
def tps_setup(wizard):
127126
network = tps_network(wizard)
128127
scheme = tps_scheme(wizard, network)
@@ -134,5 +133,4 @@ def tps_setup(wizard):
134133
if __name__ == "__main__":
135134
from paths_cli.wizard.wizard import Wizard
136135
wiz = Wizard({'tps_network': ('networks', 1, '='),
137-
'tps_scheme': ('schemes', 1, '='),
138-
'tps_finalize': (None, None, None)})
136+
'tps_scheme': ('schemes', 1, '=')})

‎paths_cli/wizard/wizard.py‎

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
flex_length_tps_network, fixed_length_tps_network, tps_scheme
1111
)
1212
from paths_cli.wizard.tools import yes_no, a_an
13-
from paths_cli.parsing.core import custom_eval
13+
from paths_cli.wizard.core import get_object
1414
from paths_cli.wizard.errors import (
1515
FILE_LOADING_ERROR_MSG, RestartObjectException
1616
)
1717
from paths_cli.wizard.joke import name_joke
18+
from paths_cli.parsing.core import custom_eval
1819

1920
import shutil
2021

@@ -73,14 +74,13 @@ def _speak(self, content, preface):
7374
wrapped.append(wrap_line)
7475
self.console.print("\n".join(wrapped))
7576

77+
@get_object
7678
def ask(self, question, options=None, default=None, helper=None):
77-
result = None
78-
while result is None:
79-
result = self.console.input("🧙 " + question + " ")
80-
self.console.print()
81-
if helper and result.startswith("?"):
82-
helper(self, result)
83-
result = None
79+
result = self.console.input("🧙 " + question + " ")
80+
self.console.print()
81+
if helper and result.startswith("?"):
82+
helper(self, result)
83+
return
8484
return result
8585

8686
def say(self, content, preface="🧙 "):
@@ -117,21 +117,20 @@ def ask_enumerate(self, question, options):
117117
return result
118118

119119
# this should match the args for wizard.ask
120+
@get_object
120121
def ask_custom_eval(self, question, options=None, default=None,
121122
helper=None, type_=float):
122-
result = None
123-
while result is None:
124-
as_str = self.ask(question, options=options, default=default,
125-
helper=helper)
126-
try:
127-
result = type_(custom_eval(as_str))
128-
except Exception as e:
129-
self.exception(f"Sorry, I couldn't understand the input "
130-
f"'{as_str}'", e)
131-
result = None
132-
123+
as_str = self.ask(question, options=options, default=default,
124+
helper=helper)
125+
try:
126+
result = type_(custom_eval(as_str))
127+
except Exception as e:
128+
self.exception(f"Sorry, I couldn't understand the input "
129+
f"'{as_str}'", e)
130+
return
133131
return result
134132

133+
135134
def obj_selector(self, store_name, text_name, create_func):
136135
opts = {name: lambda wiz, o=obj: o
137136
for name, obj in getattr(self, store_name).items()}
@@ -173,31 +172,32 @@ def register(self, obj, obj_type, store_name):
173172
store_dict[obj.name] = obj
174173
return obj
175174

175+
@get_object
176176
def get_storage(self):
177177
from openpathsampling.experimental.storage import Storage
178-
storage = None
179-
while storage is None:
180-
filename = self.ask("Where would you like to save your setup "
181-
"database?")
182-
if not filename.endswith(".db"):
183-
self.bad_input("Files produced by this wizard must end in "
184-
"'.db'.")
185-
continue
186-
187-
if os.path.exists(filename):
188-
overwrite = self.ask(f"{filename} exists. Overwrite it?",
189-
options=["[Y]es", "[N]o"])
190-
overwrite = yes_no(overwrite)
191-
if not overwrite:
192-
continue
193-
194-
try:
195-
storage = Storage(filename, mode='w')
196-
except Exception as e:
197-
self.exception(FILE_LOADING_ERROR_MSG, e)
178+
filename = self.ask("Where would you like to save your setup "
179+
"database?")
180+
if not filename.endswith(".db"):
181+
self.bad_input("Files produced by this wizard must end in "
182+
"'.db'.")
183+
return
184+
185+
if os.path.exists(filename):
186+
overwrite = self.ask(f"{filename} exists. Overwrite it?",
187+
options=["[Y]es", "[N]o"])
188+
overwrite = yes_no(overwrite)
189+
if not overwrite:
190+
return
191+
192+
try:
193+
storage = Storage(filename, mode='w')
194+
except Exception as e:
195+
self.exception(FILE_LOADING_ERROR_MSG, e)
196+
return
198197

199198
return storage
200199

200+
201201
def _storage_description_line(self, store_name):
202202
store = getattr(self, store_name)
203203
if len(store) == 0:

0 commit comments

Comments
 (0)