Oleg Broytmann, el 24 de enero a las 10:23 me escribiste:
> On Tue, Jan 23, 2007 at 07:24:42PM -0300, Leandro Lucarella wrote:
> > And tableDict[str(table)] = 1 is way too ugly, right? =)
> 
>    No, but someone has to patch SQLObject (and there are many places where
> tableDict is used, including inheritance) and write tests.

There are not that many places:

trunk$ grep tablesDict -lr sqlobject/ | grep -v .svn | egrep -v '\.pyc$'
sqlobject/inheritance/__init__.py
sqlobject/sresults.py

Patch attached, but I don't know what kind of testing do you want. I
couldn't even run the current tests because I can't get py.test right now,
but I've tried the example on the bug report and worked =)

Things like tablesDict[sourceClass.sqlmeta.table] = 1 were patched too
just in case in the future sqlmeta.table becomes a Table ;)

There is another patch that converts tables.*Dict to tables.*Set. This
patch requires Python 2.3 but AFAIK Python 2.2 support was dropped in 0.8
so this should'n be an issue. Both pathes are provided against 0.8 branch
and trunk. The "set" patches includes the other (the "str" one).

-- 
Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/
 .------------------------------------------------------------------------,
  \  GPG: 5F5A8D05 // F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05 /
   '--------------------------------------------------------------------'
Karma police
arrest this girl,
her Hitler hairdo
is making me feel ill
and we have crashed her party.
Index: sqlobject/inheritance/__init__.py
===================================================================
--- sqlobject/inheritance/__init__.py	(revisión: 2240)
+++ sqlobject/inheritance/__init__.py	(copia de trabajo)
@@ -33,11 +33,11 @@
         if clause is None or isinstance(clause, str) and clause == 'all':
             clause = sqlbuilder.SQLTrueClause
         tablesDict = tablesUsedDict(clause)
-        tablesDict[sourceClass.sqlmeta.table] = 1
+        tablesDict[str(sourceClass.sqlmeta.table)] = 1
         orderBy = ops.get('orderBy')
         if inheritedTables:
             for tableName in inheritedTables:
-                tablesDict[tableName] = 1
+                tablesDict[str(tableName)] = 1
         if orderBy and not isinstance(orderBy, basestring):
             tablesDict.update(tablesUsedDict(orderBy))
         #DSM: if this class has a parent, we need to link it
@@ -51,7 +51,7 @@
             allClasses = classregistry.registry(
                 sourceClass.sqlmeta.registry).allClasses()
             for registryClass in allClasses:
-                if registryClass.sqlmeta.table in tablesDict:
+                if str(registryClass.sqlmeta.table) in tablesDict:
                     #DSM: By default, no parents are needed for the clauses
                     tableRegistry[registryClass] = registryClass
             tableRegistryCopy = tableRegistry.copy()
@@ -78,7 +78,7 @@
                     parentClass = currentClass.sqlmeta.parentClass
                     parentClause.append(currentClass.q.id == parentClass.q.id)
                     currentClass = parentClass
-                    tablesDict[currentClass.sqlmeta.table] = 1
+                    tablesDict[str(currentClass.sqlmeta.table)] = 1
             clause = reduce(sqlbuilder.AND, parentClause, clause)
 
         super(InheritableSelectResults, self).__init__(sourceClass,
Index: sqlobject/sqlbuilder.py
===================================================================
--- sqlobject/sqlbuilder.py	(revisión: 2240)
+++ sqlobject/sqlbuilder.py	(copia de trabajo)
@@ -187,7 +187,7 @@
     def tablesUsedDict(self):
         tables = {}
         for table in self.tablesUsedImmediate():
-            tables[table] = 1
+            tables[str(table)] = 1
         for component in self.components():
             tables.update(tablesUsedDict(component))
         return tables
Index: sqlobject/sresults.py
===================================================================
--- sqlobject/sresults.py	(revisión: 2240)
+++ sqlobject/sresults.py	(copia de trabajo)
@@ -14,10 +14,10 @@
             clause = sqlbuilder.SQLTrueClause
         self.clause = clause
         tablesDict = sqlbuilder.tablesUsedDict(self.clause)
-        tablesDict[sourceClass.sqlmeta.table] = 1
+        tablesDict[str(sourceClass.sqlmeta.table)] = 1
         if clauseTables:
             for table in clauseTables:
-                tablesDict[table] = 1
+                tablesDict[str(table)] = 1
         self.clauseTables = clauseTables
         self.tables = tablesDict.keys()
         self.ops = ops
Index: sqlobject/inheritance/__init__.py
===================================================================
--- sqlobject/inheritance/__init__.py	(revisión: 2240)
+++ sqlobject/inheritance/__init__.py	(copia de trabajo)
@@ -5,6 +5,11 @@
    makeProperties, getterName, setterName
 import iteration
 
+try:
+    set
+except NameError: # Python 2.3
+    from sets import Set, ImmutableSet
+    set, frozenset = Set, ImmutableSet
 
 try:
     basestring
@@ -13,16 +18,16 @@
     basestring = (types.StringType, types.UnicodeType)
 
 
-def tablesUsedDict(obj):
-    if hasattr(obj, "tablesUsedDict"):
-        return obj.tablesUsedDict()
-    elif isinstance(obj, (tuple, list)):
-        d = {}
+def tablesUsedSet(obj):
+    if hasattr(obj, "tablesUsedSet"):
+        return obj.tablesUsedSet()
+    elif isinstance(obj, (tuple, list, set, frozenset)):
+        s = set()
         for component in obj:
-            d.update(tablesUsedDict(component))
-        return d
+            s.update(tablesUsedSet(component))
+        return s
     else:
-        return {}
+        return set()
 
 
 class InheritableSelectResults(SelectResults):
@@ -32,14 +37,14 @@
             inheritedTables=None, **ops):
         if clause is None or isinstance(clause, str) and clause == 'all':
             clause = sqlbuilder.SQLTrueClause
