Skip to content

Commit

Permalink
Remove Table.__init__
Browse files Browse the repository at this point in the history
As I do not want to show Table.__init__ to users,
I rename it to Table._initialize.
  • Loading branch information
fjkz committed Feb 24, 2017
1 parent bc48c9e commit 20a60d5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
16 changes: 10 additions & 6 deletions inline_table.py
Expand Up @@ -152,7 +152,7 @@ def compile(text, **variables):

# Convert strings to ColumnType values
column_types = [ColumnType.get_column_type(a) for a in column_types]
table = Table(labels, column_types)
table = Table()._initialize(labels, column_types)
for row in rows:
# Evaluate the literal in each cell with given variables.
row_evaluated = []
Expand Down Expand Up @@ -197,11 +197,14 @@ class Table:
"""Data structure having a table data.
Table objects are created by the ``compile`` function.
A user does not create a table object by himself.
"""

def __init__(self, labels, column_types=None):
"""Initialize the object.
def _initialize(self, labels, column_types=None):
"""Private initializer.
This method is defined as I do not show __init__ for users. Call
``Table._initialize(labels, column_types)`` instead of
``Table(labels column_types)``.
:param labels: list of label names
:param column_types: list of column types
Expand All @@ -215,6 +218,7 @@ def __init__(self, labels, column_types=None):
column_types = [ColumnType.Value() for _ in labels]
self.column_types = self.Tuple(*column_types)
self.rows = []
return self

def __str__(self):
"""Return Tab separated values."""
Expand Down Expand Up @@ -250,7 +254,7 @@ def _insert(self, row_values):
def iterator(self):
"""Return a iterator object.
The ``iter`` build-in function or for-loop is also available.
The ``iter`` build-in function and for-loops are also available.
A row that contains the not-applicable value is skipped.
Expand Down Expand Up @@ -539,7 +543,7 @@ def getvalue(row, label):
# If the row does not have the label, return the wild card.
return WildCard

joined_table = Table(union_labels, union_ctypes)
joined_table = Table()._initialize(union_labels, union_ctypes)

for l_row in self.rows:
for r_row in other.rows:
Expand Down
26 changes: 13 additions & 13 deletions test_inline_table.py
Expand Up @@ -573,28 +573,28 @@ def test_no_arg(self):
class TestTable(unittest.TestCase):

def test_labels(self):
tb = Table(['keyA', 'keyB', 'keyC'])
tb = Table()._initialize(['keyA', 'keyB', 'keyC'])
ret = tb._labels
self.assertEqual(ret, ('keyA', 'keyB', 'keyC'))

def test_one_key_no_value(self):
tb = Table(['key'])
tb = Table()._initialize(['key'])
try:
tb.select(key='value')
self.fail()
except LookupError as _ok:
pass

def test_one_key_one_value(self):
tb = Table(['key'])
tb = Table()._initialize(['key'])
tb._insert(['value'])

ret = tb.select(key='value')

self.assertEqual(ret, ('value',))

def test_one_key_two_value(self):
tb = Table(['key'])
tb = Table()._initialize(['key'])
tb._insert(['value1'])
tb._insert(['value2'])

Expand All @@ -603,7 +603,7 @@ def test_one_key_two_value(self):
self.assertEqual(ret, ('value1',))

def test_two_key_two_value1(self):
tb = Table(['keyA', 'keyB'])
tb = Table()._initialize(['keyA', 'keyB'])
tb._insert(['value1A', 'value1B'])
tb._insert(['value2A', 'value2B'])

Expand All @@ -612,7 +612,7 @@ def test_two_key_two_value1(self):
self.assertEqual(ret, ('value1A', 'value1B'))

def test_two_key_two_value2(self):
tb = Table(['keyA', 'keyB'])
tb = Table()._initialize(['keyA', 'keyB'])
tb._insert(['value1A', 'value1B'])
tb._insert(['value2A', 'value2B'])

Expand All @@ -621,7 +621,7 @@ def test_two_key_two_value2(self):
self.assertEqual(ret, ('value2A', 'value2B'))

def test_incorrect_label(self):
tb = Table(['keyA', 'keyB'])
tb = Table()._initialize(['keyA', 'keyB'])
try:
tb.select(keyC=1)
self.fail()
Expand Down Expand Up @@ -668,8 +668,8 @@ def test_plus_operator(self):
self.assertEqual(next(it), (4, 8))

def test_width_diff(self):
t1 = Table(['a', 'b'])
t2 = Table(['a', 'b', 'c'])
t1 = Table()._initialize(['a', 'b'])
t2 = Table()._initialize(['a', 'b', 'c'])
try:
t1 + t2
self.fail()
Expand All @@ -679,8 +679,8 @@ def test_width_diff(self):
"Width of the tables are different: 2 != 3")

def test_labels_diff(self):
t1 = Table(['a', 'b'])
t2 = Table(['a', 'c'])
t1 = Table()._initialize(['a', 'b'])
t2 = Table()._initialize(['a', 'c'])
try:
t1 + t2
self.fail()
Expand All @@ -690,8 +690,8 @@ def test_labels_diff(self):
"Labels of the tables are different: ('a', 'b') != ('a', 'c')")

def test_column_types_diff(self):
t1 = Table(['a', 'b'], [ColumnType.Value(), ColumnType.Condition()])
t2 = Table(['a', 'b'], [ColumnType.Value(), ColumnType.String()])
t1 = Table()._initialize(['a', 'b'], [ColumnType.Value(), ColumnType.Condition()])
t2 = Table()._initialize(['a', 'b'], [ColumnType.Value(), ColumnType.String()])
try:
t1 + t2
self.fail()
Expand Down

0 comments on commit 20a60d5

Please sign in to comment.