Skip to main content

Use context managers with a function instead of a statement.

Provides a minimal and portable interface for using context managers with all the advantages of functions over syntax.

Allows using context managers on Python implementations that are too old or too incomplete to have the with statement.

Versioning

This library’s version numbers follow the SemVer 2.0.0 specification.

Installation

pip install with-as-a-function

Usage

Import with_, iwith, or both:

from with_ import with_, iwith

with_ wraps a function in a context manager. For example,

data = with_(open('my_file.txt'), lambda my_file: my_file.read())

is similar to:

with open('my_file.txt') as my_file:
    data = my_file.read()

iwith wraps a generator or other iterable in a context manager. For example,

lines = iwith(open('my_file.txt'), lambda my_file: my_file)

is similar to:

def _lines():
    with open('my_file.txt') as my_file:
        yield from my_file
lines = _lines()

And of course because with_ and iwith are functions, you can combine them with functools.partial and other functional programming libraries and techniques for many more uses.

Portability

Portable to all releases of Python 3, and releases of Python 2 starting with 2.2.

Even those without the with statement and without the yield from expression.

For popular Python reimplementations with quicks or bugs that make the normal implementation of this module not work, other implementations are included in the source distribution.

Metadata

Release files for with-as-a-function 1.1.0

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

Source distribution (sdist)

Source distribution for with-as-a-function 1.1.0
File Size Uploaded Provenance
with-as-a-function-1.1.0.tar.gz 8.1 kB Without provenance Details

Built distributions (wheels)

Table of built distributions (wheels) for with-as-a-function 1.1.0
File
with_as_a_function-1.1.0-py33-none-any.whl Python 3.3 none any Details
with_as_a_function-1.1.0-py26.py30-none-any.whl Python 2.6, Python 3.0 none any Details
with_as_a_function-1.1.0-py25-none-any.whl Python 2.5 none any Details
with_as_a_function-1.1.0-py22-none-any.whl Python 2.2 none any Details

Total release size: 23.7 kB

Release files / with-as-a-function-1.1.0.tar.gz

Download URL with-as-a-function-1.1.0.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
933da4367185a5265ca5e5a7f34b96c9a4f3a408a3cf5d4c92229ea395bb5165
BLAKE2b-256 checksum
How to use checksums
13b8f93db6505df9b1965496be20b6b0cba88e130978392127e09355444e2553
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.2

Release files / with_as_a_function-1.1.0-py33-none-any.whl

Download URL with_as_a_function-1.1.0-py33-none-any.whl
Size 3.5 kB
Tags Python 3.3
SHA-256 checksum
How to use checksums
81d2fcd4a6e4daa2fc6837774720170c2d7d607ea51d9a321ca014f3c68d70a5
BLAKE2b-256 checksum
How to use checksums
5640854ec4b4fab059635656ee4370bce2ca5d70c796eb1cfc0cd6c054b64564
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.2

Release files / with_as_a_function-1.1.0-py26.py30-none-any.whl

Download URL with_as_a_function-1.1.0-py26.py30-none-any.whl
Size 3.8 kB
Tags Python 2.6 Python 3.0
SHA-256 checksum
How to use checksums
8c3933e1882002e10e0d53812eaf5d757ccbc2ee478b89c0c456ab8ac17f40b7
BLAKE2b-256 checksum
How to use checksums
3c474a69b0dacd1439f71b7629860433b008a2bef024276dfc873e9487a057d5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.2

Release files / with_as_a_function-1.1.0-py25-none-any.whl

Download URL with_as_a_function-1.1.0-py25-none-any.whl
Size 3.9 kB
Tags Python 2.5
SHA-256 checksum
How to use checksums
f40c6e3d9457c028f7a9c8a21bc5df7ba6fe46fb68ad19a3592031517b63be4a
BLAKE2b-256 checksum
How to use checksums
424f1cdc260f6431ee2d2f0a9cd04457d8e8a3ea0b56dfc3929a3f18c2339ec4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.2

Release files / with_as_a_function-1.1.0-py22-none-any.whl

Download URL with_as_a_function-1.1.0-py22-none-any.whl
Size 4.3 kB
Tags Python 2.2
SHA-256 checksum
How to use checksums
f0bd2c03ba0f3cd8c4e0b50e22a46b1a43f4a50eb677bee858fa892bcd78dad6
BLAKE2b-256 checksum
How to use checksums
e60ef7b61d4b19c1f46a9b2c960d6578402287c6509cbad6c39a67109a4616f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.2

Security for with-as-a-function 1.1.0

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.

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