Skip to main content

Formation

A generic functional middleware infrastructure for Python.

Take a look:

from datetime.datetime import now
from hs_formation import wrap
from requests import get

def log(ctx, call):
    print("started")
    ctx = call(ctx)
    print("ended")
    return ctx

def timeit(ctx, call):
    started = now()
    ctx = call(ctx)
    ended = now() - started
    ctx['duration'] = ended
    return ctx

def to_requests(ctx):
    get(ctx['url'])
    return ctx

fancy_get = wrap(to_requests, middleware=[log, timeit])
fancy_get({'url':'https://google.com'})

Quick Start

Install from PyPI:

$ pip install hs-formation

Development

Install runtime dependencies plus the dev and ci groups, then run tests:

$ make install
$ make test

The ci group lists everything needed to execute the test suite and produce a coverage report in CI (for example pytest, pytest-cov, and other plugins). The dev group lists local-only tools such as pytest-watch.

To install only what CircleCI uses (no dev-only tools):

$ make install_ci

make ci_install is an alias for make install_ci.

After changing dependencies:

$ make lock

To verify pdm.lock is up to date without writing:

$ make verify_lock_file

Releases and tags

Release automation is handled by ci/release.sh and Makefile targets:

$ make release         # patch
$ make release_minor   # minor
$ make release_major   # major

For breaking changes (for example dropping Python support), use a major release so the tag and package version move to the next major semver:

$ make release_major

Best Practices

A context object is a loose bag of objects. With that freedom comes responsibility and opinion.

For example, this is how Formation models a requests integration, with data flowing inside context:

  • It models a FormationHttpRequest which abstracts the essentials of making an HTTP request (later shipped to requests itself in the way that it likes)
  • It tucks FormationHttpRequest under the fmtn.req key.
  • Additional information regarding such a request is kept alongside fmtn.req. For example a request id is kept in the req.id key. This creates a flat (good thing) dict to probe. The reason additional data does not have the fmtn prefix is that you can always build your own that uses a different prefix (which you cant say about internal Formation inner workings).

added support for async http client via aio_http

You can use this via for_aio_http

Thanks:

To all Contributors - you make this happen, thanks!

Copyright

Copyright (c) 2018 @jondot. See LICENSE for further details.

Metadata

Release files for hs-formation 6.0.1

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

Source distribution (sdist)

Source distribution for hs-formation 6.0.1
File Size Uploaded
hs_formation-6.0.1.tar.gz 345.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for hs-formation 6.0.1
File Interpreter ABI Platform
hs_formation-6.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 360.2 kB

Release files / hs_formation-6.0.1.tar.gz

Download URL hs_formation-6.0.1.tar.gz
Size 345.0 kB
Tags Source
SHA-256 checksum
How to use checksums
97a1ccf1cb30e4403dfc8f95fbb46b70fd2be08ea4afed70d9cbeb4187802381
BLAKE2b-256 checksum
How to use checksums
95abb5d5b87b632c5d4992dbbd169bb500878647ec3aca6d4f6a2c1cfcb4fdd1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.26.9 CPython/3.9.16 Darwin/25.5.0

Release files / hs_formation-6.0.1-py3-none-any.whl

Download URL hs_formation-6.0.1-py3-none-any.whl
Size 15.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9e608b019c9c7b61821cbfb5c9ddf29291368240641871c5b977f3af25119f38
BLAKE2b-256 checksum
How to use checksums
b615f26a994c689eb195aa594589fa30b709e1eef368c2c7110a599967ef976b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.26.9 CPython/3.9.16 Darwin/25.5.0

Release history Release notifications | RSS feed

This release

6.0.1 This release

2 release files

5.5.0

2 release files

5.4.0

2 release files

5.2.0

2 release files

5.1.0

2 release files

5.0.2

2 release files

5.0.0

2 release files

4.3.7

2 release files

4.3.6

2 release files

4.3.5

2 release files

4.3.4

2 release files

4.3.1

2 release files

4.3.0

2 release files

4.2.2

2 release files

4.2.1

2 release files

4.1.13

2 release files

4.1.12

2 release files

4.1.11

2 release files

4.1.0

2 release files

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