asottile / astpretty Goto Github PK
View Code? Open in Web Editor NEWPretty print the output of python stdlib `ast.parse`.
License: MIT License
Pretty print the output of python stdlib `ast.parse`.
License: MIT License
That'd let me do:
echo "assert (x:=0), x" | astpretty -
Which is much better than:
echo "assert (x:=0), x + 1" | python3.8 -c "import ast; print(ast.dump(ast.parse(input()), c))"
Of course, I ended up doing the following, which works -- but isn't as nice as doing it in one step. :)
echo "assert (x:=0), x" > /tmp/assert.py
astpretty /tmp/assert.py
I want to take the output of astpretty and stuff it back again into a program (manually), i.e. I am using astpretty to produce the python code that then produces the python program :D.
To that end, I would rather like to avoid importing * from ast and simply import ast
.
With the current implementation of astpretty I need to add 'ast.' to all the ast
types like Subscript
, Name
etc.
The change to always print these out is trivial: there are two places where adding 'ast.' +
in fron of type(node).__name__
does that, in _leaf
(line 50) and the else path of pformat
(line 93), respectively.
It might be worth it to make this configurable maybe...? I don't think it should be the default for brevity reasons... prints of ASTs are verbose enough already as you know ;)
It would be good if astpretty supported AST's built with Guido's typed_ast which is a Python 3 package which parses both Python 2 and Python 3 and produce the standard Python AST.
There is a hack I came up with which works, but perhaps there should be a more elegant official way in the future.
# import typed_ast.ast27 as ast
import typed_ast.ast3 as ast
import astpretty
astpretty.ast = ast # monkey patch the ast away from built in python to use typed_ast's instead
Like json.dumps
or ast.dump
, making indent have an option to be an integer would be nice.
I could probably make a PR for this, if the idea gets approval.
Hi there,
Thanks for creating and maintaining this useful library!
I recently ran into this error
File "/Users/yifanwu/miniforge3/lib/python3.9/site-packages/astpretty.py", line 115, in pformat
attr = getattr(node, field)
AttributeError: 'Import' object has no attribute 'lineno'
And I think it would be nice not to make optional entries like lineno
a requirement. From a cursory glance this can be either hard coded (all the context info around line numbers) or blanket caught in a try/catch statement.
What do you think?
import ast, astpretty
o = ast.parse('2 + 3')
astpretty.pprint(o, indent=True)
Output:
Module(
body=[
Expr(
lineno=1,
col_offset=0,
end_lineno=1,
end_col_offset=5,
value=BinOp(
lineno=1,
col_offset=0,
end_lineno=1,
end_col_offset=5,
left=Constant(lineno=1, col_offset=0, end_lineno=1, end_col_offset=1, value=2, kind=None),
op=Add(),
right=Constant(lineno=1, col_offset=4, end_lineno=1, end_col_offset=5, value=3, kind=None),
),
),
],
type_ignores=[],
)
I guess a special case can be added for True
to use 4 spaces? What do you think?
P.S. this is a bug in the code that I contributed before :P
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.