Mercurial > hg > pyloader
annotate pyloader/cast.py @ 6:886009132409
start a dict for type-based stringcasting
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Thu, 12 May 2011 08:23:10 -0700 |
parents | 5ce55f6c8964 |
children | 2261cf505daf |
rev | line source |
---|---|
2
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
1 class StringCastException(Exception): |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
2 """raised on unsucessful cast""" |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
3 |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
4 class StringCaster(object): |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
5 """cast strings to other things""" |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
6 # as yet unused |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
7 |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
8 def __init__(self, *casts): |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
9 self.casts = casts |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 def __call__(self, string): |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 for cast in self.casts: |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 try: |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 return cast(string) |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 except: |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 continue |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
17 return string |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
18 |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
19 ### casters |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
20 |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
21 def str2bool(string): |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
22 mapping = {'true': True, |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
23 'True': True, |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
24 'false': False, |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
25 'False': False} |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
26 return mapping[string] |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
27 |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
28 def str2list(string, separator=None): |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
29 string = string.rstrip(separator) |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
30 return [i.strip() for i in string.split(separator)] |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
31 |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
32 def str2args(string, separator=None): |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
33 args = str2list(string, separator) |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
34 _args = [] |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
35 kw = {} |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
36 for arg in args: |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
37 if '=' in arg: |
4
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
38 key, value = arg.split('=', 1) |
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
39 key = key.strip() |
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
40 value = value.strip() |
5ce55f6c8964
invokation works in the barest sense
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
41 kw[key] = value |
2
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
42 else: |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
43 _args.append(arg) |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
44 return _args, kw |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
45 |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
46 # convenience functions |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
47 casts = [int, float, str2bool, str2list, str2args] |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
48 cast = StringCaster(*casts) |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
49 |
6
886009132409
start a dict for type-based stringcasting
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
50 # cast by type |
886009132409
start a dict for type-based stringcasting
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
51 cast_dict = dict([(i,i) for i in int, float]) |
886009132409
start a dict for type-based stringcasting
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
52 cast_dict[bool] = str2bool |
886009132409
start a dict for type-based stringcasting
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
53 cast_dict[tuple] = cast_dict[list] = str2list |
886009132409
start a dict for type-based stringcasting
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
54 |
2
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
55 if __name__ == '__main__': |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
56 import sys |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
57 for arg in sys.argv[1:]: |
6cb7696edc4d
add some more stuff for string loady type things and rearrange structure
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
58 print cast(arg) |