convert.py revision 1519
1# metric prefixes
2exa  = 1.0e18
3peta = 1.0e15
4tera = 1.0e12
5giga = 1.0e9
6mega = 1.0e6
7kilo = 1.0e3
8
9milli = 1.0e-3
10micro = 1.0e-6
11nano  = 1.0e-9
12pico  = 1.0e-12
13femto = 1.0e-15
14atto  = 1.0e-18
15
16# power of 2 prefixes
17kibi = 1024
18mebi = kibi * 1024
19gibi = mebi * 1024
20tebi = gibi * 1024
21pebi = tebi * 1024
22exbi = pebi * 1024
23
24# memory size configuration stuff
25def to_integer(value):
26    if not isinstance(value, str):
27        result = int(value)
28    elif value.endswith('Ei'):
29        result = int(value[:-2]) * exbi
30    elif value.endswith('Pi'):
31        result = int(value[:-2]) * pebi
32    elif value.endswith('Ti'):
33        result = int(value[:-2]) * tebi
34    elif value.endswith('Gi'):
35        result = int(value[:-2]) * gibi
36    elif value.endswith('Mi'):
37        result = int(value[:-2]) * mebi
38    elif value.endswith('ki'):
39        result = int(value[:-2]) * kibi
40    elif value.endswith('E'):
41        result = int(value[:-1]) * exa
42    elif value.endswith('P'):
43        result = int(value[:-1]) * peta
44    elif value.endswith('T'):
45        result = int(value[:-1]) * tera
46    elif value.endswith('G'):
47        result = int(value[:-1]) * giga
48    elif value.endswith('M'):
49        result = int(value[:-1]) * mega
50    elif value.endswith('k'):
51        result = int(value[:-1]) * kilo
52    elif value.endswith('m'):
53        result = int(value[:-1]) * milli
54    elif value.endswith('u'):
55        result = int(value[:-1]) * micro
56    elif value.endswith('n'):
57        result = int(value[:-1]) * nano
58    elif value.endswith('p'):
59        result = int(value[:-1]) * pico
60    elif value.endswith('f'):
61        result = int(value[:-1]) * femto
62    else:
63        result = int(value)
64
65    return result
66
67def to_bool(val):
68    t = type(val)
69    if t == bool:
70        return val
71
72    if t == None:
73        return False
74
75    if t == int or t == long:
76        return bool(val)
77
78    if t == str:
79        val = val.lower()
80        if val == "true" or val == "t" or val == "yes" or val == "y":
81            return True
82        elif val == "false" or val == "f" or val == "no" or val == "n":
83            return False
84
85    return to_integer(val) != 0
86
87def to_frequency(value):
88    if not isinstance(value, str):
89        result = float(value)
90    elif value.endswith('THz'):
91        result = float(value[:-3]) * tera
92    elif value.endswith('GHz'):
93        result = float(value[:-3]) * giga
94    elif value.endswith('MHz'):
95        result = float(value[:-3]) * mega
96    elif value.endswith('kHz'):
97        result = float(value[:-3]) * kilo
98    elif value.endswith('Hz'):
99        result = float(value[:-2])
100    else:
101        result = float(value)
102
103    return result
104
105def to_latency(value):
106    if not isinstance(value, str):
107        result = float(value)
108    elif value.endswith('c'):
109        result = float(value[:-1])
110    elif value.endswith('ps'):
111        result = float(value[:-2]) * pico
112    elif value.endswith('ns'):
113        result = float(value[:-2]) * nano
114    elif value.endswith('us'):
115        result = float(value[:-2]) * micro
116    elif value.endswith('ms'):
117        result = float(value[:-2]) * milli
118    elif value.endswith('s'):
119        result = float(value[:-1])
120    else:
121        result = float(value)
122
123    return result;
124
125def to_network_bandwidth(value):
126    if not isinstance(value, str):
127        result = float(value)
128    elif value.endswith('Tbps'):
129        result = float(value[:-3]) * tera
130    elif value.endswith('Gbps'):
131        result = float(value[:-3]) * giga
132    elif value.endswith('Mbps'):
133        result = float(value[:-3]) * mega
134    elif value.endswith('kbps'):
135        result = float(value[:-3]) * kilo
136    elif value.endswith('bps'):
137        result = float(value[:-2])
138    else:
139        result = float(value)
140
141    return result
142
143def to_memory_bandwidth(value):
144    if not isinstance(value, str):
145        result = int(value)
146    elif value.endswith('PB/s'):
147        result = int(value[:-4]) * pebi
148    elif value.endswith('TB/s'):
149        result = int(value[:-4]) * tebi
150    elif value.endswith('GB/s'):
151        result = int(value[:-4]) * gibi
152    elif value.endswith('MB/s'):
153        result = int(value[:-4]) * mebi
154    elif value.endswith('kB/s'):
155        result = int(value[:-4]) * kibi
156    elif value.endswith('B/s'):
157        result = int(value[:-3])
158    else:
159        result = int(value)
160
161    return result
162
163def to_memory_size(value):
164    if not isinstance(value, str):
165        result = int(value)
166    elif value.endswith('PB'):
167        result = int(value[:-2]) * pebi
168    elif value.endswith('TB'):
169        result = int(value[:-2]) * tebi
170    elif value.endswith('GB'):
171        result = int(value[:-2]) * gibi
172    elif value.endswith('MB'):
173        result = int(value[:-2]) * mebi
174    elif value.endswith('kB'):
175        result = int(value[:-2]) * kibi
176    elif value.endswith('B'):
177        result = int(value[:-1])
178    else:
179        result = int(value)
180
181    return result
182