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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: with_contextvars-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 4cd0e33de8fe91c42ad7264faa9ce75e696e3820226bc2ff79d55ea7c7ed5fa8
MD5 eee6ad515b1d736252d083273590fcc3
BLAKE2b-256 37416f51ab3089f1d1de1988373402b96b5a3258752852be43183ad111040564

See more details on using hashes here.

File details

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

File metadata

  • Download URL: with_contextvars-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 78438b01726927af3677834429a4c5e77e5e31e84296c2c5749848c45ed8a145
MD5 cf9299fae618656ed28c1d4922e87623
BLAKE2b-256 ecfac53471db9b79e3e73bca021ac5446225197b389ffce33fe220422bb1edb6

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