Skip to content

Commit 6b2a7dd

Browse files
committed
Merge branch 'master' into enh/DirectoryType
2 parents 99ba655 + 638957c commit 6b2a7dd

File tree

3 files changed

+47
-28
lines changed

3 files changed

+47
-28
lines changed

pydra/engine/helpers_state.py

+18-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@
1010
logger = logging.getLogger("pydra")
1111

1212

13+
class PydraStateError(Exception):
14+
"""Custom error for Pydra State"""
15+
16+
def __init__(self, value):
17+
self.value = value
18+
19+
def __str__(self):
20+
return "{}".format(self.value)
21+
22+
1323
def splitter2rpn(splitter, other_states=None, state_fields=True):
1424
"""
1525
Translate user-provided splitter into *reverse polish notation*.
@@ -49,7 +59,7 @@ def _ordering(
4959
if type(el[0]) is str and el[0].startswith("_"):
5060
node_nm = el[0][1:]
5161
if node_nm not in other_states and state_fields:
52-
raise Exception(
62+
raise PydraStateError(
5363
"can't ask for splitter from {}, other nodes that are connected: {}".format(
5464
node_nm, other_states.keys()
5565
)
@@ -64,7 +74,7 @@ def _ordering(
6474
if type(el[1]) is str and el[1].startswith("_"):
6575
node_nm = el[1][1:]
6676
if node_nm not in other_states and state_fields:
67-
raise Exception(
77+
raise PydraStateError(
6878
"can't ask for splitter from {}, other nodes that are connected: {}".format(
6979
node_nm, other_states.keys()
7080
)
@@ -87,7 +97,7 @@ def _ordering(
8797
if type(el[0]) is str and el[0].startswith("_"):
8898
node_nm = el[0][1:]
8999
if node_nm not in other_states and state_fields:
90-
raise Exception(
100+
raise PydraStateError(
91101
"can't ask for splitter from {}, other nodes that are connected: {}".format(
92102
node_nm, other_states.keys()
93103
)
@@ -102,7 +112,7 @@ def _ordering(
102112
if type(el[1]) is str and el[1].startswith("_"):
103113
node_nm = el[1][1:]
104114
if node_nm not in other_states and state_fields:
105-
raise Exception(
115+
raise PydraStateError(
106116
"can't ask for splitter from {}, other nodes that are connected: {}".format(
107117
node_nm, other_states.keys()
108118
)
@@ -125,7 +135,7 @@ def _ordering(
125135
if el.startswith("_"):
126136
node_nm = el[1:]
127137
if node_nm not in other_states and state_fields:
128-
raise Exception(
138+
raise PydraStateError(
129139
"can't ask for splitter from {}, other nodes that are connected: {}".format(
130140
node_nm, other_states.keys()
131141
)
@@ -156,7 +166,7 @@ def _ordering(
156166
state_fields=state_fields,
157167
)
158168
else:
159-
raise Exception("splitter has to be a string, a tuple or a list")
169+
raise PydraStateError("splitter has to be a string, a tuple or a list")
160170
if i > 0:
161171
output_splitter.append(current_sign)
162172

@@ -702,7 +712,7 @@ def _single_op_splits_groups(op_single, combiner, inner_inputs, groups):
702712
return [], {}, [], combiner
703713
else:
704714
# TODO: probably not needed, should be already check by st.combiner_validation
705-
raise Exception(
715+
raise PydraStateError(
706716
"all fields from the combiner have to be in splitter_rpn: {}, but combiner: {} is set".format(
707717
[op_single], combiner
708718
)
@@ -733,7 +743,7 @@ def combine_final_groups(combiner, groups, groups_stack, keys):
733743
elif gr in grs_removed:
734744
pass
735745
else:
736-
raise Exception(
746+
raise PydraStateError(
737747
"input {} not ready to combine, you have to combine {} "
738748
"first".format(comb, groups_stack[-1])
739749
)

pydra/engine/state.py

+18-10
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ def splitter(self):
111111
@splitter.setter
112112
def splitter(self, splitter):
113113
if splitter and not isinstance(splitter, (str, tuple, list)):
114-
raise Exception("splitter has to be a string, a tuple or a list")
114+
raise hlpst.PydraStateError(
115+
"splitter has to be a string, a tuple or a list"
116+
)
115117
if splitter:
116118
self._splitter = hlpst.add_name_splitter(splitter, self.name)
117119
else:
@@ -209,7 +211,7 @@ def combiner(self):
209211
def combiner(self, combiner):
210212
if combiner:
211213
if not isinstance(combiner, (str, list)):
212-
raise Exception("combiner has to be a string or a list")
214+
raise hlpst.PydraStateError("combiner has to be a string or a list")
213215
self._combiner = hlpst.add_name_combiner(ensure_list(combiner), self.name)
214216
else:
215217
self._combiner = []
@@ -251,11 +253,13 @@ def other_states(self):
251253
def other_states(self, other_states):
252254
if other_states:
253255
if not isinstance(other_states, dict):
254-
raise Exception("other states has to be a dictionary")
256+
raise hlpst.PydraStateError("other states has to be a dictionary")
255257
else:
256258
for key, val in other_states.items():
257259
if not val:
258-
raise Exception(f"connection from node {key} is empty")
260+
raise hlpst.PydraStateError(
261+
f"connection from node {key} is empty"
262+
)
259263
self._other_states = other_states
260264
else:
261265
self._other_states = {}
@@ -360,7 +364,9 @@ def _left_right_check(self, splitter_part, check_nested=True):
360364
):
361365
return "[Left, Right]" # Left and Right parts separated in outer scalar
362366
else:
363-
raise Exception("Left and Right splitters are mixed - splitter invalid")
367+
raise hlpst.PydraStateError(
368+
"Left and Right splitters are mixed - splitter invalid"
369+
)
364370

365371
def set_input_groups(self, state_fields=True):
366372
"""Evaluate groups, especially the final groups that address the combiner."""
@@ -409,7 +415,7 @@ def _merge_previous_groups(self):
409415
):
410416
last_gr = last_gr - 1
411417
if left_nm[1:] not in self.other_states:
412-
raise Exception(
418+
raise hlpst.PydraStateError(
413419
f"can't ask for splitter from {left_nm[1:]}, other nodes that are connected: {self.other_states}"
414420
)
415421
st = self.other_states[left_nm[1:]][0]
@@ -434,7 +440,7 @@ def _merge_previous_groups(self):
434440
)
435441
self.keys_final += keys_f_st # st.keys_final
436442
if not hasattr(st, "group_for_inputs_final"):
437-
raise Exception("previous state has to run first")
443+
raise hlpst.PydraStateError("previous state has to run first")
438444
group_for_inputs = group_for_inputs_f_st
439445
groups_stack = groups_stack_f_st
440446
self.left_combiner_all += combiner_all_st
@@ -487,7 +493,7 @@ def splitter_validation(self):
487493
or spl.startswith("_")
488494
or spl.split(".")[0] == self.name
489495
):
490-
raise Exception(
496+
raise hlpst.PydraStateError(
491497
"can't include {} in the splitter, consider using _{}".format(
492498
spl, spl.split(".")[0]
493499
)
@@ -497,9 +503,11 @@ def combiner_validation(self):
497503
""" validating if the combiner is correct (after all states are connected)"""
498504
if self.combiner:
499505
if not self.splitter:
500-
raise Exception("splitter has to be set before setting combiner")
506+
raise hlpst.PydraStateError(
507+
"splitter has to be set before setting combiner"
508+
)
501509
if set(self._combiner) - set(self.splitter_rpn):
502-
raise Exception("all combiners have to be in the splitter")
510+
raise hlpst.PydraStateError("all combiners have to be in the splitter")
503511

504512
def prepare_states(self, inputs, cont_dim=None):
505513
"""

