Skip to main content

EZ Context Managers

Allows you to create generator-based context managers without try/finally. Fully static-typed and thread-safe.

Installing

pip install ez_context

Example:

@context_mgr
def example():
    print("entering")
    yield
    print("exiting")

with example as ex:
    print("Hello World!")
    
entering
Hello World!
exiting

Detailed Example:

@context_mgr
def random_value(a=0, b=100):
    rand_num: int = random.randint(a, b)
    print(f"entering {rand_num}")

    # `yield` runs the body of the `with` statement.
    # The yielded value is stored in VAR by `with ... as VAR`
    error = yield rand_num
    # If the body raises an Exception, it's returned here.
    # Otherwise, None is returned

    print(f"exiting {rand_num}, {error=}")
    if isinstance(error, ValueError):
        # Return True to suppress the Exception
        return True


with random_value(-5, -1) as n:
    assert_type(n, int)  # statically typed!
    print(n)

# Parentheses are optional if there are no arguments.
with random_value as n1:

    # Reusable and re-entrant
    with random_value as n2:
        raise ValueError("CoolBeans")

Created for a comment on this mCoding video.

Metadata

Release files for ez-context 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ez-context 1.0.2
File Size Uploaded Provenance Actions
ez_context-1.0.2.tar.gz 3.2 kB Without provenance Details for ez_context-1.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ez-context 1.0.2
File Interpreter ABI Platform Actions
ez_context-1.0.2-py3-none-any.whl Python 3 none any Details for ez_context-1.0.2-py3-none-any.whl

Total release size: 6.7 kB

Release files / ez_context-1.0.2.tar.gz

Download URL ez_context-1.0.2.tar.gz
Size 3.2 kB
Tags Source
SHA-256 checksum
How to use checksums
c6bd97f5d294dc864afbba60c66bc8f41e40ef1e42252ca26ae601a7e0828ffd
BLAKE2b-256 checksum
How to use checksums
9de81e49389f177e0e5a39a4e78822da14346041fbedd57aa253891eed361dc3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.2

Release files / ez_context-1.0.2-py3-none-any.whl

Download URL ez_context-1.0.2-py3-none-any.whl
Size 3.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e5f7b32d98903298fa74962b5967efd4984a04217759055549f5d05d35ed6fd1
BLAKE2b-256 checksum
How to use checksums
fe7a18f171fb4bf3ab3c0f9f3f63b64abf27eebde5d6888733fbbb23483d58e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.2

Security for ez-context 1.0.2

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page