Skip to main content

No project description provided

Project description

xtuples

PyPI - Version PyPI - Python Version


Table of Contents

Installation

pip install xtuples

xtuples

xtuples is designed to make functional programming easier in Python.

The two key constructs are:

  • xtuples.iTuple: a base class for iterable data types, equipped with methods like .map(), .fold(), .pipe() and .partial().
assert (
    iTuple.range(5)
    .map(lambda x: x * 2)
    .filter(lambda x: x < 5)
    .accumulate(operator.sum)
    .pipe(sum)
) == 8
  • xuples.nTuple.decorate: a decorator to inject .pipe() and .partial() into user-defined NamedTuples (as we can't subclass them directly).
@nTuple.decorate
class Example(collections.NamedTuple):
    x: int

assert Example(1, "a").pipe(lambda obj: obj.int) == 1

As briefly demonstrated above, by equipping our data structures with .pipe() and .partial(), we're able to use method chaining to mimic the functional pipelines seen in languages like f#.

This then tends to lead us away from inheritance, and more towards composition: to a code base comprised entirely of either free functions, or descendants of either of the above.

Performance

Performance should generally be at worst, not worse than a non-optimised canonical equivalent (and can often be significantly better).

For instance, NamedTuples provide faster access, and are more memory efficient, than standard classes (or even raw dicts).

< Insert demo >

Similarly, iTuple - as a subclass of tuple - is more memory efficient, and provides as fast access, than a standard raw list.

< Insert demo >

The one caveat to this is that some methods which are canonically lazy, returning generators, are treated eagerly - however this is something I am actively working on.

xtuples.json

xtuples.json provides base classes for weakly-rich json encoding / decoding (rich in that classes are preserved, weak in that this is based on class name alone and no further checks or guarantees are provided).

License

xtuples is distributed under the terms of the MIT license.

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

xtuples-0.0.6.tar.gz (118.2 kB view hashes)

Uploaded Source

Built Distribution

xtuples-0.0.6-py3-none-any.whl (8.9 kB view hashes)

Uploaded Python 3

Supported by

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