Skip to content

Commit

Permalink
Evaluate injectable functions in get_injecatble
Browse files Browse the repository at this point in the history
Only when it was registered with autocall=True.
  • Loading branch information
jiffyclub committed Oct 10, 2014
1 parent f6b6e04 commit 0545525
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
5 changes: 3 additions & 2 deletions urbansim/sim/simulation.py
Expand Up @@ -971,11 +971,12 @@ def get_injectable(name):
Returns
-------
injectable
Original value or _InjectableFuncWrapper if autocall was True.
Original value or evaluated value of an _InjectableFuncWrapper.
"""
if name in _INJECTABLES:
return _INJECTABLES[name]
i = _INJECTABLES[name]
return i() if isinstance(i, _InjectableFuncWrapper) else i
else:
raise KeyError('injectable not found: {}'.format(name))

Expand Down
16 changes: 11 additions & 5 deletions urbansim/sim/tests/test_simulation.py
Expand Up @@ -388,11 +388,17 @@ def inj_func3(func2):
def inj_func4(func1):
return func1 / 2

assert sim._INJECTABLES['answer'] == 42
assert sim._INJECTABLES['func1']() == 42 * 2
assert sim._INJECTABLES['func2'](4) == 2
assert sim._INJECTABLES['func3']() == 2
assert sim._INJECTABLES['func4']() == 42

assert sim.get_injectable('answer') == 42
assert sim.get_injectable('func1')() == 42 * 2
assert sim.get_injectable('func1') == 42 * 2
assert sim.get_injectable('func2')(4) == 2
assert sim.get_injectable('func3')() == 2
assert sim.get_injectable('func4')() == 42
assert sim.get_injectable('func3') == 2
assert sim.get_injectable('func4') == 42

assert set(sim.list_injectables()) == \
{'answer', 'func1', 'func2', 'func3', 'func4'}
Expand Down Expand Up @@ -428,7 +434,7 @@ def test_injectables_cache():
def inj():
return x * x

i = lambda: sim.get_injectable('inj')
i = lambda: sim._INJECTABLES['inj']

assert i()() == 4
x = 3
Expand All @@ -452,7 +458,7 @@ def test_injectables_cache_disabled():
def inj():
return x * x

i = lambda: sim.get_injectable('inj')
i = lambda: sim._INJECTABLES['inj']

sim.disable_cache()

Expand Down

0 comments on commit 0545525

Please sign in to comment.