]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
added some __repr__ functionality
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 8 Jan 2006 19:02:26 +0000 (19:02 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 8 Jan 2006 19:02:26 +0000 (19:02 +0000)
lib/sqlalchemy/schema.py
lib/sqlalchemy/types.py
lib/sqlalchemy/util.py
test/engines.py

index 026825737e1734551440bc3680c250720184b8c7..b0cf931d8846da1ef633b16656a5f6e232426ceb 100644 (file)
@@ -17,7 +17,7 @@ the schema package "plugs in" to the SQL package.
 
 from sqlalchemy.util import *
 from sqlalchemy.types import *
-import copy, re
+import copy, re, string
 
 __all__ = ['SchemaItem', 'Table', 'Column', 'ForeignKey', 'Sequence', 'SchemaEngine', 'SchemaVisitor']
 
@@ -42,6 +42,9 @@ class SchemaItem(object):
         """returns a string that identifies this SchemaItem uniquely"""
         return repr(self)
 
+    def __repr__(self):
+        return "%s()" % self.__class__.__name__
+
     def __getattr__(self, key):
         """proxies method calls to an underlying implementation object for methods not found
         locally"""
@@ -143,6 +146,13 @@ class Table(SchemaItem):
             self.fullname = self.name
         if len(kwargs):
             raise "Unknown arguments passed to Table: " + repr(kwargs.keys())
+
+    def __repr__(self):
+       return "Table(%s)" % string.join(
+        [repr(self.name)] + [repr(self.engine)] +
+        [repr(x) for x in self.columns] +
+        ["%s=%s" % (k, repr(getattr(self, k))) for k in ['schema']]
+       , ',\n')
     
     def reload_values(self, *args):
         """clears out the columns and other properties of this Table, and reloads them from the 
@@ -237,7 +247,14 @@ class Column(SchemaItem):
         
     original = property(lambda s: s._orig or s)
     engine = property(lambda s: s.table.engine)
-    
+     
+    def __repr__(self):
+       return "Column(%s)" % string.join(
+        [repr(self.name)] + [repr(self.type)] +
+        [repr(x) for x in [self.foreign_key] if x is not None] +
+        ["%s=%s" % (k, repr(getattr(self, k))) for k in ['key', 'primary_key', 'nullable', 'hidden', 'default']]
+       , ',')
+        
     def append_item(self, item):
         self._init_items(item)
         
@@ -327,16 +344,21 @@ class ForeignKey(SchemaItem):
         self._colspec = column
         self._column = None
 
+    def __repr__(self):
+        return "ForeignKey(%s)" % repr(self._get_colspec())
+        
     def copy(self):
         """produces a copy of this ForeignKey object."""
+        return ForeignKey(self._get_colspec())
+    
+    def _get_colspec(self):
         if isinstance(self._colspec, str):
-            return ForeignKey(self._colspec)
+            return self._colspec
+        elif self._colspec.table.schema is not None:
+            return "%s.%s.%s" % (self._colspec.table.schema, self._colspec.table.name, self._colspec.column.key)
         else:
-            if self._colspec.table.schema is not None:
-                return ForeignKey("%s.%s.%s" % (self._colspec.table.schema, self._colspec.table.name, self._colspec.column.key))
-            else:
-                return ForeignKey("%s.%s" % (self._colspec.table.name, self._colspec.column.key))
-    
+            return "%s.%s" % (self._colspec.table.name, self._colspec.column.key)
+        
     def references(self, table):
         """returns True if the given table is referenced by this ForeignKey."""
         try:
@@ -390,7 +412,9 @@ class DefaultGenerator(SchemaItem):
     def _set_parent(self, column):
         self.column = column
         self.column.default = self
-
+    def __repr__(self):
+        return "DefaultGenerator()"
+        
 class ColumnDefault(DefaultGenerator):
     """A plain default value on a column.  this could correspond to a constant, 
     a callable function, or a SQL clause."""
@@ -399,6 +423,8 @@ class ColumnDefault(DefaultGenerator):
     def accept_visitor(self, visitor):
         """calls the visit_column_default method on the given visitor."""
         return visitor.visit_column_default(self)
+    def __repr__(self):
+        return "ColumnDefault(%s)" % repr(self.arg)
         
 class Sequence(DefaultGenerator):
     """represents a sequence, which applies to Oracle and Postgres databases."""
@@ -407,6 +433,12 @@ class Sequence(DefaultGenerator):
         self.start = start
         self.increment = increment
         self.optional=optional
+    def __repr__(self):
+        return "Sequence(%s)" % string.join(
+             [repr(self.name)] +
+             ["%s=%s" % (k, repr(getattr(self, k))) for k in ['start', 'increment', 'optional']]
+            , ',')
+    
     def accept_visitor(self, visitor):
         """calls the visit_seauence method on the given visitor."""
         return visitor.visit_sequence(self)
index 3b2b5cf043da6ddab0693291de8d41b544e3c12e..970dcbd31ccad7708f99c20fca82b35a815073d4 100644 (file)
@@ -9,6 +9,7 @@ __all__ = [ 'TypeEngine', 'TypeDecorator', 'NullTypeEngine',
             'TIMESTAMP', 'DATETIME', 'CLOB', 'BLOB', 'BOOLEAN', 'String', 'Integer', 'Numeric', 'Float', 'DateTime', 'Binary', 'Boolean', 'Unicode', 'NULLTYPE'
             ]
 
+import sqlalchemy.util as util
 
 class TypeEngine(object):
     def get_col_spec(self):
@@ -21,6 +22,8 @@ class TypeEngine(object):
         return typeobj()
     def adapt_args(self):
         return self
+    def __repr__(self):
+        return util.generic_repr(self)
         
 def adapt_type(typeobj, colspecs):
     """given a generic type from this package, and a dictionary of 
index b922a82461ff461e9bd67961055fd937be2288b8..7f417ba7e65320c98d7f365e3dfbb1df1f4635b0 100644 (file)
@@ -4,7 +4,7 @@
 # This module is part of SQLAlchemy and is released under
 # the MIT License: http://www.opensource.org/licenses/mit-license.php
 
-__all__ = ['OrderedProperties', 'OrderedDict']
+__all__ = ['OrderedProperties', 'OrderedDict', 'generic_repr', 'HashSet']
 import thread, weakref, UserList,string, inspect
 
 def to_list(x):
@@ -14,6 +14,10 @@ def to_list(x):
         return [x]
     else:
         return x
+
+def generic_repr(obj, exclude=None):
+    L = ['%s=%s' % (a, repr(getattr(obj, a))) for a in dir(obj) if not callable(getattr(obj, a)) and not a.startswith('_') and (exclude is None or not exclude.has_key(a))]
+    return '%s(%s)' % (obj.__class__.__name__, ','.join(L))
         
 class OrderedProperties(object):
     """an object that maintains the order in which attributes are set upon it.
index 8a0bc2458346abde5e02fed3e316c089dfacbc07..0ed537bca38a70b702e4e38648c3b854a804552a 100644 (file)
@@ -35,6 +35,9 @@ class EngineTest(PersistTest):
             Column('email_address', String(20)),
         )
 
+        print repr(users)
+        print repr(addresses)
+        
 #        users.c.parent_user_id.set_foreign_key(ForeignKey(users.c.user_id))
 
         users.create()