Skip to main content

human-oriented ast parser/unparser

Project description

Human-oriented abstract syntax tree (AST) parser/unparser for Python 3 that doesn’t discard comments.

package version from PyPI build status from Travis CI build status from AppVeyor grade from Codacy test coverage from Codecov license

This package provides new AST node types (Comment and Directive) which inherit from nodes in typed_ast.ast3 module. Additionally, it provides implementation of parser and unparser for the extended AST allowing straightforward readable code generation.

Simple example of how to use this package:

from horast import parse, unparse

tree = parse("""a = 1  # a equals one after this""")
print(unparse(tree))
# this will print the code with original comment

More examples in examples.ipynb.

technical details

Parser is based on built-in tokenize module, as well as community packages asttokens and typed_ast.

Unparser is essentially an extension of Unparser class from static_typing package.

Nodes provided and handled by horast are listed below.

Comment

Full line as well as end-of-line comments are parsed/unparsed correctly when they are outside of multi-line expressions.

Currently, handling of comments within multi-line expressions is implemented only partially.

BlockComment

Not implemented yet.

This node type is intended to store consecutive full-line comments in a single AST node, therefore simplifying handling of large blocks of comments.

Directive

Since Python doesn’t offer a direct way to convey directives in code, comments beginning with any of the following prefixes will not be classified as typical comments, but as directives:

  • if

  • else

  • endif

  • def

  • undef

  • ifdef

  • ifndef

However in Python code they still remain as usual comments.

For example, the comments in the following code will all be classified as directives.

#ifdef DEBUG
debugging = True
#else
debugging = False
#fi

However, when executed as Python, debugging will always end up False because directives are preserved as usual comments in Python and therefore they are ignored.

Therefore, the Directive node is not meant to enable preprocessing of Python, at least for now.

Note: the prefix is checked exactly. See the following example:

#if something
# if some other thing

The comment in the first line will become Directive object, while the one on the second like will become Comment object.

Currently, this node is meant to work towards AST compatibility between Python and other languages, to aide code generation from Python AST into code in other languages – one such use case is *transpyle* project.

Pragma

Pragma nodes follow the same general rules as Directive nodes, i.e. they are stored in Python code as comments, but a comment will be classified as a pragma when it’s prefixed with a predefined prefix:

  • " pragma:"

Additionally, two subclasses of Pragma are defined in horast, each with its own prefix that builds upon the generic pragma prefix:

  • OpenMpPragma class defines prefix " pragma: omp" and stores OpenMP pragmas.

  • OpenAccPragma class defines prefix " pragma: acc" and stores OpenACC pragmas.

A code snippet below contains all 3 pragma types.

# pragma: once
use_openmp = True
use_openacc = True
...
a, b = np.ndarray(...)
c = np.zeros(...)
# pragma: acc parallel copyin(a,b) copyout(c)
# pragma: acc loop gang
for y in range(ymax): # type: np.int32
    # pragma: acc loop worker
    for i in range(imax): # type: np.int32
        # pragma: acc loop vector reduction(+: c[y][x])
        for x in range(xmax): # type: np.int32
            c[y, x] += a[y, i] * b[i, x]
# pragma: acc end parallel
...
# pragma: omp parallel do
for i in range(input_data.size):  # type: int
    # here we compute spam spam spam
    heavy_compute(input_data[i])
...

And thus, in the example above:

  • all comments starting with " pragma: omp" become OpenMpPragma objects,

  • all comments starting with " pragma: acc" become OpenAccPragma objects,

  • all other comments starting with " pragma:" become Pragma objects,

  • type comments are ignored, and

  • all other comments become Comment objects.

Additionally, horast module provides an extensible infrastructure to define custom Pragma subclasses, enabling user to define their own pragmas for experimentation. The provided OpenMP and OpenACC pragma definitions serve as examples of how to use this feature.

Include

Similarly to how pragmas are handled, if a comment begins with " include:" prefix, it will be classified as a special kind of include directive.

Again, this will be preserved as comment in Python code, but it’s useful for enhancing syntactic compatibility between Python and other, especially statically compiled languages.

requirements

CPython 3.5 or later.

Python libraries as specified in requirements.txt.

Building and running tests additionally requires packages listed in test_requirements.txt.

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

horast-rereleased-0.4.3.tar.gz (29.8 kB view details)

Uploaded Source

Built Distribution

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

horast_rereleased-0.4.3-py3-none-any.whl (21.5 kB view details)

Uploaded Python 3

File details

Details for the file horast-rereleased-0.4.3.tar.gz.

File metadata

  • Download URL: horast-rereleased-0.4.3.tar.gz
  • Upload date:
  • Size: 29.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.6

File hashes

Hashes for horast-rereleased-0.4.3.tar.gz
Algorithm Hash digest
SHA256 babd547a19588d103640390496c7433f0831e34e811baa0e42fee98b37b00673
MD5 d33d1bb97f521c0e1e89516909b68695
BLAKE2b-256 fc9bb3a6fb804b6bce74e0bc9b3b4c3ca234b95d91da4d09e8ca644672979e40

See more details on using hashes here.

File details

Details for the file horast_rereleased-0.4.3-py3-none-any.whl.

File metadata

  • Download URL: horast_rereleased-0.4.3-py3-none-any.whl
  • Upload date:
  • Size: 21.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.6

File hashes

Hashes for horast_rereleased-0.4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d92cc6142a9b0d5f67c3b8964529b3c88e12b1790699fde9c6592deb74fab149
MD5 ec53953455076132a7cfa9c13935a08f
BLAKE2b-256 02fc9c0b1ba86b1a0f7df0e7c0d02f9cbb4ee7f407d4512b1493ed7f7ee581c0

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