Giter VIP home page Giter VIP logo

Comments (4)

newville avatar newville commented on May 31, 2024

@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.

jedie avatar jedie commented on May 31, 2024

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.

newville avatar newville commented on May 31, 2024

@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.

newville avatar newville commented on May 31, 2024

version 0.9.9 now supports turning off many parts of the interpreter, including if, for, while, try, functiondefs.

from asteval.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.