Skip to main content

Missing AST features

Project description

Asteria (Al2O3)

Missing AST features

Features

  • Repr implementation to every node (uses astpretty if it is available, if not fall backs to the ast.dump)
  • AST Comparisons with other nodes
  • Custom initializer with finding default values according to ASDL spec
  • Shortcuts for fix_missing_locations, compile (e.g ast.parse("2+2").compile())
  • Unparsing shortcuts, ast.parse("2+2").to_source(). It uses astor if its available, if not it fallbacks to the ast.unparse interface.
  • Parent/Child relationships to nodes
  • Helpers functions for mutating tree (like add_global method to Module nodes which inserts given node at the top of body)
  • Symbol table access with get_symbol_table method
  • and many more...

Demo

>>> import ast
>>> import asteria
>>> ast.parse("2+2") == ast.parse("2+2")
True
>>> ast.parse("2+2").body[0].value
BinOp(
    left=Constant(value=2, kind=None),
    op=Add(),
    right=Constant(value=2, kind=None),
)
>>> ast.parse("print(2+2)").compile()
<code object <module> at 0x7f2602f21450, file "<ASTERIA>", line 1>
>>> eval(_)
4
>>> ast.parse("import asteria").to_source()
'import asteria'
>>> sample = ast.parse("2+2")
>>> sample.parentize()
>>> sample.body[0].value.left.parent
BinOp(
    left=Constant(value=2, kind=None),
    op=Add(),
    right=Constant(value=2, kind=None),
)
>>> sample.body[0].value.parent
Expr(
    value=BinOp(
        left=Constant(value=2, kind=None),
        op=Add(),
        right=Constant(value=2, kind=None),
    ),
)
>>> some_try = ast.parse("""\
... try:                
...     def x():        
...             print(1)
... finally: pass       
... """)
>>> some_try.parentize()
>>> some_try.body[0].body[0].body[0].value
Call(
    func=Name(id='print', ctx=Load()),
    args=[Constant(value=1, kind=None)],
    keywords=[],
)
>>> len(tuple(some_try.body[0].body[0].body[0].value.until_parented_by(ast.Try)))
3
>>> some_try.add_global(ast.parse("print('lol')").body[0])
>>> print(some_try.to_source())
print('lol')
try:

    def x():
        print(1)
finally:
    pass

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asteria-0.0.2.tar.gz (4.8 kB view details)

Uploaded Source

File details

Details for the file asteria-0.0.2.tar.gz.

File metadata

  • Download URL: asteria-0.0.2.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.5

File hashes

Hashes for asteria-0.0.2.tar.gz
Algorithm Hash digest
SHA256 852ced0ee2bb064908b98748c0b4ddc2d0c7d8d71a4885b9efe0c1aff49a8e7b
MD5 8ac25bcc5d889567654241651f55848e
BLAKE2b-256 0445ef5cc71cdb44a91b4a0aa3fff8c6f660d082911ade053ca7bb2de0e1f940

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page