Skip to main content

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.

Metadata

Release files for structional 0.5.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 structional 0.5.1
File Size Uploaded
structional-0.5.1.tar.gz 28.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for structional 0.5.1
File Interpreter ABI Platform
structional-0.5.1-py3-none-any.whl Python 3 none any Details

Total release size: 62.7 kB

Release files / structional-0.5.1.tar.gz

Download URL structional-0.5.1.tar.gz
Size 28.0 kB
Tags Source
SHA-256 checksum
How to use checksums
0f97f7212d2c652ccbe91e8a4fc8b920ce81e469e53e63a3c5c9e1c0dd7290b4
BLAKE2b-256 checksum
How to use checksums
c5226a2090c3eb1003e3639ffc5955e2e19c560e1bee253bd0bdf704bb82b5c3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.14

Release files / structional-0.5.1-py3-none-any.whl

Download URL structional-0.5.1-py3-none-any.whl
Size 34.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ef398849fd450ca043f65a641202263a0ab2ca57d49582ec2cf75f22355c6bd6
BLAKE2b-256 checksum
How to use checksums
e3c40f85b67eb128383ad99627d5c63b7c4c47468bf0740676b39f6f1d53e341
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.14

Release history Release notifications | RSS feed

This release

0.5.1 This release

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.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