summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/util.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2007-10-16 21:33:21 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2007-10-16 21:33:21 +0000
commitcc0e7c796f61e17cb7050de0701905c65ce558eb (patch)
tree1c63b86a5ae37a6b5c44bbecf86182d4c415f43b /lib/sqlalchemy/util.py
parenta54deba01d07e8e0971db31a96379d275ccfb0d3 (diff)
downloadsqlalchemy-cc0e7c796f61e17cb7050de0701905c65ce558eb.tar.gz
- string-based query param parsing/config file parser understands
wider range of string values for booleans [ticket:817]
Diffstat (limited to 'lib/sqlalchemy/util.py')
-rw-r--r--lib/sqlalchemy/util.py16
1 files changed, 14 insertions, 2 deletions
diff --git a/lib/sqlalchemy/util.py b/lib/sqlalchemy/util.py
index 633e6b0c1..6fd3a81ef 100644
--- a/lib/sqlalchemy/util.py
+++ b/lib/sqlalchemy/util.py
@@ -153,6 +153,18 @@ def get_func_kwargs(func):
"""Return the full set of legal kwargs for the given `func`."""
return [vn for vn in func.func_code.co_varnames]
+# from paste.deploy.converters
+def asbool(obj):
+ if isinstance(obj, (str, unicode)):
+ obj = obj.strip().lower()
+ if obj in ['true', 'yes', 'on', 'y', 't', '1']:
+ return True
+ elif obj in ['false', 'no', 'off', 'n', 'f', '0']:
+ return False
+ else:
+ raise ValueError("String is not true/false: %r" % obj)
+ return bool(obj)
+
def coerce_kw_type(kw, key, type_, flexi_bool=True):
"""If 'key' is present in dict 'kw', coerce its value to type 'type_' if
necessary. If 'flexi_bool' is True, the string '0' is considered false
@@ -160,8 +172,8 @@ def coerce_kw_type(kw, key, type_, flexi_bool=True):
"""
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
+ if type_ is bool and flexi_bool:
+ kw[key] = asbool(kw[key])
else:
kw[key] = type_(kw[key])