Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide 'how to upgrade' instructions, please? #5

Closed
luciansmith opened this issue Jun 21, 2023 · 4 comments
Closed

Provide 'how to upgrade' instructions, please? #5

luciansmith opened this issue Jun 21, 2023 · 4 comments

Comments

@luciansmith
Copy link

I'm trying to maintain legacy code, which literally just broke because evalidate.evalidate is now gone. I don't know the code nearly well enough to tell how to update it; some sort of 'if you did X, do Y now' instructions would be fantastic.

Specifically, the code in question looks like:

    math_node = evalidate.evalidate(math,
                                    addnodes=VALID_MATH_EXPRESSION_NODES,
                                    funcs=MATHEMATICAL_FUNCTIONS.keys())
    compiled_math = compile(math_node, '<math>', 'eval')
    return compiled_math

Any hints appreciated!

@yaroslaff
Copy link
Owner

yaroslaff commented Jun 21, 2023

Easiest workaround is just to lock evalidate version to any before 2.0.0 (which I published just few hours ago), e.g. v1.1.0 has evalidate function.

But better to upgrade it:

#!/usr/bin/env python3

import evalidate

VALID_MATH_EXPRESSION_NODES = ['Call', 'Mult']
MATHEMATICAL_FUNCTIONS = ['int']

def pi():
    return 3.14 

model = evalidate.base_eval_model.clone()
model.nodes.extend(VALID_MATH_EXPRESSION_NODES)
model.allowed_functions.extend(MATHEMATICAL_FUNCTIONS)
model.imported_functions = dict(pi=pi)


def prepare(math):
    expr = evalidate.Expr(math, model=model)
    return expr.code

code = prepare('int(a) * 3 + pi() + 100')

# 139.14
print(eval(code, None, dict(a=12.34)))

@yaroslaff yaroslaff pinned this issue Jun 21, 2023
luciansmith added a commit to biosimulators/Biosimulators_utils that referenced this issue Jun 21, 2023
@luciansmith
Copy link
Author

Thank you! That was enough to figure it out, though I did have to change 'expr.code' to 'expr.node' to match the previous functionality. For future explorers:

    math_node = evalidate.evalidate(math,
                                    addnodes=VALID_MATH_EXPRESSION_NODES,
                                    funcs=MATHEMATICAL_FUNCTIONS.keys())

becomes:

    model = evalidate.base_eval_model.clone()
    model.nodes.extend(VALID_MATH_EXPRESSION_NODES)
    model.allowed_functions.extend(MATHEMATICAL_FUNCTIONS.keys())

    math_node = evalidate.Expr(math, model=model).node

If something like this could be added to the docs (for this and other functionality), I think that would help anyone else in the same boat.

@yaroslaff
Copy link
Owner

Attribute node is parsed AST tree, but not an python byte-code. Do you really need it (AST-tree)? If you need to eval(), then Expr.code is byte-code which would be evaluated very quickly (no need to call compile() to convert AST node to bytecode). Using code attribute can speed-up execution greatly (as demonstrated in benchmarks).

@luciansmith
Copy link
Author

It's possible the downstream code could also be changed, but since 'evalidate' retuned a node, that's what the code expects--using 'code' gave me an error it took some time to track down. This is not time-sensitive code; the time it takes me as a programmer to update stuff is far far longer than the time spent executing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants