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 asint(obj): |
---|
16 | try: |
---|
17 | return int(obj) |
---|
18 | except (TypeError, ValueError), e: |
---|
19 | raise ValueError( |
---|
20 | "Bad integer value: %r" % obj) |
---|
21 | |
---|
22 | def aslist(obj, sep=None, strip=True): |
---|
23 | if isinstance(obj, (str, unicode)): |
---|
24 | lst = obj.split(sep) |
---|
25 | if strip: |
---|
26 | lst = [v.strip() for v in lst] |
---|
27 | return lst |
---|
28 | elif isinstance(obj, (list, tuple)): |
---|
29 | return obj |
---|
30 | elif obj is None: |
---|
31 | return [] |
---|
32 | else: |
---|
33 | return [obj] |
---|