Skip to main content

Context manager for setting contextvars variables

Project description

This module provides Set, a context manager which sets one or more contextvars variables upon activation and resets them to their previous values at exit.

Usage:

import contextvars, with_contextvars
A = contextvars.ContextVar("A")
B = contextvars.ContextVar("B")
A.set("Hello,")
B.set("world!")
print(A.get(), B.get())
# prints: Hello, world!
with with_contextvars.Set((A, "other"), (B, "value")):
    print(A.get(), B.get())
    # prints: other value
print(A.get(), B.get())
# prints: Hello, world!

Even the entirety of variable assignments of a contextvars.Context object (as obtained from contextvars.copy_context()) can be activated by initializing Set with its items:

with with_contextvars.Set(*context.items()):
    ...

However, using contextvars.Context.run() is more efficient and should be preferred where possible.

More information can be found in the documentation of Set.

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

with_contextvars-0.1.2.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

with_contextvars-0.1.2-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file with_contextvars-0.1.2.tar.gz.

File metadata

  • Download URL: with_contextvars-0.1.2.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.0 CPython/3.9.2 Linux/5.10.0-26-amd64

File hashes

Hashes for with_contextvars-0.1.2.tar.gz
Algorithm Hash digest
SHA256 bb26d9a51f310cef9fb306cb04b09e87a3c0378059327bd2b5a6ea66c5ab7ce7
MD5 92d7c0dbf0a2fe00235b7a7de78d3bee
BLAKE2b-256 061d1225d1c36e074c31fa75267aa70042a086c0c21f35a20a42e8eef628ed32

See more details on using hashes here.

File details

Details for the file with_contextvars-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: with_contextvars-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.0 CPython/3.9.2 Linux/5.10.0-26-amd64

File hashes

Hashes for with_contextvars-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8aae290cd363aa35560e4f1c3959a98b025457b8319c8d63f624b7825e51996e
MD5 bea491914bca2011cf2da94b6e52f431
BLAKE2b-256 fdc80269fdca6fab81ec9aa487df5bc3f09fb4577fb3d49a6f6682a5cb466579

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page