Comments (4)
@jedie That's an interesting suggestion. BTW, you can pass use_numpy=False
to asteval.Interpreter()
to turn off using the numpy functions.
But for the larger question, asteval.Interpreter
currently uses the class variable supported_nodes
to list the supported ast
nodes -- other nodes will raise NotImplementedError
. That could become an user-specified option. Right now that list has 41 node names (the order of the nodes is not important): https://github.com/newville/asteval/blob/master/asteval/asteval.py#L67
Some of these (name, expr, binop, call, etc) are probably required for almost anything to work, and some (assert, for, if, ifexp, while, functiondef) could definitely be optional for a minimal interpreter.
I wouldn't be opposed to making some of these optional, but I'm not sure the best way to expose that. Pass is a white-list of desired nodes? Pass in a black-list of undesired nodes? Add a pile of options like "with_if=True, with_for=False" to Interpreter? Any suggestions?
from asteval.
I have a solution made by myself here: https://github.com/jedie/django-dbpreferences/blob/82725edda09e10f3f9723066d8491c832ebd41ae/dbpreferences/tools/data_eval.py
Most parts are the same as ast.literal_eval()... But i need also True, False, None and datetime/timedelta...
I wouldn't be opposed to making some of these optional, but I'm not sure the best way to expose that.
Maybe a base class that does only the common types. Just like my solution...
from asteval.
@jedie I think the main question for having a base class would be what the "common types" are. Like, your solution supports Add and Sub but not any other binary operations. It also has special support for one very specialized type (datetime).
Those aren't the choices I would have made. That's OK, it's really just to point out how such needs quickly become specialized. if I were thinking of a "simple, common types", I would probably go for a 4-function (+, -, *, /) calculator with named variables, for floats, int, strings, lists, and tuples, and maybe slices for setting / getting members of lists and tuples. The way I would approach supporting functions from datetime would be to support calling functions and pre-load those named functions to the namespace.
Again, that's really just to say that there are multiple ways to think about the problem, making it unclear what the functionality of base class, say SimpleInterpreter
would be.
It might make sense to have an InterpreterConfig
class that had a pile of bools for each of the possibly-supported nodes, so that one could do something like
from asteval import Interpreter, InterpreterConfig
conf = InterpreterConfig()
conf.use_ifexp = False
conf.use_if = False
conf.use_for = False
conf.use_while = False
conf.use_functiondef = False
aeval = Interpreter(config=conf)
That's a little ugly, but I'm not sure how to do it better.
from asteval.
version 0.9.9 now supports turning off many parts of the interpreter, including if, for, while, try, functiondefs.
from asteval.
Related Issues (20)
- Supporting lambda expressions HOT 2
- Python 3.10: test_kaboom fails with IndexError HOT 4
- Import error with python 3.7 due to missing import.metadata HOT 5
- Backward incompatible change is not documented HOT 8
- Does not evaluate types like eval() HOT 6
- Returned value is repeated on next invocation of eval HOT 4
- asteval segfault HOT 5
- Missing dependency in Python 3.7 HOT 3
- Latest release (0.9.26) has importlib_metadata/importlib.metadata bug HOT 7
- Improve the propagation of Exception information (the bad name for a NameError) HOT 2
- List comprehension with multiple "for" loops results in NameError HOT 6
- max_time seems to be unsued HOT 2
- Support regex / glob modules HOT 2
- PyPI: also upload source tarball HOT 2
- power to a negative int HOT 4
- name 'str' is not defined HOT 1
- tests/test_asteval.py::TestEval::test_set_default_nodehandler fails if Python is built without Tkiner HOT 4
- ipmt throws deprecation warning in numpy 2.0.dev HOT 2
- The truth value of a Series is ambiguous HOT 3
- Variable unpacking does not work as expected HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from asteval.