Skip to content

tobgu/pyrthon

Repository files navigation

Introduction

Pyrthon is a utility library that substitutes python collection literals with their Pyrsistent counterparts.

Instead of writing this:

You can simply write this:

The results will be equivalent.

Examples

In foo/main.py:

In foo/app.py:

Pyrthon also has basic shell support. Just import pyrthon.console and you're good to go:

It's also possible to use pyrthon from Jupyter/IPython notebooks. For that an extension must be loaded. This can be done from the console or a cell:

% load_ext pyrthon

Installation

pip install pyrthon

How it works

Pyrthon works by Python AST manipulation and import hooks. All literal lists and list comprehensions, dicts and dict comprehensions, sets and set comprehensions in selected modules are substituted to produce the equivalent Pyrsistent data structure upon module import.

Limitations and quirks

This library is currently in experimental status.

If combined with other frameworks that manipulate the AST or performs other "magic" transformations to your code the result may not be as expected.

Usage in tests executed with pytest requires some additional work since no explicit import of the test files is ever performed. Also the assert used by pytest is heavily manipulated compared to the original assert and prevents direct substitution of literals. Normally this should not matter for the sake of testing though since a pvector compares to a list, a pmap to a dict and a pset to a set but it's good to know.

Because substitution is performed on import Pyrthon currently requires at least two python files in any application and library. One file, in which no substitutions will take place, will have to register all modules on which transformations should be applied before those modules are imported. The file containing the main entry point for a program/library would be a good point to perform this registration.

Compatibility

Pyrthon is developed and tested on Python 2.7, 3.4 and PyPy (Python 2.7 compatible).

Contributors

Tobias Gustafsson https://github.com/tobgu

Todd Iverson https://github.com/yardsale8 (IPython/Jupyter support)

Contributing

If you experience problems please log them on GitHub. If you want to contribute code, please submit a pull request.