diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-07-04 13:56:17 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-07-04 13:56:17 -0400 |
| commit | 3daae3e5b6e54163452ed2aca15b300544daf455 (patch) | |
| tree | 4fb760e4bb01c3b263cf3e3baf91667ee9d6e5a0 /lib/sqlalchemy | |
| parent | c0ffa33b2fc676734004895eefe6bc058f105fd6 (diff) | |
| download | sqlalchemy-3daae3e5b6e54163452ed2aca15b300544daf455.tar.gz | |
- Added an improved repr() to TypeEngine objects
that will only display constructor args which
are positional or kwargs that deviate
from the default. [ticket:2209]
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/types.py | 6 | ||||
| -rw-r--r-- | lib/sqlalchemy/util/__init__.py | 3 | ||||
| -rw-r--r-- | lib/sqlalchemy/util/langhelpers.py | 27 |
3 files changed, 30 insertions, 6 deletions
diff --git a/lib/sqlalchemy/types.py b/lib/sqlalchemy/types.py index e8d0b6f22..c7781a76f 100644 --- a/lib/sqlalchemy/types.py +++ b/lib/sqlalchemy/types.py @@ -263,11 +263,7 @@ class TypeEngine(AbstractType): "constructor %s is deprecated" % self.__class__) def __repr__(self): - return "%s(%s)" % ( - self.__class__.__name__, - ", ".join("%s=%r" % (k, getattr(self, k, None)) - for k in inspect.getargspec(self.__init__)[0][1:])) - + return util.generic_repr(self) class UserDefinedType(TypeEngine): """Base for user defined types. diff --git a/lib/sqlalchemy/util/__init__.py b/lib/sqlalchemy/util/__init__.py index 827fce088..4b7a43752 100644 --- a/lib/sqlalchemy/util/__init__.py +++ b/lib/sqlalchemy/util/__init__.py @@ -26,7 +26,8 @@ from langhelpers import iterate_attributes, class_hierarchy, \ monkeypatch_proxied_specials, asbool, bool_or_str, coerce_kw_type,\ duck_type_collection, assert_arg_type, symbol, dictlike_iteritems,\ classproperty, set_creation_order, warn_exception, warn, NoneType,\ - constructor_copy, methods_equivalent, chop_traceback, asint + constructor_copy, methods_equivalent, chop_traceback, asint,\ + generic_repr from deprecations import warn_deprecated, warn_pending_deprecation, \ deprecated, pending_deprecation diff --git a/lib/sqlalchemy/util/langhelpers.py b/lib/sqlalchemy/util/langhelpers.py index e9213845b..c3a358220 100644 --- a/lib/sqlalchemy/util/langhelpers.py +++ b/lib/sqlalchemy/util/langhelpers.py @@ -215,6 +215,33 @@ def unbound_method_to_callable(func_or_cls): else: return func_or_cls +def generic_repr(obj): + """Produce a __repr__() based on direct association of the __init__() + specification vs. same-named attributes present. + + """ + def genargs(): + try: + (args, vargs, vkw, defaults) = inspect.getargspec(obj.__init__) + except TypeError: + return + + default_len = defaults and len(defaults) or 0 + + if not default_len: + for arg in args[1:]: + yield repr(getattr(obj, arg, None)) + if vargs is not None and hasattr(obj, vargs): + yield ', '.join(repr(val) for val in getattr(obj, vargs)) + else: + for arg in args[1:-default_len]: + yield repr(getattr(obj, arg, None)) + for (arg, defval) in zip(args[-default_len:], defaults): + val = getattr(obj, arg, None) + if val != defval: + yield '%s=%r' % (arg, val) + return "%s(%s)" % (obj.__class__.__name__, ", ".join(genargs())) + class portable_instancemethod(object): """Turn an instancemethod into a (parent, name) pair to produce a serializable callable. |
