.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 120 of file calculator.py.

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

Referenced by _eval().

Here is the caller graph for this function:

◆ _eval()

searx.plugins.calculator._eval ( node)
protected

Definition at line 209 of file calculator.py.

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

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 181 of file calculator.py.

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

References _eval().

Here is the call graph for this function:

◆ handler()

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

Definition at line 228 of file calculator.py.

228def handler(q: multiprocessing.Queue, func, args, **kwargs): # pylint:disable=invalid-name
229 try:
230 q.put(func(*args, **kwargs))
231 except:
232 q.put(None)
233 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 168 of file calculator.py.

◆ mp_fork

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

Definition at line 178 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 151 of file calculator.py.