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_:
from with_ import with_
With it we can do things like this:
data = with_(open('my_file.txt'), lambda my_file: my_file.read())
Which is similar to:
with open('my_file.txt') as my_file:
data = my_file.read()
And of course because with_ is a function, you can combine it with functools.partial and other functional programming libraries and techniques for many more uses.
Portability
Portable to all releases of both Python 3 and Python 2.
Even those without the with statement.
(The oldest tested is 2.5, but it will likely work on all Python 2 versions and probably on even earlier versions.)
For Python implementations that neither support the with statement nor have sys.exc_info, a “no traceback” variant is included in the source that can be installed manually.
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
Hashes for with_as_a_function-1.0.9-py26.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a9d4c175fdd2d92106d77a56d32df6c7f3b458d69a0f38f3c073cd17b757b181 |
|
MD5 | d845b77821f3ba9de9e788da27359990 |
|
BLAKE2b-256 | f8db537c6d3aa76aae405fc18910e28e0b9f87673b4d3c448157bd0c75d9768c |
Hashes for with_as_a_function-1.0.9-py25-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd1b81af50428b85506fccc41547185d0415db23d18235dd7ea73d24515da4b5 |
|
MD5 | 13a6392e2c3070408c9ce82d7cd3d428 |
|
BLAKE2b-256 | f1ea09279444c145d18c836fce12185e186ed51a606df0695328c28b413f535d |
Hashes for with_as_a_function-1.0.9-py20-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ba3bd3ac39e97abc66c81791870f08ebbe3fcc48d7356f8f4d12a5221e61e04 |
|
MD5 | ce3ae06a2f3c49398b692bd592c45b1c |
|
BLAKE2b-256 | ffbf1c5e90d2a10abde4a3fa2c8d707e54926fba8235d2703d3001f5eafe495a |