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

Uploaded Python 3

File details

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

File metadata

  • Download URL: contextmodel-0.2.1.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.2.1.tar.gz
Algorithm Hash digest
SHA256 ef69daef178ca187f53cd23446755711df77f45f3011672ac6112ec1ca8ca1af
MD5 fa02ae18afebe0def5aee95f69e70cf2
BLAKE2b-256 26cc52bee5ad80d1aa6039d9d969aec9887eb12b5d0d433c172f986141ad7206

See more details on using hashes here.

Provenance

The following attestation bundles were made for contextmodel-0.2.1.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.2.1-py3-none-any.whl.

File metadata

  • Download URL: contextmodel-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 3.7 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.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c67d24479e56ea484d347374b803913dbb4da07c056d2d9213a3a1a212fa0944
MD5 e70ebe6572aba9a6d162be869fc093d9
BLAKE2b-256 ee8d91a4936270424c4367650391119464d1f503a402aba9dc8bc6d5b0e07620

See more details on using hashes here.

Provenance

The following attestation bundles were made for contextmodel-0.2.1-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