178 if not (from_query
and to_query):
181 measured = re.match(RE_MEASURE, from_query, re.VERBOSE)
182 if not (measured
and measured.group(
'number'), measured.group(
'unit')):
191 source_list, target_list = [], []
193 for symbol, si_name, from_si, to_si, orig_symbol
in symbol_to_si():
195 if symbol == measured.group(
'unit'):
196 source_list.append((si_name, to_si))
197 if symbol == to_query:
198 target_list.append((si_name, from_si, orig_symbol))
200 if not (source_list
and target_list):
203 source_to_si = target_from_si = target_symbol =
None
207 for source
in source_list:
208 for target
in target_list:
209 if source[0] == target[0]:
210 source_to_si = source[1]
211 target_from_si = target[1]
212 target_symbol = target[2]
214 if not (source_to_si
and target_from_si):
217 _locale = get_locale()
or 'en_US'
219 value = measured.group(
'sign') + measured.group(
'number') + (measured.group(
'E')
or '')
220 value = babel.numbers.parse_decimal(value, locale=_locale)
224 if isinstance(source_to_si, (float, int)):
225 value = float(value) * source_to_si
227 value = source_to_si(float(value))
231 if isinstance(target_from_si, (float, int)):
232 value = float(value) * target_from_si
234 value = target_from_si(float(value))
236 if measured.group(
'E'):
238 result = babel.numbers.format_scientific(value, locale=_locale)
240 result = babel.numbers.format_decimal(value, locale=_locale, format=
'#,##0.##########;-#')
242 search.result_container.answers[
'conversion'] = {
'answer': f
'{result} {target_symbol}'}
247 if search.search_query.pageno > 1:
250 query = search.search_query.query
251 query_parts = query.split(
" ")
253 if len(query_parts) < 3:
256 for query_part
in query_parts:
257 for keyword
in CONVERT_KEYWORDS:
258 if query_part == keyword:
259 from_query, to_query = query.split(keyword, 1)