diff options
author | Jason Kirtland <jek@discorporate.us> | 2007-05-03 00:57:59 +0000 |
---|---|---|
committer | Jason Kirtland <jek@discorporate.us> | 2007-05-03 00:57:59 +0000 |
commit | 300d1d2c136462201c79ff19cb6b8c2bbc0c8dfd (patch) | |
tree | 1ae489dc49024405e3c1eca3dfc4f1b8daca6883 /lib/sqlalchemy/util.py | |
parent | 612c49f545b5374be45dbb4da21a5d708ebb894f (diff) | |
download | sqlalchemy-300d1d2c136462201c79ff19cb6b8c2bbc0c8dfd.tar.gz |
- New association proxy implementation, implementing complete proxies to list, dict and set-based relation collections (and scalar relations). Extensive tests.
- Added util.duck_type_collection
Diffstat (limited to 'lib/sqlalchemy/util.py')
-rw-r--r-- | lib/sqlalchemy/util.py | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/lib/sqlalchemy/util.py b/lib/sqlalchemy/util.py index e4b0efad4..ea5a468d2 100644 --- a/lib/sqlalchemy/util.py +++ b/lib/sqlalchemy/util.py @@ -103,12 +103,30 @@ def coerce_kw_type(kw, key, type_, flexi_bool=True): necessary. If 'flexi_bool' is True, the string '0' is considered false when coercing to boolean. """ + if key in kw and type(kw[key]) is not type_ and kw[key] is not None: if type_ is bool and flexi_bool and kw[key] == '0': kw[key] = False else: kw[key] = type_(kw[key]) +def duck_type_collection(col, default=None): + """Given an instance or class, guess if it is or is acting as one of + the basic collection types: list, set and dict. If the __emulates__ + property is present, return that preferentially. + """ + + if hasattr(col, '__emulates__'): + return getattr(col, '__emulates__') + elif hasattr(col, 'append'): + return list + elif hasattr(col, 'add'): + return Set + elif hasattr(col, 'set'): + return dict + else: + return default + class SimpleProperty(object): """A *default* property accessor.""" |