Skip to main content

Extra helpers for python contextmanager

Project description

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

  • ContextObject initialized by collections, to make it possible to enter a set/list/dict of context managers.
  • Add ContextObject to other objects, to support nested contexts. For example a server (implemented as a ContextThread) can spawn it's client threads within the parents context.

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

ctxs-0.0.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ctxs-0.0.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file ctxs-0.0.1.tar.gz.

File metadata

  • Download URL: ctxs-0.0.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for ctxs-0.0.1.tar.gz
Algorithm Hash digest
SHA256 4bf524bacab16c1fcd84d7d3321f03b49c0dda8de543119c4cafd0bf795be017
MD5 941c0fe1ab0ecc1c8eb4455c90949631
BLAKE2b-256 68677495a46050914b6ff3ad728e1edd3a71dc1b2341469cc891db68a6c1356e

See more details on using hashes here.

Provenance

The following attestation bundles were made for ctxs-0.0.1.tar.gz:

Publisher: python-publish.yml on pengi/ctxs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ctxs-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: ctxs-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for ctxs-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0b1ea1a9d729ab0029c213d9628d9c3ffcdf1ec6e70638d212050872ffe7fd91
MD5 f20f0c3eb2bf3b2005ede922c3340d34
BLAKE2b-256 01150a67d6e17086ba115083b1556da0f6f6db809bb183fbcaaf50f38765c86b

See more details on using hashes here.

Provenance

The following attestation bundles were made for ctxs-0.0.1-py3-none-any.whl:

Publisher: python-publish.yml on pengi/ctxs

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