From 66ae09e953f0ae760bbe20dc239b9c850d95d1a6 Mon Sep 17 00:00:00 2001 From: Kyle Sunden Date: Wed, 16 Dec 2020 10:25:32 -0600 Subject: [PATCH] Make implicit setables on call default behavior (#123) Used to error --- attune/_instrument.py | 14 ++++++++------ tests/instrument/test_call.py | 9 +++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/attune/_instrument.py b/attune/_instrument.py index 7bf50db..39b8ae6 100644 --- a/attune/_instrument.py +++ b/attune/_instrument.py @@ -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, @@ -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() } @@ -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, diff --git a/tests/instrument/test_call.py b/tests/instrument/test_call.py index 9ae5426..ba2c911 100644 --- a/tests/instrument/test_call.py +++ b/tests/instrument/test_call.py @@ -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)