Skip to content

Commit

Permalink
Upgrade table wrappers to public classes
Browse files Browse the repository at this point in the history
These are going to be commonly used by users so they shouldn't
be hidden as private classes in the sim module.
  • Loading branch information
jiffyclub committed Jul 29, 2014
1 parent 42facd5 commit d47122a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
24 changes: 12 additions & 12 deletions urbansim/sim/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SimulationError(Exception):
pass


class _DataFrameWrapper(object):
class DataFrameWrapper(object):
"""
Wraps a DataFrame so it can provide certain columns and handle
computed columns.
Expand Down Expand Up @@ -144,7 +144,7 @@ def __len__(self):
return len(self._frame)


class _TableFuncWrapper(object):
class TableFuncWrapper(object):
"""
Wrap a function that provides a DataFrame.
Expand Down Expand Up @@ -224,7 +224,7 @@ def to_frame(self, columns=None):
"""
frame = self._call_func()
return _DataFrameWrapper(self.name, frame).to_frame(columns)
return DataFrameWrapper(self.name, frame).to_frame(columns)

def get_column(self, column_name):
"""
Expand All @@ -251,7 +251,7 @@ def __len__(self):
return self._len


class _TableSourceWrapper(_TableFuncWrapper):
class TableSourceWrapper(TableFuncWrapper):
"""
Wraps a function that returns a DataFrame. After the function
is evaluated the returned DataFrame replaces the function in the
Expand All @@ -266,7 +266,7 @@ class _TableSourceWrapper(_TableFuncWrapper):
def convert(self):
"""
Evaluate the wrapped function, store the returned DataFrame as a
table, and return the new _DataFrameWrapper instance created.
table, and return the new DataFrameWrapper instance created.
"""
frame = self._call_func()
Expand Down Expand Up @@ -430,13 +430,13 @@ def add_table(table_name, table):
Returns
-------
wrapped : `_DataFrameWrapper` or `_TableFuncWrapper`
wrapped : `DataFrameWrapper` or `TableFuncWrapper`
"""
if isinstance(table, pd.DataFrame):
table = _DataFrameWrapper(table_name, table)
table = DataFrameWrapper(table_name, table)
elif isinstance(table, Callable):
table = _TableFuncWrapper(table_name, table)
table = TableFuncWrapper(table_name, table)
else:
raise TypeError('table must be DataFrame or function.')

Expand Down Expand Up @@ -475,10 +475,10 @@ def add_table_source(table_name, func):
Returns
-------
wrapped : `_TableSourceWrapper`
wrapped : `TableSourceWrapper`
"""
wrapped = _TableSourceWrapper(table_name, func)
wrapped = TableSourceWrapper(table_name, func)
_TABLES[table_name] = wrapped
return wrapped

Expand Down Expand Up @@ -506,7 +506,7 @@ def get_table(table_name):
Returns
-------
table : _DataFrameWrapper or _TableFuncWrapper
table : `DataFrameWrapper`, `TableFuncWrapper`, or `TableSourceWrapper`
"""
if table_name in _TABLES:
Expand Down Expand Up @@ -804,7 +804,7 @@ def merge_tables(target, tables, columns=None):
----------
target : str
Name of the table onto which tables will be merged.
tables : list of _DataFrameWrapper or _TableFuncWrapper
tables : list of `DataFrameWrapper` or `TableFuncWrapper`
All of the tables to merge. Should include the target table.
columns : list of str, optional
If given, columns will be mapped to `tables` and only those columns
Expand Down
12 changes: 6 additions & 6 deletions urbansim/sim/tests/test_mergetables.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@pytest.fixture
def dfa():
return sim._DataFrameWrapper('a', pd.DataFrame(
return sim.DataFrameWrapper('a', pd.DataFrame(
{'a1': [1, 2, 3],
'a2': [4, 5, 6],
'a3': [7, 8, 9]},
Expand All @@ -18,7 +18,7 @@ def dfa():

@pytest.fixture
def dfz():
return sim._DataFrameWrapper('z', pd.DataFrame(
return sim.DataFrameWrapper('z', pd.DataFrame(
{'z1': [90, 91],
'z2': [92, 93],
'z3': [94, 95],
Expand All @@ -29,7 +29,7 @@ def dfz():

@pytest.fixture
def dfb():
return sim._DataFrameWrapper('b', pd.DataFrame(
return sim.DataFrameWrapper('b', pd.DataFrame(
{'b1': range(10, 15),
'b2': range(15, 20),
'a_id': ['ac', 'ac', 'ab', 'aa', 'ab'],
Expand All @@ -39,7 +39,7 @@ def dfb():

@pytest.fixture
def dfc():
return sim._DataFrameWrapper('c', pd.DataFrame(
return sim.DataFrameWrapper('c', pd.DataFrame(
{'c1': range(20, 30),
'c2': range(30, 40),
'b_id': ['ba', 'bd', 'bb', 'bc', 'bb', 'ba', 'bb', 'bc', 'bd', 'bb']},
Expand All @@ -48,14 +48,14 @@ def dfc():

@pytest.fixture
def dfg():
return sim._DataFrameWrapper('g', pd.DataFrame(
return sim.DataFrameWrapper('g', pd.DataFrame(
{'g1': [1, 2, 3]},
index=['ga', 'gb', 'gc']))


@pytest.fixture
def dfh():
return sim._DataFrameWrapper('h', pd.DataFrame(
return sim.DataFrameWrapper('h', pd.DataFrame(
{'h1': range(10, 15),
'g_id': ['ga', 'gb', 'gc', 'ga', 'gb']},
index=['ha', 'hb', 'hc', 'hd', 'he']))
Expand Down
8 changes: 4 additions & 4 deletions urbansim/sim/tests/test_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def source():
return df

table = sim.get_table('source')
assert isinstance(table, sim._TableSourceWrapper)
assert isinstance(table, sim.TableSourceWrapper)

test_df = table.to_frame()
pdt.assert_frame_equal(test_df, df)
Expand All @@ -296,7 +296,7 @@ def source():
pdt.assert_index_equal(table.index, df.index)

table = sim.get_table('source')
assert isinstance(table, sim._DataFrameWrapper)
assert isinstance(table, sim.DataFrameWrapper)

test_df = table.to_frame()
pdt.assert_frame_equal(test_df, df)
Expand All @@ -308,10 +308,10 @@ def source():
return df

table = sim.get_table('source')
assert isinstance(table, sim._TableSourceWrapper)
assert isinstance(table, sim.TableSourceWrapper)

table = table.convert()
assert isinstance(table, sim._DataFrameWrapper)
assert isinstance(table, sim.DataFrameWrapper)
pdt.assert_frame_equal(table.to_frame(), df)

table2 = sim.get_table('source')
Expand Down

0 comments on commit d47122a

Please sign in to comment.