From 300d1d2c136462201c79ff19cb6b8c2bbc0c8dfd Mon Sep 17 00:00:00 2001 From: Jason Kirtland Date: Thu, 3 May 2007 00:57:59 +0000 Subject: - 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 --- lib/sqlalchemy/util.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'lib/sqlalchemy/util.py') 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.""" -- cgit v1.2.1