Code Monkey home page Code Monkey logo

Comments (5)

patricksurry avatar patricksurry commented on June 18, 2024

I guess this is another example, trying to evaluate something like {**a, **b}:

>>> names = dict(a=dict(x=1, y=2), b=dict(z=3))
>>> EvalWithCompoundTypes(names=names).eval('{**a, **b}')
Traceback (most recent call last):
  File "/Users/psurry/Downloads/simpleeval.py", line 395, in _eval
    handler = self.nodes[type(node)]
KeyError: <class 'NoneType'>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/psurry/Downloads/simpleeval.py", line 616, in eval
    return super(EvalWithCompoundTypes, self).eval(expr)
  File "/Users/psurry/Downloads/simpleeval.py", line 389, in eval
    return self._eval(ast.parse(expr.strip()).body[0])
  File "/Users/psurry/Downloads/simpleeval.py", line 401, in _eval
    return handler(node)
  File "/Users/psurry/Downloads/simpleeval.py", line 404, in _eval_expr
    return self._eval(node.value)
  File "/Users/psurry/Downloads/simpleeval.py", line 401, in _eval
    return handler(node)
  File "/Users/psurry/Downloads/simpleeval.py", line 619, in _eval_dict
    return {self._eval(k): self._eval(v) for (k, v) in zip(node.keys, node.values)}
  File "/Users/psurry/Downloads/simpleeval.py", line 619, in <dictcomp>
    return {self._eval(k): self._eval(v) for (k, v) in zip(node.keys, node.values)}
  File "/Users/psurry/Downloads/simpleeval.py", line 398, in _eval
    "Sorry, {0} is not available in this " "evaluator".format(type(node).__name__)
simpleeval.FeatureNotAvailable: Sorry, NoneType is not available in this evaluator
>>> 

from simpleeval.

patricksurry avatar patricksurry commented on June 18, 2024

I used a workaround by evaluating something like this dict(list(a.items()) + list(b.items())). Looking at the AST parse:

>>> ast.dump(ast.parse('{**a}'))
"Module(body=[Expr(value=Dict(keys=[None], values=[Name(id='a', ctx=Load())]))])"

I guess it's not handling the keys=[None] piece? Adding an explicit key doesn't help:

>>> ast.dump(ast.parse('{"a": 1, **a}'))
"Module(body=[Expr(value=Dict(keys=[Str(s='a'), None], values=[Num(n=1), Name(id='a', ctx=Load())]))])"
>>> 

I guess this would be fixed by either tweaking _eval_dict to handle injection of a dictionary in v when k was None? See the ast docs where it explains: "When doing dictionary unpacking using dictionary literals the expression to be expanded goes in the values list, with a None at the corresponding position in keys."

Perhaps this is a different bug that the originally reported one?

from simpleeval.

danthedeckie avatar danthedeckie commented on June 18, 2024

This would be really cool to add

from simpleeval.

amyasnikov avatar amyasnikov commented on June 18, 2024

+1 for this feature request. DictComp is really a missing thing for this cool library.
@Reskov could you prepare a PR? As I see in the previous comment @danthedeckie is ready to accept it (please correct me if I'm wrong)

from simpleeval.

Reskov avatar Reskov commented on June 18, 2024

@amyasnikov Not sure. Personally, I am not interested in this anymore. 😞 And there was already PR #62.

from simpleeval.

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.