Use context managers with a function instead of a statement.
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file with-as-a-function-1.1.0.tar.gz
.
File metadata
- Download URL: with-as-a-function-1.1.0.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 933da4367185a5265ca5e5a7f34b96c9a4f3a408a3cf5d4c92229ea395bb5165 |
|
MD5 | 3c9be07b44054b270e3639002eb71cd4 |
|
BLAKE2b-256 | 13b8f93db6505df9b1965496be20b6b0cba88e130978392127e09355444e2553 |
File details
Details for the file with_as_a_function-1.1.0-py33-none-any.whl
.
File metadata
- Download URL: with_as_a_function-1.1.0-py33-none-any.whl
- Upload date:
- Size: 3.5 kB
- Tags: Python 3.3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 81d2fcd4a6e4daa2fc6837774720170c2d7d607ea51d9a321ca014f3c68d70a5 |
|
MD5 | 66949c2f5121ab6b0cb01fcc384deff6 |
|
BLAKE2b-256 | 5640854ec4b4fab059635656ee4370bce2ca5d70c796eb1cfc0cd6c054b64564 |
File details
Details for the file with_as_a_function-1.1.0-py26.py30-none-any.whl
.
File metadata
- Download URL: with_as_a_function-1.1.0-py26.py30-none-any.whl
- Upload date:
- Size: 3.8 kB
- Tags: Python 2.6, Python 3.0
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c3933e1882002e10e0d53812eaf5d757ccbc2ee478b89c0c456ab8ac17f40b7 |
|
MD5 | 817098af24cbe610b33d8e949faad14e |
|
BLAKE2b-256 | 3c474a69b0dacd1439f71b7629860433b008a2bef024276dfc873e9487a057d5 |
File details
Details for the file with_as_a_function-1.1.0-py25-none-any.whl
.
File metadata
- Download URL: with_as_a_function-1.1.0-py25-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 2.5
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f40c6e3d9457c028f7a9c8a21bc5df7ba6fe46fb68ad19a3592031517b63be4a |
|
MD5 | 4a0973edd14d46cc2c945c062a8647a0 |
|
BLAKE2b-256 | 424f1cdc260f6431ee2d2f0a9cd04457d8e8a3ea0b56dfc3929a3f18c2339ec4 |
File details
Details for the file with_as_a_function-1.1.0-py22-none-any.whl
.
File metadata
- Download URL: with_as_a_function-1.1.0-py22-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 2.2
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f0bd2c03ba0f3cd8c4e0b50e22a46b1a43f4a50eb677bee858fa892bcd78dad6 |
|
MD5 | daffe9b9465c4b1f2457ec3fde5d58b6 |
|
BLAKE2b-256 | e60ef7b61d4b19c1f46a9b2c960d6578402287c6509cbad6c39a67109a4616f6 |