Skip to main content

Extends Python ast nodes with positional informations

Project description

This project extends Python ast nodes with positional informations, such as where the ast node starts and finishes in the code. This allows us to skip using the Python inconsistent lineno and col_offset

I (João Felipe Pimentel) started this library to help me with ast analysis on noWorkflow

Installing and using PyPosAST is simple and easy. Please check our installation and basic usage guidelines below.

This package supports Python 2.7, 3.4 and 3.5

Quick Installation

To install PyPosAST, you should follow these basic instructions:

If you have pip, just run:

$ pip install pyposast

If you do not have pip, but already have Git (to clone our repository) and Python:

$ git clone git@github.com:JoaoFelipe/pyposast.git
$ cd pyposast
$ ./setup.py install

This installs PyPosAST on your system.

Usage

Just use the ‘parse’ from pyposast it instead of ast.parse:

import pyposast
code = ("variable = 1234\n"
        "if variable:\n"
        "    result = 2 + 2\n"
        "else:\n"
        "    result = 1")
tree = pyposast.parse(code, filename='__main__', mode='exec')

This will add the fields first_line, first_col, last_line, last_col, uid, op_pos to the nodes as following:

Module -> first_line=1, first_col=0, last_line=5, last_col=14, uid=(5, 14)
  body[0]=Assign: firts_line=1, first_col=0, last_line=1, last_col=15, uid=(1, 15)
    targets[0]=Name: id='variable', first_line=1, firts_col=0, last_line=1, last_col=8, uid=(1, 8)
    op_pos[0]=Node: first_line=1, first_col=9, last_line=1, last_col=10, uid=(1, 10)
    value=Num: n=1234, first_line=1, first_col=11, last_line=1, last_col=15, uid=(1, 15)
  body[1]=If: first_line=2, first_col=0, last_line=5, last_col=14, uid=(2, 2)
    test=Name: id='variable', first_line=2, first_col=3, last_line=2, last_col=11, uid=(2, 11)
    body[0]=Assign: first_line=3, first_col=4, last_line=3, last_col=20, uid=(3, 20)
      targets[0]=Name: id='result', first_line=3, first_col=4, last_line=3, last_col=10, uid=(3, 10)
      op_pos[0]=Node: first_line=3, first_col=11, last_line=3, last_col=12, uid=(3, 12)
      value=BinOp: first_line=3, first_col=13, last_line=3, last_col=18, uid=(3, 18)
        op=Add()
        left=Num: n=2, first_line=3, first_col=13, last_line=3, last_col=14, uid=(3, 14)
        right=Num: n=2, first_line=3, first_col=17, last_line=3, last_col=18, uid=(3, 18)
        op_pos=Node: first_line=3, first_col=15, last_line=3, last_col=16, uid=(3, 16)
    orelse[0]=Assign: first_line=5, first_col=4, last_line=5, last_col=14, uid=(5, 14)
      targets[0]=Name: id='result', first_line=5, first_col=4, last_line=5, last_col=10, uid=(5, 10)
      op_pos[0]=Node: first_line=5, first_col=11, last_line=5, last_col=12, uid=(5, 12)
      value=Num: n=1, first_line=5, first_col=13, last_line=5, last_col=14, uid=(5, 14)

The resulting tree can be used as usual with any AST visitor

Contact

Do not hesitate to contact me:

License Terms

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

PyPosAST-1.1.0.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

PyPosAST-1.1.0-py2.py3-none-any.whl (15.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file PyPosAST-1.1.0.tar.gz.

File metadata

  • Download URL: PyPosAST-1.1.0.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for PyPosAST-1.1.0.tar.gz
Algorithm Hash digest
SHA256 748c023a41cbcd55731a787c02801264731ad0084b25cff9c3f825da4bb5676c
MD5 90c602b5fe1e6d2e4e96db7c33bfbaaf
BLAKE2b-256 249596b32b384f481b73fe35a9b5f06a436ae06d85eafc73f6a7fe50d77fe460

See more details on using hashes here.

File details

Details for the file PyPosAST-1.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for PyPosAST-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 15eb3c678c8c3967fe2884a05960e3d5787f87297d19400a261dc95c35de4937
MD5 4b57928a95aea357135ecd55ffbbfa0e
BLAKE2b-256 dc185d64939a8180442095e526f60854a9e199c5e9907e4ec39fb94710970be7

See more details on using hashes here.

Supported by

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