Skip to content

Commit

Permalink
Make implicit setables on call default behavior (#123)
Browse files Browse the repository at this point in the history
Used to error
  • Loading branch information
ksunden authored Dec 16, 2020
1 parent f0c918e commit 66ae09e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
14 changes: 8 additions & 6 deletions attune/_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Instrument(object):
def __init__(
self,
arrangements: Dict["str", Union[Arrangement, dict]],
setables: Dict["str", Union[Setable, dict]],
setables: Dict["str", Optional[Union[Setable, dict]]] = None,
*,
name: Optional[str] = None,
transition: Optional[Union[Transition, dict]] = None,
Expand All @@ -25,6 +25,8 @@ def __init__(
self._arrangements: Dict["str", Arrangement] = {
k: Arrangement(**v) if isinstance(v, dict) else v for k, v in arrangements.items()
}
if setables is None:
setables = {}
self._setables: Dict["str", Setable] = {
k: Setable(**v) if isinstance(v, dict) else v for k, v in setables.items()
}
Expand Down Expand Up @@ -77,20 +79,20 @@ def __call__(self, ind_value, arrangement_name=None) -> Note:
raise ValueError("There are multiple valid arrangements! You must specify one.")
# call arrangement
setable_positions = {}
setables = self._setables.copy()
todo = [(ind_value, tune) for tune in arrangement.tunes.items()]
while todo:
v, t = todo.pop(0)
tune_name, tune = t
if tune_name in self._setables:
assert tune_name not in setable_positions
setable_positions[tune_name] = tune(v)
elif tune_name in self._arrangements:
if tune_name in self._arrangements:
new = [
(tune(v), subtune) for subtune in self._arrangements[tune_name].tunes.items()
]
todo += new
else:
raise ValueError(f"Unrecognized name {tune_name}")
assert tune_name not in setable_positions
setable_positions[tune_name] = tune(v)
setables[tune_name] = Setable(tune_name)
# finish
note = Note(
setables=self._setables,
Expand Down
9 changes: 9 additions & 0 deletions tests/instrument/test_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,12 @@ def test_nested():
second = attune.Arrangement("second", {"first": tune1})
inst = attune.Instrument({"first": first, "second": second}, {"tune": attune.Setable("tune")})
assert math.isclose(inst(0.75, "second")["tune"], 0.25)


def test_implicit_setable():
tune = attune.Tune([0, 1], [0, 1])
tune1 = attune.Tune([0.5, 1.5], [0, 1])
first = attune.Arrangement("first", {"tune": tune})
second = attune.Arrangement("second", {"first": tune1})
inst = attune.Instrument({"first": first, "second": second})
assert math.isclose(inst(0.75, "second")["tune"], 0.25)

0 comments on commit 66ae09e

Please sign in to comment.