219 if not (from_query
and to_query):
222 measured = re.match(RE_MEASURE, from_query, re.VERBOSE)
223 if not (measured
and measured.group(
'number'), measured.group(
'unit')):
232 source_list, target_list = [], []
234 for symbol, si_name, from_si, to_si, orig_symbol
in symbol_to_si():
236 if symbol == measured.group(
'unit'):
237 source_list.append((si_name, to_si))
238 if symbol == to_query:
239 target_list.append((si_name, from_si, orig_symbol))
241 if not (source_list
and target_list):
244 source_to_si = target_from_si = target_symbol =
None
248 for source
in source_list:
249 for target
in target_list:
250 if source[0] == target[0]:
251 source_to_si = source[1]
252 target_from_si = target[1]
253 target_symbol = target[2]
255 if not (source_to_si
and target_from_si):
258 _locale = get_locale()
or 'en_US'
260 value = measured.group(
'sign') + measured.group(
'number') + (measured.group(
'E')
or '')
261 value = babel.numbers.parse_decimal(value, locale=_locale)
265 if isinstance(source_to_si, (float, int)):
266 value = float(value) * source_to_si
268 value = source_to_si(float(value))
272 if isinstance(target_from_si, (float, int)):
273 value = float(value) * target_from_si
275 value = target_from_si(float(value))
277 if measured.group(
'E'):
279 result = babel.numbers.format_scientific(value, locale=_locale)
281 result = babel.numbers.format_decimal(value, locale=_locale, format=
'#,##0.##########;-#')
283 return f
'{result} {target_symbol}'