-        tablesDict = tablesUsedDict(clause)
-        tablesDict[sourceClass.sqlmeta.table] = 1
+        tablesSet = tablesUsedSet(clause)
+        tablesSet.add(str(sourceClass.sqlmeta.table))
         orderBy = ops.get('orderBy')
         if inheritedTables:
             for tableName in inheritedTables:
-                tablesDict[tableName] = 1
+                tablesSet.add(str(tableName))
         if orderBy and not isinstance(orderBy, basestring):
-            tablesDict.update(tablesUsedDict(orderBy))
+            tablesSet.update(tablesUsedSet(orderBy))
         #DSM: if this class has a parent, we need to link it
         #DSM: and be sure the parent is in the table list.
         #DSM: The following code is before clauseTables
@@ -51,7 +56,7 @@
             allClasses = classregistry.registry(
                 sourceClass.sqlmeta.registry).allClasses()
             for registryClass in allClasses:
-                if registryClass.sqlmeta.table in tablesDict:
+                if str(registryClass.sqlmeta.table) in tablesSet:
                     #DSM: By default, no parents are needed for the clauses
                     tableRegistry[registryClass] = registryClass
             tableRegistryCopy = tableRegistry.copy()
@@ -78,7 +83,7 @@
                     parentClass = currentClass.sqlmeta.parentClass
                     parentClause.append(currentClass.q.id == parentClass.q.id)
                     currentClass = parentClass
