Use asteval for Table.eval #156
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is an attempt to address #135 using the asteval package, which is safer than the built in
eval. This disables certain features of the language using essentially a white list (see here).The way this is not written is for
Table.evalto take an extra argumentlibrary(similar toview_as). The table will then beview_ased appropriately for this library, and the fields will be fed into the interpreter. In addition, modules needed for using that library are fed in (e.g.,akif library is"ak").I also added an argument
as_lgdoto control the return type, so you can return as thelibraryyou provide or as anLGDOobject (defaults toTruesince this is the current behavior).I left in the
parametersargument to feed in external values, but removed themodulesargument; the user actually can still feed modules asparameters, but I'm not so sure we want that (at some pointTable.evalisn't the right tool for the job...).The libraries included here are (note
numpyis automatically included):lgdo: includes the Table as LGDO objects and includeslgdo.typesmodulepd: callspandas.DataFrame.evalnumexpr: similar to before; usesflattento use__for nested objects, and then includes thendas for each field (excludingVectorOfVectorsfields)ak: includes fields asakobjects, and includes theakmodulenp: will throw an Error becauseview_as('np')is not implementedImplementing each of these was extremely simple with
asteval, and extending this system further would be fairly trivial!Right now, this PR is not ready to merge; I have submitted it for the sake of discussion. In particular, this adds a new dependency in
asteval, and it works a little differently from the old implementation. It should be possible to make the default behavior for no provided library act like before (basicallyakif needed andnumexprotherwise). In addition, I have to still make sure the tests are appropriate.