.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.plugins.calculator Namespace Reference

Classes

class  SXNGPlugin

Functions

int _compare (list[ast.cmpop] ops, list[int|float] values)
 _eval_expr (expr)
 _eval (node)
 handler (multiprocessing.Queue q, func, args, **kwargs)

Variables

dict operators
dict math_constants
 mp_fork = multiprocessing.get_context("fork")

Detailed Description

Calculate mathematical expressions using :py:obj:`ast.parse` (mode="eval").

Function Documentation

◆ _compare()

int searx.plugins.calculator._compare ( list[ast.cmpop] ops,
list[int | float] values )
protected
2 < 3 becomes ops=[ast.Lt] and values=[2,3]
2 < 3 <= 4 becomes ops=[ast.Lt, ast.LtE] and values=[2,3, 4]

Definition at line 121 of file calculator.py.

121def _compare(ops: list[ast.cmpop], values: list[int | float]) -> int:
122 """
123 2 < 3 becomes ops=[ast.Lt] and values=[2,3]
124 2 < 3 <= 4 becomes ops=[ast.Lt, ast.LtE] and values=[2,3, 4]
125 """
126 for op, a, b in zip(ops, values, values[1:]): # pylint: disable=invalid-name
127 if isinstance(op, ast.Eq) and a == b:
128 continue
129 if isinstance(op, ast.NotEq) and a != b:
130 continue
131 if isinstance(op, ast.Lt) and a < b:
132 continue
133 if isinstance(op, ast.LtE) and a <= b:
134 continue
135 if isinstance(op, ast.Gt) and a > b:
136 continue
137 if isinstance(op, ast.GtE) and a >= b:
138 continue
139
140 # Ignore impossible ops:
141 # * ast.Is
142 # * ast.IsNot
143 # * ast.In
144 # * ast.NotIn
145
146 # the result is False for a and b and operation op
147 return 0
148 # the results for all the ops are True
149 return 1
150
151

Referenced by _eval().

Here is the caller graph for this function:

◆ _eval()

searx.plugins.calculator._eval ( node)
protected

Definition at line 210 of file calculator.py.

210def _eval(node):
211 if isinstance(node, ast.Constant) and isinstance(node.value, (int, float)):
212 return node.value
213
214 if isinstance(node, ast.BinOp):
215 return operators[type(node.op)](_eval(node.left), _eval(node.right))
216
217 if isinstance(node, ast.UnaryOp):
218 return operators[type(node.op)](_eval(node.operand))
219
220 if isinstance(node, ast.Compare):
221 return _compare(node.ops, [_eval(node.left)] + [_eval(c) for c in node.comparators])
222
223 if isinstance(node, ast.Name) and node.id in math_constants:
224 return math_constants[node.id]
225
226 raise TypeError(node)
227
228

References _compare(), and _eval().

Referenced by _eval(), and _eval_expr().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _eval_expr()

searx.plugins.calculator._eval_expr ( expr)
protected
Evaluates the given textual expression.

Returns a tuple of (numericResult, isBooleanResult).

>>> _eval_expr('2^6')
64, False
>>> _eval_expr('2**6')
64, False
>>> _eval_expr('1 + 2*3**(4^5) / (6 + -7)')
-5.0, False
>>> _eval_expr('1 < 3')
1, True
>>> _eval_expr('5 < 3')
0, True
>>> _eval_expr('17 == 11+1+5 == 7+5+5')
1, True

Definition at line 182 of file calculator.py.

182def _eval_expr(expr):
183 """
184 Evaluates the given textual expression.
185
186 Returns a tuple of (numericResult, isBooleanResult).
187
188 >>> _eval_expr('2^6')
189 64, False
190 >>> _eval_expr('2**6')
191 64, False
192 >>> _eval_expr('1 + 2*3**(4^5) / (6 + -7)')
193 -5.0, False
194 >>> _eval_expr('1 < 3')
195 1, True
196 >>> _eval_expr('5 < 3')
197 0, True
198 >>> _eval_expr('17 == 11+1+5 == 7+5+5')
199 1, True
200 """
201 try:
202 root_expr = ast.parse(expr, mode='eval').body
203 return _eval(root_expr), isinstance(root_expr, ast.Compare)
204
205 except (SyntaxError, TypeError, ZeroDivisionError):
206 # Expression that can't be evaluated (i.e. not a math expression)
207 return "", False
208
209

References _eval().

Here is the call graph for this function:

◆ handler()

searx.plugins.calculator.handler ( multiprocessing.Queue q,
func,
args,
** kwargs )

Definition at line 229 of file calculator.py.

229def handler(q: multiprocessing.Queue, func, args, **kwargs): # pylint:disable=invalid-name
230 try:
231 q.put(func(*args, **kwargs))
232 except:
233 q.put(None)
234 raise

Variable Documentation

◆ math_constants

dict searx.plugins.calculator.math_constants
Initial value:
1= {
2 'e': math.e,
3 'pi': math.pi,
4}

Definition at line 169 of file calculator.py.

◆ mp_fork

searx.plugins.calculator.mp_fork = multiprocessing.get_context("fork")

Definition at line 179 of file calculator.py.

◆ operators

dict searx.plugins.calculator.operators
Initial value:
1= {
2 ast.Add: operator.add,
3 ast.Sub: operator.sub,
4 ast.Mult: operator.mul,
5 ast.Div: operator.truediv,
6 ast.Pow: operator.pow,
7 ast.BitXor: operator.xor,
8 ast.BitOr: operator.or_,
9 ast.BitAnd: operator.and_,
10 ast.USub: operator.neg,
11 ast.RShift: operator.rshift,
12 ast.LShift: operator.lshift,
13 ast.Mod: operator.mod,
14 ast.Compare: _compare,
15}

Definition at line 152 of file calculator.py.