-                    tablesDict[currentClass.sqlmeta.table] = 1
+                    tablesSet.add(str(currentClass.sqlmeta.table))
             clause = reduce(sqlbuilder.AND, parentClause, clause)
 
         super(InheritableSelectResults, self).__init__(sourceClass,
Index: sqlobject/sqlbuilder.py
===================================================================
--- sqlobject/sqlbuilder.py	(revisión: 2240)
+++ sqlobject/sqlbuilder.py	(copia de trabajo)
@@ -67,6 +67,12 @@
 class NoDefault:
     pass
 
+try:
+    set
+except NameError: # Python 2.3
+    from sets import Set, ImmutableSet
+    set, frozenset = Set, ImmutableSet
+
 True, False = (1==1), (0==1)
 
 import re, fnmatch
@@ -183,13 +189,12 @@
         return []
 
     def tablesUsed(self):
-        return self.tablesUsedDict().keys()
-    def tablesUsedDict(self):
-        tables = {}
-        for table in self.tablesUsedImmediate():
-            tables[table] = 1
+        return self.tablesUsedSet()
+    def tablesUsedSet(self):
+        tables = set()
+        tables.update(self.tablesUsedImmediate())
         for component in self.components():
-            tables.update(tablesUsedDict(component))
+            tables.update(tablesUsedSet(component))
         return tables
     def tablesUsedImmediate(self):
         return []
@@ -203,9 +208,9 @@
 
 registerConverter(SQLExpression, SQLExprConverter)
 
-def tablesUsedDict(obj):
-    if hasattr(obj, "tablesUsedDict"):
-        return obj.tablesUsedDict()
+def tablesUsedSet(obj):
+    if hasattr(obj, "tablesUsedSet"):
+        return obj.tablesUsedSet()
     else:
         return {}
 
@@ -482,7 +487,7 @@
             things.append(self.whereClause)
         for thing in things:
             if isinstance(thing, SQLExpression):
-                tables.update(tablesUsedDict(thing))
+                tables.update(tablesUsedSet(thing))
         for j in join:
             if j.table1 in tables: del tables[j.table1]
             if j.table2 in tables: del tables[j.table2]
Index: sqlobject/sresults.py
===================================================================
--- sqlobject/sresults.py	(revisión: 2240)
+++ sqlobject/sresults.py	(copia de trabajo)
@@ -13,13 +13,13 @@
         if clause is None or isinstance(clause, str) and clause == 'all':
             clause = sqlbuilder.SQLTrueClause
         self.clause = clause
-        tablesDict = sqlbuilder.tablesUsedDict(self.clause)
-        tablesDict[sourceClass.sqlmeta.table] = 1
+        tablesSet = sqlbuilder.tablesUsedSet(self.clause)
+        tablesSet.add(str(sourceClass.sqlmeta.table))
         if clauseTables:
             for table in clauseTables:
-                tablesDict[table] = 1
+                tablesSet.add(str(table))
         self.clauseTables = clauseTables
-        self.tables = tablesDict.keys()
+        self.tables = list(tablesSet)
         self.ops = ops
         if self.ops.get('orderBy', sqlbuilder.NoDefault) is sqlbuilder.NoDefault:
             self.ops['orderBy'] = sourceClass.sqlmeta.defaultOrder
Index: sqlobject/inheritance/__init__.py
===================================================================
--- sqlobject/inheritance/__init__.py	(revisión: 2229)
+++ sqlobject/inheritance/__init__.py	(copia de trabajo)
@@ -20,7 +20,7 @@
         if clause is None or isinstance(clause, str) and clause == 'all':
             clause = sqlbuilder.SQLTrueClause
         tablesDict = sqlbuilder.tablesUsedDict(clause)
-        tablesDict[sourceClass.sqlmeta.table] = 1
+        tablesDict[str(sourceClass.sqlmeta.table)] = 1
         orderBy = ops.get('orderBy')
         if orderBy and not isinstance(orderBy, basestring):
             tablesDict.update(sqlbuilder.tablesUsedDict(orderBy))
@@ -35,7 +35,7 @@
             allClasses = classregistry.registry(
                 sourceClass.sqlmeta.registry).allClasses()
             for registryClass in allClasses:
-                if registryClass.sqlmeta.table in tablesDict:
+                if str(registryClass.sqlmeta.table) in tablesDict:
                     #DSM: By default, no parents are needed for the clauses
                     tableRegistry[registryClass] = registryClass
             tableRegistryCopy = tableRegistry.copy()
@@ -62,7 +62,7 @@
                     parentClass = currentClass.sqlmeta.parentClass
                     parentClause.append(currentClass.q.id == parentClass.q.id)
                     currentClass = parentClass
-                    tablesDict[currentClass.sqlmeta.table] = 1
+                    tablesDict[str(currentClass.sqlmeta.table)] = 1
             clause = reduce(sqlbuilder.AND, parentClause, clause)
 
         super(InheritableSelectResults, self).__init__(sourceClass,
Index: sqlobject/sqlbuilder.py
===================================================================
--- sqlobject/sqlbuilder.py	(revisión: 2229)
+++ sqlobject/sqlbuilder.py	(copia de trabajo)
@@ -187,7 +187,7 @@
     def tablesUsedDict(self):
         tables = {}
         for table in self.tablesUsedImmediate():
-            tables[table] = 1
+            tables[str(table)] = 1
         for component in self.components():
             tables.update(tablesUsedDict(component))
         return tables
Index: sqlobject/sresults.py
===================================================================
--- sqlobject/sresults.py	(revisión: 2229)
+++ sqlobject/sresults.py	(copia de trabajo)
@@ -14,10 +14,10 @@
             clause = sqlbuilder.SQLTrueClause
         self.clause = clause
         tablesDict = sqlbuilder.tablesUsedDict(self.clause)
-        tablesDict[sourceClass.sqlmeta.table] = 1
+        tablesDict[str(sourceClass.sqlmeta.table)] = 1
         if clauseTables:
             for table in clauseTables:
-                tablesDict[table] = 1
+                tablesDict[str(table)] = 1
         self.clauseTables = clauseTables
         self.tables = tablesDict.keys()
         self.ops = ops
Index: sqlobject/inheritance/__init__.py
===================================================================
--- sqlobject/inheritance/__init__.py	(revisión: 2229)
+++ sqlobject/inheritance/__init__.py	(copia de trabajo)
@@ -5,6 +5,11 @@
    makeProperties, getterName, setterName
 import iteration
 
+try:
+    set
+except NameError: # Python 2.3
+    from sets import Set, ImmutableSet
+    set, frozenset = Set, ImmutableSet
 
 try:
     basestring
@@ -19,11 +24,11 @@
     def __init__(self, sourceClass, clause, clauseTables=None, **ops):
         if clause is None or isinstance(clause, str) and clause == 'all':
             clause = sqlbuilder.SQLTrueClause
-        tablesDict = sqlbuilder.tablesUsedDict(clause)
-        tablesDict[sourceClass.sqlmeta.table] = 1
+        tablesSet = sqlbuilder.tablesUsedSet(clause)
+        tablesSet.add(str(sourceClass.sqlmeta.table))
         orderBy = ops.get('orderBy')
         if orderBy and not isinstance(orderBy, basestring):
-            tablesDict.update(sqlbuilder.tablesUsedDict(orderBy))
+            tablesSet.update(sqlbuilder.tablesUsedSet(orderBy))
         #DSM: if this class has a parent, we need to link it
         #DSM: and be sure the parent is in the table list.
         #DSM: The following code is before clauseTables
@@ -35,7 +40,7 @@
             allClasses = classregistry.registry(
                 sourceClass.sqlmeta.registry).allClasses()
             for registryClass in allClasses:
-                if registryClass.sqlmeta.table in tablesDict:
+                if str(registryClass.sqlmeta.table) in tablesSet:
                     #DSM: By default, no parents are needed for the clauses
                     tableRegistry[registryClass] = registryClass
             tableRegistryCopy = tableRegistry.copy()
@@ -62,7 +67,7 @@
                     parentClass = currentClass.sqlmeta.parentClass
                     parentClause.append(currentClass.q.id == parentClass.q.id)
                     currentClass = parentClass
-                    tablesDict[currentClass.sqlmeta.table] = 1
+                    tablesSet.add(str(currentClass.sqlmeta.table))
             clause = reduce(sqlbuilder.AND, parentClause, clause)
 
         super(InheritableSelectResults, self).__init__(sourceClass,
Index: sqlobject/sqlbuilder.py
===================================================================
--- sqlobject/sqlbuilder.py	(revisión: 2229)
+++ sqlobject/sqlbuilder.py	(copia de trabajo)
@@ -67,6 +67,12 @@
 class NoDefault:
     pass
 
+try:
+    set
+except NameError: # Python 2.3
+    from sets import Set, ImmutableSet
+    set, frozenset = Set, ImmutableSet
+
 True, False = (1==1), (0==1)
 
 import re, fnmatch
@@ -183,13 +189,12 @@
         return []
 
     def tablesUsed(self):
-        return self.tablesUsedDict().keys()
-    def tablesUsedDict(self):
-        tables = {}
-        for table in self.tablesUsedImmediate():
-            tables[table] = 1
+        return self.tablesUsedSet()
+    def tablesUsedSet(self):
+        tables = set()
+        tables.update(self.tablesUsedImmediate())
         for component in self.components():
-            tables.update(tablesUsedDict(component))
+            tables.update(tablesUsedSet(component))
         return tables
     def tablesUsedImmediate(self):
         return []
@@ -203,11 +208,11 @@
 
 registerConverter(SQLExpression, SQLExprConverter)
 
-def tablesUsedDict(obj):
-    if hasattr(obj, "tablesUsedDict"):
-        return obj.tablesUsedDict()
+def tablesUsedSet(obj):
+    if hasattr(obj, "tablesUsedSet"):
+        return obj.tablesUsedSet()
     else:
-        return {}
+        return set()
 
 operatorMap = {
     "+": operator.add,
@@ -482,7 +487,7 @@
             things.append(self.whereClause)
         for thing in things:
             if isinstance(thing, SQLExpression):
-                tables.update(tablesUsedDict(thing))
+                tables.update(tablesUsedSet(thing))
         for j in join:
             if j.table1 in tables: del tables[j.table1]
             if j.table2 in tables: del tables[j.table2]
Index: sqlobject/sresults.py
===================================================================
--- sqlobject/sresults.py	(revisión: 2229)
+++ sqlobject/sresults.py	(copia de trabajo)
@@ -13,13 +13,13 @@
         if clause is None or isinstance(clause, str) and clause == 'all':
             clause = sqlbuilder.SQLTrueClause
         self.clause = clause
-        tablesDict = sqlbuilder.tablesUsedDict(self.clause)
-        tablesDict[sourceClass.sqlmeta.table] = 1
+        tablesSet = sqlbuilder.tablesUsedSet(self.clause)
+        tablesSet.add(str(sourceClass.sqlmeta.table))
         if clauseTables:
             for table in clauseTables:
-                tablesDict[table] = 1
+                tablesSet.add(str(table))
         self.clauseTables = clauseTables
-        self.tables = tablesDict.keys()
+        self.tables = list(tablesSet)
         self.ops = ops
         if self.ops.get('orderBy', sqlbuilder.NoDefault) is sqlbuilder.NoDefault:
             self.ops['orderBy'] = sourceClass.sqlmeta.defaultOrder
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
sqlobject-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Reply via email to