Why would `eval('dict('+s+')')` work but not `literal_eval('dict('+s+')')`?

I could do this with eval(): >>> s = ‘hello=True,world="foobar"’ >>> eval(‘dict(‘+s+’)’) {‘hello’: True, ‘world’: ‘foobar’} but with literal_eval(): >>> from ast import literal_eval >>> literal_eval(‘dict(‘+s+’)’) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/Cellar/python@3.11/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/ast.py", line 110, in literal_eval return _convert(node_or_string) ^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/Cellar/python@3.11/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/ast.py", line 109, in _convert return _convert_signed_num(node) ^^^^^^^^^^^^^^^^^^^^^^^^^… Read More Why would `eval('dict('+s+')')` work but not `literal_eval('dict('+s+')')`?

How do I fix subprocess:-exited-with-error whenever I pip install?

Whenever I try pip install ast. I keep receiving this error code: Downloading AST-0.0.2.tar.gz (19 kB) Installing build dependencies … done Getting requirements to build wheel … error error: subprocess-exited-with-error note: This error originates from a subprocess, and is likely not a problem with pip. error: subprocess-exited-with-error × Getting requirements to build wheel did not… Read More How do I fix subprocess:-exited-with-error whenever I pip install?

Pandas covert nan values to None in string list before literal_eval and convert back to np.nan

I have a dataframe with a few series that contain lists of floats that includes nan values. Eg. s[0] = ‘[1.21, 1.21, nan, nan, 100]’ These strings I want to convert to lists using literal_eval. When I try I get the error ValueError: malformed node or string on line 1: because as per the docs,… Read More Pandas covert nan values to None in string list before literal_eval and convert back to np.nan

How to I convert a string that contains a list of sets to list without changing the order of the sets?

I have a string that contains a list of sets – ‘[{13,18},{14,19}]’ I want it to be like this – [‘[13,18]’,'[14,19]’] When I use ast.literal_eval() the order of the sets gets changed – >>> >>> l1='[{13,18},{14,19}]’ >>> >>> >>> ast.literal_eval(l1) [{18, 13}, {19, 14}] >>> Please suggest how can I keep the order of the… Read More How to I convert a string that contains a list of sets to list without changing the order of the sets?

Python ast Libary – retreive value of Node inside a function call

Follow up question of here: Python ast Libary – how to retreive value of a specific node I use the following code to retreive {‘console_scripts’: [‘main=smamesdemo.run.main:main’]} import ast code = ”’extras_require={"dev": dev_reqs} x = 3 entry_points={ "console_scripts": ["main=smamesdemo.run.main:main"] }”’ for node in ast.walk(ast.parse(code)): if isinstance(node, ast.Assign) and node.targets[0].id == ‘entry_points’: expr = ast.Expression(body=node.value) ast.fix_missing_locations(expr) entry_points… Read More Python ast Libary – retreive value of Node inside a function call

How to convert this tree structure into a JS MemberExpression tree structure?

I have figured out a way to represent the expression a.b[c.d][e].f[g[h[i.j]]] using my own tree format. That expression, represented as a tree, looks like this: { "form": "nest", "link": [ { "form": "site", "name": "a" }, { "form": "site", "name": "b" }, { "form": "nest", "link": [ { "form": "site", "name": "c" }, { "form":… Read More How to convert this tree structure into a JS MemberExpression tree structure?