Giter Site home page Giter Site logo

astload's Introduction

astload

Create ASTs from ast.dump() output.

It's possible to serialize an AST node from Python's ast module to a string, using ast.dump. However, it's not possible to convert the said string back to an AST node. This simple module adds that functionality.

Examples

We will take a simple Python AST, serialize it and then deserialize it:

>>> import ast
>>> import astload
>>> tree1 = ast.parse('1 == 2')
>>> ast_string = ast.dump(tree1, indent=2)
>>> print(ast_string)
Module(
  body=[
    Expr(
      value=Compare(
        left=Constant(value=1),
        ops=[
          Eq()],
        comparators=[
          Constant(value=2)]))],
  type_ignores=[])
>>> tree2 = astload.load(ast_string)
>>> print(ast.dump(tree2, indent=2))
Module(
  body=[
    Expr(
      value=Compare(
        left=Constant(value=1),
        ops=[
          Eq()],
        comparators=[
          Constant(value=2)]))],
  type_ignores=[])
>>> ast.dump(tree1) == ast.dump(tree2)
True
>>> ast.unparse(tree2)
'1 == 2'

Adding load() to the ast module

If you prefer ast.load() instead of astload.load(), you can do the following to add a load function to the ast module directly:

>>> import astload
>>> astload.install()
>>> import ast
>>> tree = ast.parse('print(2 + 2)')
>>> copy_tree = ast.load(ast.dump(tree))
>>> ast.unparse(copy_tree)
'print(2 + 2)'

astpretty support

astload supports astpretty output also:

>>> ast_string = astpretty.pformat(ast.parse('1 == 2'))
>>> print(ast_string)
Module(
    body=[
        Expr(
            lineno=1,
            col_offset=0,
            end_lineno=1,
            end_col_offset=6,
            value=Compare(
                lineno=1,
                col_offset=0,
                end_lineno=1,
                end_col_offset=6,
                left=Constant(lineno=1, col_offset=0, end_lineno=1, end_col_offset=1, value=1, kind=None),
                ops=[Eq()],
                comparators=[Constant(lineno=1, col_offset=5, end_lineno=1, end_col_offset=6, value=2, kind=None)],
            ),
        ),
    ],
    type_ignores=[],
)
>>> tree = astload.load(ast_string)
>>> astpretty.pprint(tree)
Module(
    body=[
        Expr(
            lineno=1,
            col_offset=0,
            end_lineno=1,
            end_col_offset=6,
            value=Compare(
                lineno=1,
                col_offset=0,
                end_lineno=1,
                end_col_offset=6,
                left=Constant(lineno=1, col_offset=0, end_lineno=1, end_col_offset=1, value=1, kind=None),
                ops=[Eq()],
                comparators=[Constant(lineno=1, col_offset=5, end_lineno=1, end_col_offset=6, value=2, kind=None)],
            ),
        ),
    ],
    type_ignores=[],
)

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.