Revision 1046
Added by Aaron Marcuse-Kubitza almost 13 years ago
util.py | ||
---|---|---|
22 | 22 |
+check_func.__name__) |
23 | 23 |
|
24 | 24 |
def cast(type_, val): |
25 |
'''Passes None through''' |
|
26 |
if val != None: val = type_(val) |
|
25 |
'''Passes None through. Does not cast a subclass to a superclass (which |
|
26 |
doesn't make sense in a dynamically-typed language).''' |
|
27 |
if val != None and not isinstance(val, type_): val = type_(val) |
|
27 | 28 |
return val |
28 | 29 |
|
29 | 30 |
def is_str(val): return isinstance(val, basestring) |
30 | 31 |
|
31 | 32 |
def is_list(val): return isinstance(val, list) |
32 | 33 |
|
34 |
#### Basic types |
|
35 |
|
|
36 |
def none_if(val, none_val): |
|
37 |
if cast(type(none_val), val) == none_val: return None |
|
38 |
else: return val |
|
39 |
|
|
33 | 40 |
#### Iterables |
34 | 41 |
|
35 | 42 |
def first(iter_): return iter_.next() |
Also available in: Unified diff
util.py: Fixed cast() to not cast a subclass to a superclass (which doesn't make sense in a dynamically-typed language). Added none_if().