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.1.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.1-py3-none-any.whl (34.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: structional-0.5.1.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.1.tar.gz
Algorithm Hash digest
SHA256 0f97f7212d2c652ccbe91e8a4fc8b920ce81e469e53e63a3c5c9e1c0dd7290b4
MD5 d2416e1df2e8296680ace7a352f520ca
BLAKE2b-256 c5226a2090c3eb1003e3639ffc5955e2e19c560e1bee253bd0bdf704bb82b5c3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: structional-0.5.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ef398849fd450ca043f65a641202263a0ab2ca57d49582ec2cf75f22355c6bd6
MD5 460123efff2d94de4d1a09ed51477b8d
BLAKE2b-256 e3c40f85b67eb128383ad99627d5c63b7c4c47468bf0740676b39f6f1d53e341

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