diff --git a/sew/_core.py b/sew/_core.py index e27188a..0794a63 100644 --- a/sew/_core.py +++ b/sew/_core.py @@ -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): diff --git a/sew/column.py b/sew/column.py index bbc8fa3..9387595 100644 --- a/sew/column.py +++ b/sew/column.py @@ -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) + diff --git a/tests/columns_conditions.py b/tests/columns_conditions.py index f06164e..751b0bd 100644 --- a/tests/columns_conditions.py +++ b/tests/columns_conditions.py @@ -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