Skip to content

Commit

Permalink
replace tableproxy internals with ColumnProxyContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
icyveins7 committed Mar 22, 2024
1 parent b910259 commit e708a82
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
18 changes: 9 additions & 9 deletions sew/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,27 +269,27 @@ def __init__(self, parent: SqliteContainer, tbl: str, fmt: dict):
self._fmt = fmt
self._cols = self._populateColumns()

def _populateColumns(self):
cols = dict()
def _populateColumns(self) -> ColumnProxyContainer:
cols = list()
# typehints =
for col in self._fmt['cols']:
colname = col[0]
# Parse the type (note that we cannot determine the upper/lowercase)
if re.match(r"int", col[1], flags=re.IGNORECASE): # All the versions have the substring 'int', so this works
cols[colname] = ColumnProxy(colname, int, self._tbl)
cols.append(ColumnProxy(colname, int, self._tbl))
elif re.match(r"text", col[1], flags=re.IGNORECASE) or re.match(r"char", col[1], flags=re.IGNORECASE):
cols[colname] = ColumnProxy(colname, str, self._tbl)
cols.append(ColumnProxy(colname, str, self._tbl))
elif re.match(r"real", col[1], flags=re.IGNORECASE) or re.match(r"double", col[1], flags=re.IGNORECASE) or re.match(r"float", col[1], flags=re.IGNORECASE):
cols[colname] = ColumnProxy(colname, float, self._tbl)
cols.append(ColumnProxy(colname, float, self._tbl))
elif re.match(r"blob", col[1], flags=re.IGNORECASE):
cols[colname] = ColumnProxy(colname, bytes, self._tbl)
cols.append(ColumnProxy(colname, bytes, self._tbl))
elif re.match(r"numeric", col[1], flags=re.IGNORECASE):
cols[colname] = ColumnProxy(colname, (int, float), self._tbl)
cols.append(ColumnProxy(colname, (int, float), self._tbl))
else:
# cols[colname] = ColumnProxy(colname, object)
# cols.append(ColumnProxy(colname, object)
raise NotImplementedError("Unknown parse for sql type %s" % col[1])

return cols
return ColumnProxyContainer(cols)


def __getitem__(self, i: slice):
Expand Down
3 changes: 3 additions & 0 deletions sew/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,8 @@ def __init__(self, columnProxies: list[ColumnProxy]):
# Here is where we set the attribute directly
setattr(self, col.name, col)

def __getitem__(self, key: str) -> ColumnProxy:
return getattr(self, key)



15 changes: 15 additions & 0 deletions tests/columns_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@
import unittest

#%%
class TestColumnProxyContainer(unittest.TestCase):
def test_columnProxyContainer_simple(self):
column1 = sew.ColumnProxy("col1", "int", "mytbl")
column2 = sew.ColumnProxy("col2", "int", "mytbl")
container = sew.ColumnProxyContainer([column1, column2])

# Retrieve by dict-like string
self.assertEqual(column1, container['col1'])
self.assertEqual(column2, container['col2'])

# Retrieve by attribute
self.assertEqual(column1, container.col1)
self.assertEqual(column2, container.col2)


class TestColumnProxy(unittest.TestCase):
def test_columnProxy_properties(self):
# Create a simple one
Expand Down

0 comments on commit e708a82

Please sign in to comment.