From: Mike Bayer Date: Sun, 8 Jan 2006 19:02:26 +0000 (+0000) Subject: added some __repr__ functionality X-Git-Tag: rel_0_1_0~149 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=37c762622cf4ea91d34a591b5f71771db27bb5cb;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git added some __repr__ functionality --- diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py index 026825737e..b0cf931d88 100644 --- a/lib/sqlalchemy/schema.py +++ b/lib/sqlalchemy/schema.py @@ -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) diff --git a/lib/sqlalchemy/types.py b/lib/sqlalchemy/types.py index 3b2b5cf043..970dcbd31c 100644 --- a/lib/sqlalchemy/types.py +++ b/lib/sqlalchemy/types.py @@ -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 diff --git a/lib/sqlalchemy/util.py b/lib/sqlalchemy/util.py index b922a82461..7f417ba7e6 100644 --- a/lib/sqlalchemy/util.py +++ b/lib/sqlalchemy/util.py @@ -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. diff --git a/test/engines.py b/test/engines.py index 8a0bc24583..0ed537bca3 100644 --- a/test/engines.py +++ b/test/engines.py @@ -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()