numpy 1.22.4 Pypi GitHub Homepage
Other Docs
ParametersRaisesReturns
safe_eval(source)

Evaluate a string containing a Python literal expression without allowing the execution of arbitrary non-literal code.

Parameters

source : str

The string to evaluate.

Raises

SyntaxError

If the code has invalid Python syntax, or if it contains non-literal code.

Returns

obj : object

The result of evaluating source .

Protected string evaluation.

Examples

>>> np.safe_eval('1')
1
>>> np.safe_eval('[1, 2, 3]')
[1, 2, 3]
>>> np.safe_eval('{"foo": ("bar", 10.0)}')
{'foo': ('bar', 10.0)}
This example is valid syntax, but raise an exception at execution
>>> np.safe_eval('import os')
Traceback (most recent call last):
  ...
SyntaxError: invalid syntax
This example is valid syntax, but raise an exception at execution
>>> np.safe_eval('open("/home/user/.ssh/id_dsa").read()')
Traceback (most recent call last):
  ...
ValueError: malformed node or string: <_ast.Call object at 0x...>
See :

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /numpy/lib/utils.py#957
type: <class 'function'>
Commit: