[3] | 1 | # (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org) |
---|
| 2 | # Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php |
---|
| 3 | def asbool(obj): |
---|
| 4 | if isinstance(obj, (str, unicode)): |
---|
| 5 | obj = obj.strip().lower() |
---|
| 6 | if obj in ['true', 'yes', 'on', 'y', 't', '1']: |
---|
| 7 | return True |
---|
| 8 | elif obj in ['false', 'no', 'off', 'n', 'f', '0']: |
---|
| 9 | return False |
---|
| 10 | else: |
---|
| 11 | raise ValueError( |
---|
| 12 | "String is not true/false: %r" % obj) |
---|
| 13 | return bool(obj) |
---|
| 14 | |
---|
| 15 | def aslist(obj, sep=None, strip=True): |
---|
| 16 | if isinstance(obj, (str, unicode)): |
---|
| 17 | lst = obj.split(sep) |
---|
| 18 | if strip: |
---|
| 19 | lst = [v.strip() for v in lst] |
---|
| 20 | return lst |
---|
| 21 | elif isinstance(obj, (list, tuple)): |
---|
| 22 | return obj |
---|
| 23 | elif obj is None: |
---|
| 24 | return [] |
---|
| 25 | else: |
---|
| 26 | return [obj] |
---|