Skip to content

Commit

Permalink
Reset caches when replacing things in the registry
Browse files Browse the repository at this point in the history
Includes: tables, columns, and injectables.
  • Loading branch information
jiffyclub committed Jul 31, 2014
1 parent cc8b207 commit 8e9a071
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
10 changes: 10 additions & 0 deletions urbansim/sim/simulation.py
Expand Up @@ -648,6 +648,9 @@ def add_table(table_name, table, cache=False):
else:
raise TypeError('table must be DataFrame or function.')

# clear any cached data from a previously registered table
table.clear_cached()

logger.debug('registering table {!r}'.format(table_name))
_TABLES[table_name] = table

Expand Down Expand Up @@ -751,10 +754,15 @@ def add_column(table_name, column_name, column, cache=False):
else:
raise TypeError('Only Series or callable allowed for column.')

# clear any cached data from a previously registered column
column.clear_cached()

logger.debug('registering column {!r} on table {!r}'.format(
column_name, table_name))
_COLUMNS[(table_name, column_name)] = column

return column


def column(table_name, column_name, cache=False):
"""
Expand Down Expand Up @@ -830,6 +838,8 @@ def add_injectable(name, value, autocall=True, cache=False):
"""
if isinstance(value, Callable) and autocall:
value = _InjectableFuncWrapper(name, value, cache=cache)
# clear any cached data from a previously registered value
value.clear_cached()
logger.debug('registering injectable {!r}'.format(name))
_INJECTABLES[name] = value

Expand Down
14 changes: 13 additions & 1 deletion urbansim/sim/tests/test_simulation.py
Expand Up @@ -55,7 +55,7 @@ def test_func(test_frame):
assert table.columns == ['a', 'b']


def test_table_func_cached(df):
def test_table_func_cache(df):
sim.add_injectable('x', 2)

@sim.table('table', cache=True)
Expand All @@ -71,6 +71,10 @@ def table(x):
pdt.assert_frame_equal(sim.get_table('table').to_frame(), df * 3)
sim.clear_cache()
pdt.assert_frame_equal(sim.get_table('table').to_frame(), df * 4)
sim.add_injectable('x', 5)
pdt.assert_frame_equal(sim.get_table('table').to_frame(), df * 4)
sim.add_table('table', table)
pdt.assert_frame_equal(sim.get_table('table').to_frame(), df * 5)


def test_columns_for_table():
Expand Down Expand Up @@ -178,6 +182,10 @@ def col(x):
pdt.assert_series_equal(c()(), series * 4)
sim.get_table('table').clear_cached()
pdt.assert_series_equal(c()(), series * 5)
sim.add_injectable('x', 6)
pdt.assert_series_equal(c()(), series * 5)
sim.add_column(*key, column=col, cache=True)
pdt.assert_series_equal(c()(), series * 6)


def test_update_col(df):
Expand Down Expand Up @@ -376,6 +384,10 @@ def inj():
assert i()() == 9
sim.clear_cache()
assert i()() == 16
x = 5
assert i()() == 16
sim.add_injectable('inj', inj, autocall=True, cache=True)
assert i()() == 25


def test_table_source(df):
Expand Down

0 comments on commit 8e9a071

Please sign in to comment.