[SQL-CVS] r1145 - in SQLObject/trunk: docs sqlobject sqlobject/tests
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: <sub...@co...> - 2005-10-26 23:13:23
|
Author: test Date: 2005-10-26 23:13:17 +0000 (Wed, 26 Oct 2005) New Revision: 1145 Modified: SQLObject/trunk/docs/interface.py SQLObject/trunk/sqlobject/main.py SQLObject/trunk/sqlobject/tests/test_basic.py Log: Added .sqlmeta.asDict() method Modified: SQLObject/trunk/docs/interface.py =================================================================== --- SQLObject/trunk/docs/interface.py 2005-10-26 23:06:11 UTC (rev 1144) +++ SQLObject/trunk/docs/interface.py 2005-10-26 23:13:17 UTC (rev 1145) @@ -254,7 +254,19 @@ Adds the index to the class. """ + def asDict(): + """ + Returns the SQLObject instance as a dictionary (column names + as keys, column values as values). + Use like:: + + ASQLObjectClass(a=1, b=2).asDict() + + Which should return ``{'a': 1, 'b': 2}``. + """ + + class ICol(Interface): def __init__(name=None, **kw): Modified: SQLObject/trunk/sqlobject/main.py =================================================================== --- SQLObject/trunk/sqlobject/main.py 2005-10-26 23:06:11 UTC (rev 1144) +++ SQLObject/trunk/sqlobject/main.py 2005-10-26 23:13:17 UTC (rev 1145) @@ -562,6 +562,16 @@ cls.indexes.append(index) addIndex = classmethod(addIndex) + ######################################## + ## Utility methods + ######################################## + + def asDict(self): + result = {} + for key in self.columns: + result[key] = getattr(self.instance, key) + return result + sqlhub = dbconnection.ConnectionHub() class _sqlmeta_attr(object): Modified: SQLObject/trunk/sqlobject/tests/test_basic.py =================================================================== --- SQLObject/trunk/sqlobject/tests/test_basic.py 2005-10-26 23:06:11 UTC (rev 1144) +++ SQLObject/trunk/sqlobject/tests/test_basic.py 2005-10-26 23:13:17 UTC (rev 1145) @@ -275,3 +275,9 @@ assert dep1.other is None assert dep2.other is None assert dep3.other is obj2 + +def testAsDict(): + setupGetters(TestSO1) + bob = TestSO1.selectBy(name='bob')[0] + assert bob.sqlmeta.asDict() == {'passwd': 'tbq', 'name': 'bob'} + |