Skip to main content

Immutable structs, controlled randomness, and trees. These are the core tools I use to build reliable software in Python.

Project description

structional

Immutable structs, controlled randomness, and trees. These are the core tools that are ubiquitous to my style of writing Python. Now available in open source!

Whilst this library does not depend on PyTorch and is broadly applicable across all of Python, the downstream use-case is to make it possible to 'write functional PyTorch'.

This is not a library implementing monads, foldl, and all that magical Haskell stuff.

Installation

pip install structional

Documentation

Available at https://docs.kidger.site/structional.

What's in the box?

Struct:

  • is an immutable (frozen) dataclass, making writing safe code easy;
  • is an abstract base class, supporting the stdlib abstractmethod and our own extensions AbstractVar (declaring abstract attributes) and __check_init__ (post-initialization invariants);
  • enforces 'abstract/final rules': every class can either be subclassed (abstract) or instantiated (concrete) – but not both.

PRNGKey provides access to deterministic randomness.

  • Use-case in ML is perfectly reproducible training runs; awesome for catching bugs.
  • A function need no longer be 'secretly random' because it depends on a background stateful RNG.
  • Call key.some_distribution() to sample from a distribution.
  • Split a key via key.split() to create deterministic but statistically independent new sources of randomness.
  • Keys can only be used once (known as 'linear typing'), to prevent accidental reuse.

tree is a subpackage for manipulating immutable nested structures of tuples, Structs, etc.:

  • tree.map ('functors' for you programming geeks) applies a function to every leaf.
  • tree.replace ('lenses' for the geeks) updates just part of a tree structure, returning a new object out-of-place.

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

structional-0.5.0.tar.gz (28.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

structional-0.5.0-py3-none-any.whl (34.7 kB view details)

Uploaded Python 3

File details

Details for the file structional-0.5.0.tar.gz.

File metadata

  • Download URL: structional-0.5.0.tar.gz
  • Upload date:
  • Size: 28.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for structional-0.5.0.tar.gz
Algorithm Hash digest
SHA256 e152d850ee83e3d188424581e2f53fd5ebe9ce844922fd75d7b2de008d73d05c
MD5 271b00d4a71cb9ab34f7eeff3af98962
BLAKE2b-256 bfceb984e020e7aec38ecc00ced4d98fc3675eec0644c9edc76c8e0596494944

See more details on using hashes here.

File details

Details for the file structional-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: structional-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 34.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for structional-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9d24c0542b276050f9a538ef95d37e07f2cdcc5cf8b30b2ada431838e04d1d60
MD5 b7bbeb4b7bf8bb663c5fcd35c22c44a3
BLAKE2b-256 34ee81dc79e2199099737f0a88c1b2ccb99d2439cdaedcb309d152b52562a306

See more details on using hashes here.

Supported by

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