Git Product home page Git Product logo

astpretty's People

Contributors

asottile avatar mxr avatar pre-commit-ci[bot] avatar progval avatar tusharsadhwani avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

astpretty's Issues

Allow using stdin for input

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

Printing quallified type names (i.e., prefixing the output types with 'ast.')

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 ;)

support dumping AST created by typed_ast

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

pprint support context fields like `lineno`?

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?

`indent=True` is treated as `indent=1` in pprint

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

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.