Lines Matching refs:value

94 def assertStr(value):
95 if not isinstance(value, str):
96 raise TypeError("wrong type '%s' should be str" % type(value))
100 def toNum(value, target_type, units, prefixes, converter):
101 assertStr(value)
108 "cannot convert '%s' to %s" % (value, target_type))
110 if units and not value.endswith(units):
113 return convert(value)
115 value = value[:-len(units)]
117 prefix = next((p for p in prefixes.keys() if value.endswith(p)), None)
119 return convert(value)
120 value = value[:-len(prefix)]
122 return convert(value) * prefixes[prefix]
124 def toFloat(value, target_type='float', units=None, prefixes=[]):
125 return toNum(value, target_type, units, prefixes, float)
127 def toMetricFloat(value, target_type='float', units=None):
128 return toFloat(value, target_type, units, metric_prefixes)
130 def toBinaryFloat(value, target_type='float', units=None):
131 return toFloat(value, target_type, units, binary_prefixes)
133 def toInteger(value, target_type='integer', units=None, prefixes=[]):
135 return toNum(value, target_type, units, prefixes, intifier)
137 def toMetricInteger(value, target_type='integer', units=None):
138 return toInteger(value, target_type, units, metric_prefixes)
140 def toBinaryInteger(value, target_type='integer', units=None):
141 return toInteger(value, target_type, units, binary_prefixes)
143 def toBool(value):
144 assertStr(value)
146 value = value.lower()
147 if value in ('true', 't', 'yes', 'y', '1'):
149 if value in ('false', 'f', 'no', 'n', '0'):
153 def toFrequency(value):
154 return toMetricFloat(value, 'frequency', 'Hz')
156 def toLatency(value):
157 return toMetricFloat(value, 'latency', 's')
159 def anyToLatency(value):
162 return 1 / toFrequency(value)
167 return toLatency(value)
171 raise ValueError("cannot convert '%s' to clock period" % value)
173 def anyToFrequency(value):
176 return toFrequency(value)
181 return 1 / toLatency(value)
185 raise ValueError("cannot convert '%s' to clock period" % value)
187 def toNetworkBandwidth(value):
188 return toMetricFloat(value, 'network bandwidth', 'bps')
190 def toMemoryBandwidth(value):
191 return toBinaryFloat(value, 'memory bandwidth', 'B/s')
193 def toMemorySize(value):
194 return toBinaryInteger(value, 'memory size', 'B')
196 def toIpAddress(value):
197 if not isinstance(value, str):
198 raise TypeError("wrong type '%s' should be str" % type(value))
200 bytes = value.split('.')
202 raise ValueError('invalid ip address %s' % value)
206 raise ValueError('invalid ip address %s' % value)
211 def toIpNetmask(value):
212 if not isinstance(value, str):
213 raise TypeError("wrong type '%s' should be str" % type(value))
215 (ip, netmask) = value.split('/')
235 def toIpWithPort(value):
236 if not isinstance(value, str):
237 raise TypeError("wrong type '%s' should be str" % type(value))
239 (ip, port) = value.split(':')
245 def toVoltage(value):
246 return toMetricFloat(value, 'voltage', 'V')
248 def toCurrent(value):
249 return toMetricFloat(value, 'current', 'A')
251 def toEnergy(value):
252 return toMetricFloat(value, 'energy', 'J')