Skip to main content

A nice interface to context variables

Project description

contextmodel

Alternative interface to context variables for practical scenarios.

>>> from contextmodel import context_get, context_set
>>> from dataclasses import dataclass

>>> @dataclass
... class Foo:
...     x: int | None = None

>>> @context_set(Foo(x=1))
... def f() -> None:
...     print(context_get(Foo))
>>> f()
Foo(x=1)

>>> from contextmodel import WithContextAttribute

>>> @dataclass
... class Foo(WithContextAttribute):
...     x: int | None = None

>>> with Foo.context.init(x=2):
...     print(Foo.context.get())
Foo(x=2)

>>> @Foo.context.init(x=3)
... def f() -> None:
...     print(context_get(Foo))
>>> f()
Foo(x=3)

>>> def oops() -> None:
...     print(context_get(Foo))
>>> oops()
Traceback (most recent call last):
  ...
LookupError: expected a context_set(Foo(...)) prior to this call

Works with type hints:

image

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

contextmodel-0.3.0.tar.gz (43.5 kB view details)

Uploaded Source

Built Distribution

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

contextmodel-0.3.0-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file contextmodel-0.3.0.tar.gz.

File metadata

  • Download URL: contextmodel-0.3.0.tar.gz
  • Upload date:
  • Size: 43.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for contextmodel-0.3.0.tar.gz
Algorithm Hash digest
SHA256 3af4e09d580e8b3083439d06aa8c664c524ee5f2d06a9a0f5350c33178c6b531
MD5 6b3ef065f6d29376a9089a179d294397
BLAKE2b-256 d1b4969ce87888cdfa725c42aa00dc0fb65f8b641df47490f245c0902f253692

See more details on using hashes here.

Provenance

The following attestation bundles were made for contextmodel-0.3.0.tar.gz:

Publisher: ci.yml on johnslavik/contextmodel

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file contextmodel-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: contextmodel-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for contextmodel-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 50aa61504c7c0d746261372b0ac563bccf97791b42aa3825ce39592a55483c5f
MD5 075a6d0343a6e523555b967c13717bd1
BLAKE2b-256 8de87214ab3c5730db8960c066dd9ba3c57e1e11df6368f20835cceac505bb15

See more details on using hashes here.

Provenance

The following attestation bundles were made for contextmodel-0.3.0-py3-none-any.whl:

Publisher: ci.yml on johnslavik/contextmodel

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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