From 8f4cd59c9ae98c90cf7cd8e50040f99573dff7d8 Mon Sep 17 00:00:00 2001 From: Stefan Wehrmeyer Date: Fri, 31 Jan 2014 20:42:04 +0100 Subject: [PATCH] Treat columns as ordered throughout code The set() does not make anything any easier, lists are closer to underlying representation. --- dataset/persistence/table.py | 7 +++++-- docs/quickstart.rst | 4 ++-- test/test_persistence.py | 3 +-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/dataset/persistence/table.py b/dataset/persistence/table.py index 55c294c..a36a9c9 100644 --- a/dataset/persistence/table.py +++ b/dataset/persistence/table.py @@ -25,7 +25,7 @@ class Table(object): """ Get a listing of all columns that exist in the table. """ - return set(self.table.columns.keys()) + return list(self.table.columns.keys()) def drop(self): """ @@ -184,7 +184,10 @@ class Table(object): self.database.executable.execute(stmt) def _ensure_columns(self, row, types={}): - for column in set(row.keys()) - set(self.table.columns.keys()): + # Keep order of inserted columns + for column in row.keys(): + if column in self.table.columns.keys(): + continue if column in types: _type = types[column] else: diff --git a/docs/quickstart.rst b/docs/quickstart.rst index 612ddbe..520468f 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -76,8 +76,8 @@ database: Now, let's list all columns available in the table ``user``: - >>> print db['user'].columns - set([u'id', u'name', u'email', u'pwd', u'country']) + >>> print(db['user'].columns) + [u'id', u'name', u'email', u'pwd', u'country'] Using ``len()`` we can get the total number of rows in a table: diff --git a/test/test_persistence.py b/test/test_persistence.py index 6ee319f..1d9a0f8 100644 --- a/test/test_persistence.py +++ b/test/test_persistence.py @@ -229,8 +229,7 @@ class TableTestCase(unittest.TestCase): def test_columns(self): cols = self.tbl.columns - assert isinstance(cols, set), 'columns should be a set' - assert len(cols) == 4, 'column count mismatch' + assert len(list(cols)) == 4, 'column count mismatch' assert 'date' in cols and 'temperature' in cols and 'place' in cols def test_iter(self):