54 if isinstance(node, ast.Constant)
and isinstance(node.value, (int, float)):
57 if isinstance(node, ast.BinOp):
58 return operators[type(node.op)](
_eval(node.left),
_eval(node.right))
60 if isinstance(node, ast.UnaryOp):
61 return operators[type(node.op)](
_eval(node.operand))
68 def handler(q: Queue, func, args, **kwargs):
70 q.put(func(*args, **kwargs))
76 p = Process(target=handler, args=(que, func, args), kwargs=kwargs)
78 p.join(timeout=timeout)
83 logger.debug(
"terminate function after timeout is exceeded")
93 if search.search_query.pageno > 1:
96 query = search.search_query.query
102 query = query.replace(
"x",
"*").replace(
":",
"/")
105 ui_locale = babel.Locale.parse(flask.request.preferences.get_value(
'locale'), sep=
'-')
108 def _decimal(match: re.Match) -> str:
109 val = match.string[match.start() : match.end()]
110 val = babel.numbers.parse_decimal(val, ui_locale, numbering_system=
"latn")
113 decimal = ui_locale.number_symbols[
"latn"][
"decimal"]
114 group = ui_locale.number_symbols[
"latn"][
"group"]
115 query = re.sub(f
"[0-9]+[{decimal}|{group}][0-9]+[{decimal}|{group}]?[0-9]?", _decimal, query)
118 if any(str.isalpha(c)
for c
in query):
122 query_py_formatted = query.replace(
"^",
"**")
125 result =
timeout_func(0.05, _eval_expr, query_py_formatted)
126 if result
is None or result ==
"":
128 result = babel.numbers.format_decimal(result, locale=ui_locale)
129 search.result_container.answers[
'calculate'] = {
'answer': f
"{search.search_query.query} = {result}"}