pydra/engine/tests/test_state.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22

33
from ..state import State
4+
from ..helpers_state import PydraStateError
45

56

67
@pytest.mark.parametrize(
@@ -95,27 +96,27 @@ def test_state_1(
9596

9697

9798
def test_state_2_err():
98-
with pytest.raises(Exception) as exinfo:
99+
with pytest.raises(PydraStateError) as exinfo:
99100
st = State("NA", splitter={"a"})
100101
assert "splitter has to be a string, a tuple or a list" == str(exinfo.value)
101102

102103

103104
def test_state_3_err():
104-
with pytest.raises(Exception) as exinfo:
105+
with pytest.raises(PydraStateError) as exinfo:
105106
st = State("NA", splitter=["a", "b"], combiner=("a", "b"))
106107
assert "combiner has to be a string or a list" == str(exinfo.value)
107108

108109

109110
def test_state_4_err():
110111
st = State("NA", splitter="a", combiner=["a", "b"])
111-
with pytest.raises(Exception) as exinfo:
112+
with pytest.raises(PydraStateError) as exinfo:
112113
st.combiner_validation()
113114
assert "all combiners have to be in the splitter" in str(exinfo.value)
114115

115116

116117
def test_state_5_err():
117118
st = State("NA", combiner="a")
118-
with pytest.raises(Exception) as exinfo:
119+
with pytest.raises(PydraStateError) as exinfo:
119120
st.combiner_validation()
120121
assert "splitter has to be set before" in str(exinfo.value)
121122

@@ -166,7 +167,7 @@ def test_state_connect_1b_exception():
166167
"""can't provide explicitly NA.a (should be _NA)"""
167168
st1 = State(name="NA", splitter="a", other_states={})
168169
st2 = State(name="NB", splitter="NA.a")
169-
with pytest.raises(Exception) as excinfo:
170+
with pytest.raises(PydraStateError) as excinfo:
170171
st2.splitter_validation()
171172
assert "consider using _NA" in str(excinfo.value)
172173

@@ -175,7 +176,7 @@ def test_state_connect_1b_exception():
175176
def test_state_connect_1c_exception(splitter2, other_states2):
176177
"""can't ask for splitter from node that is not connected"""
177178
st1 = State(name="NA", splitter="a")
178-
with pytest.raises(Exception) as excinfo:
179+
with pytest.raises(PydraStateError) as excinfo:
179180
st2 = State(name="NB", splitter=splitter2, other_states=other_states2)
180181
st2.splitter_validation()
181182

@@ -691,7 +692,7 @@ def test_state_connect_innerspl_1a():
691692

692693
def test_state_connect_innerspl_1b():
693694
"""incorrect splitter - Right & Left parts in scalar splitter"""
694-
with pytest.raises(Exception):
695+
with pytest.raises(PydraStateError):
695696
st1 = State(name="NA", splitter="a")
696697
st2 = State(name="NB", splitter=("_NA", "b"), other_states={"NA": (st1, "b")})
697698

@@ -1718,7 +1719,7 @@ def test_connect_splitters(
17181719
],
17191720
)
17201721
def test_connect_splitters_exception_1(splitter, other_states):
1721-
with pytest.raises(Exception) as excinfo:
1722+
with pytest.raises(PydraStateError) as excinfo:
17221723
st = State(name="CN", splitter=splitter, other_states=other_states)
17231724
assert "Left and Right splitters are mixed" in str(excinfo.value)
17241725

@@ -1729,13 +1730,13 @@ def test_connect_splitters_exception_2():
17291730
splitter="_NB",
17301731
other_states={"NA": (State(name="NA", splitter="a"), "b")},
17311732
)
1732-
with pytest.raises(Exception) as excinfo:
1733+
with pytest.raises(PydraStateError) as excinfo:
17331734
st.set_input_groups()
17341735
assert "can't ask for splitter from NB" in str(excinfo.value)
17351736

17361737

17371738
def test_connect_splitters_exception_3():
1738-
with pytest.raises(Exception) as excinfo:
1739+
with pytest.raises(PydraStateError) as excinfo:
17391740
st = State(
17401741
name="CN",
17411742
splitter="_NB",

0 commit comments

Comments
 (0)