Skip to main content

General Abstract Meta Programming for Python

Project description

gampy

Gampy provides experimental constructs to improve your Python programs.

Pipeline

While functional programming is a great paradigm to create data pipelines, some operations remains hard to express:

  • wrap every function of a data pipeline with an external function (e.g. logging, safe exception handling ...)
  • concatenate and transform data pipelines with algebra operators (e.g. convert and compare a pipeline ...)
  • apply the compose and partial operators on a data pipeline to create a single callable function

Gampy Pipeline is a data structure created to address these problems.

Here is an example of a simple pipeline:

from gampy.structures import Pipeline

pipeline = Pipeline([
    (map, [lambda x: x + 1], {}),
    (filter, [lambda x: x % 2 == 0]),
    (list,)
])

Each step of the pipeline is represented as a 3-tuple: (function, arguments, keyword arguments). While function is mandatory, arguments and keyword argument will be replaced by list() and dict() respectively if they are missing. This structure allows the creation of unevaluated expression, that can be further transformed prior to their execution.

The most interesting operations over a pipeline are () (call) and @ (matmul).

Call converts the pipeline into a single function. This process is divided in two steps:

  • functools.partial is applied on each step arguments to create a single function per step
  • functions are composed two by two with functools.reduce to create a single function per pipeline
>>> f = pipeline()
>>> f(range(10))
30

Matmul applies an advice to each function of the pipeline. This allows the expression of cross concern aspects.

In the snippet below, any exception raised by a pipeline function will return None.

from gampy.advices import exceptional

safepipe = pipeline @ exceptional(None)

An advice is similar to a parametrized decorator, which create a function that takes a function and replaced it by a new function. The purpose is to extend the behavior of the original function.

def exceptional(x: Any = None, on: Type[Exception] = Exception) -> Advice:
    """Return x when f raises an exception."""

    def advice(f):
        @wraps(f)
        def wrapped(*args, **kwargs):
            try:
                return f(*args, **kwargs)
            except on:
                return x

        return wrapped

    return advice

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

gampy-1.1.0-py2.py3-none-any.whl (6.5 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: gampy-1.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.3

File hashes

Hashes for gampy-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 773a2c0dd18205a52b915b9c3e0145ca9417364052cc565d5c27a444969971a5
MD5 3e0deb5f7cd0d3dd7450bedd5a800cd1
BLAKE2b-256 d57fb86a1e3b2669928c20bee443d7b73a9b340e44e0c00076ddebf07d40292d

See more details on using hashes here.

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