Skip to main content

Introduction

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

Instead of writing this:

from pyrsistent import pvector
x = pvector([1, 2, 3])
y = x.set(0, 17)

You can simply write this:

x = [1, 2, 3]
y = x.set(0, 17)

The results will be equivalent.

Examples

In foo/main.py:

# Register any modules under 'foo' for pyrsistent substitution before any
# imports from 'foo' modules.
#
# Registration can be done in three ways:
# * Exact module name.
#   register('foo.bar')
# * Prefix with wild card. All submodules of the prefix.
#   register('foo.*')
# * Custom matcher function that returns true if substitution should be applied in the module.
#   register(lambda name: name.startswith('foo') and name.endswith('baz'))

from pyrthon import register
register('foo.*')

from foo.app import run

if __name__ == '__main__':
    run()

In foo/app.py:

# All literal collection declarations in this module will now be mapped to
# corresponding pyrsistent collections.

def run():
    x = [1, 2, 3]    # -> pvector([1, 2, 3])
    y = {'a': 1}     # -> pmap({'a': 1})
    z = {'a'}        # -> pset(['a'])

    # It's not possible to declare an empty set in python using a literal.
    # Therefore some special treatment is required.
    p = set()   # -> pset([]), pyrsistent pset
    q = set([]) # -> set(), regular python set

    # To declare regular python lists, dicts and sets just use the function syntax
    a = list([1, 2, 3])    # -> [1, 2, 3]
    b = dict(a=1)          # -> {'a': 1}
    c = set(['a'])         # -> set(['a'])

    print('Hello functional world!')

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

>>> import pyrthon.console
Pyrsistent data structures enabled
>>> [1, 2, 3]
pvector([1, 2, 3])
>>> {'a': 1}
pmap({'a': 1})
>>> {'a'}
pset(['a'])

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.

Metadata

Release files for pyrthon 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyrthon 0.1.2
File Size Uploaded Provenance
pyrthon-0.1.2.tar.gz 5.2 kB Without provenance Details

Release files / pyrthon-0.1.2.tar.gz

Download URL pyrthon-0.1.2.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
489436189fd1e777d8b703b4d5cd665ebd245d490a5e70f0af8ef0eebd045e81
BLAKE2b-256 checksum
How to use checksums
8d251f95fe127a5c4a76c122b7f2b1033a688de35eb82e187f80f29873b1cea8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for pyrthon 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page