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 ContextAPIGetter, ModelGetter
>>> from typing import ClassVar

>>> @dataclass
... class Foo:
...     context = ContextAPIGetter()
...     current = ModelGetter()
...     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(Foo.current)
>>> 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.1.tar.gz (43.6 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.1-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: contextmodel-0.3.1.tar.gz
  • Upload date:
  • Size: 43.6 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.1.tar.gz
Algorithm Hash digest
SHA256 1c9fd20c29bf476523faa466a53f4f21f13f82c25e1b4d8d60e95aab5ed7576e
MD5 5812e1494f76fedd0a1c983d7b9b82da
BLAKE2b-256 f8ead0b22f4ca8b6cd4aa858c89fa065e61bcdf98bd62bb853317de459b7b5af

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: contextmodel-0.3.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a133bb96c251df84baf99897f4fc67d2f4e1522be1093d0636b74996d51e088c
MD5 0b31534d95b6a1c55b063dbe0939acc9
BLAKE2b-256 222cd4343798e655ec21c09c5a372a8311da7d3f5db55a60265d066ca87fb660

See more details on using hashes here.

Provenance

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