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_enter(Foo(...)) prior to this call

Works with type hints:

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.0.tar.gz (43.4 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.0-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: contextmodel-0.2.0.tar.gz
  • Upload date:
  • Size: 43.4 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.0.tar.gz
Algorithm Hash digest
SHA256 b239af0ad02fbac9e13541d4d67069adc6d5fbe6d9f0cfea36e0fcf7548e7219
MD5 272aa2b76d931c33cc1e035be4953764
BLAKE2b-256 a662ca903e60d99998edbe8c4bca86e4202f769aebab8d1919d81b2217430cb6

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: contextmodel-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 3.6 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7eac7d63cbf6f1bf2b0f087ee8ffd4e6b01798fe54f74e6c1dff418a1c3a94b5
MD5 7f52702854ae1e6d17152f6609753df8
BLAKE2b-256 9812bd995bfde8b3c9ed0ead6d7994b77caec0ee4be7f7c9a0d9548834b17a18

See more details on using hashes here.

Provenance

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