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.1.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

with_contextvars-0.1.1-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file with-contextvars-0.1.1.tar.gz.

File metadata

  • Download URL: with-contextvars-0.1.1.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.7.3 Linux/5.10.0-0.bpo.15-amd64

File hashes

Hashes for with-contextvars-0.1.1.tar.gz
Algorithm Hash digest
SHA256 aa3dc5320aca7ab16d742e294772f737937bc0731df0c5861f043c75ac33e5d6
MD5 87300d08254616a020147c2649107d89
BLAKE2b-256 d8df80be8bcb346d57aeb08801e329d3ca28130169d9612caa713c1974c4f706

See more details on using hashes here.

File details

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

File metadata

  • Download URL: with_contextvars-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.7.3 Linux/5.10.0-0.bpo.15-amd64

File hashes

Hashes for with_contextvars-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5588273b4b8228f952ebdc67fc458018f4fe861db7f0b719eed0edb9f9a371a7
MD5 633198abd76bc80ec228b9184ea07865
BLAKE2b-256 3e95b0187283239359d8e2daf115db53b7d05c5cab8654407a6c093acd96d9dc

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