ctxs - extra helpers for python "with ..."
The with statement available in python makes it possible to add context and cleanup to resources, which is useful when working with shared resources that needs to be released afterwards.
This adds some extra tools to make usage easier.
ContextObject
When working with multiple contexts at the same time, it is possible to use
multiple context managers in the same with statement:
with ResourceA() as a, ResourceB() as b:
a.do_something()
b.do_something()
But if something needs to be added to the scope later, or dynamically add resources to the same context, it is not possible.
ContextObject() managed the context, and new resources can be added later,
using the <<-operator.
The above statement is equivalent to:
from ctxs import ContextObject
with ContextObject() as ctx:
a = ctx << ResourceA()
b = ctx << ResourceB()
a.do_something()
b.do_something()
with the addition that resources can be added later, and dynamically.
ContextThread
threading adds the possibilty to create threads easily. However, synchronizing execution is still based upon function calls to the thread object.
In many cases, some thread should be running only within the scope of a certain block. For example, a temporary server thread only available when a system is in a certain state.
ContextThread makes it possible to create a thread that is running within a
given context, with or without ContextObject above.
To create a ContextThread, extend the ContextThread and implement the
run() function, which is executed when the thread enters the context.
Within the context, the self.is_running is True while running, and when
exiting the context, it will be set to False.
from ctxs import ContextThread
import time
class MyThread(ContextThread):
def __init__(self) -> None:
pass
def run(self) -> None:
while self.is_running:
print("Tick...")
time.sleep(1.0)
# Create and run in parallel
with MyThread():
for i in range(10):
time.sleep(0.7)
print("Tock...")
print("Has it stopped?")
time.sleep(4.0)
print("Who knows...")
Installation
ctxs is available on pypi
To install:
pip install ctxs
Feedback
The goal of this library is to keep as clean as possible, while still complement the builtin behaviour.
If you have any thoughts about what it should added.
Roadmap / Ideas
ContextObjectinitialized by collections, to make it possible to enter a set/list/dict of context managers.- Add
ContextObjectto other objects, to support nested contexts. For example a server (implemented as aContextThread) can spawn it's client threads within the parents context.
Metadata
Release files for ctxs 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ctxs-0.0.1.tar.gz | 4.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ctxs-0.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.8 kB
Release files / ctxs-0.0.1.tar.gz
| Download URL | ctxs-0.0.1.tar.gz |
|---|---|
| Size | 4.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4bf524bacab16c1fcd84d7d3321f03b49c0dda8de543119c4cafd0bf795be017
|
|
BLAKE2b-256 checksum How to use checksums |
68677495a46050914b6ff3ad728e1edd3a71dc1b2341469cc891db68a6c1356e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 7, 2025.
Transparency logRelease files / ctxs-0.0.1-py3-none-any.whl
| Download URL | ctxs-0.0.1-py3-none-any.whl |
|---|---|
| Size | 5.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0b1ea1a9d729ab0029c213d9628d9c3ffcdf1ec6e70638d212050872ffe7fd91
|
|
BLAKE2b-256 checksum How to use checksums |
01150a67d6e17086ba115083b1556da0f6f6db809bb183fbcaaf50f38765c86b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 7, 2025.
Transparency log