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.8-py26.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1dcac17d6e90c40f72787802d196cd6db05e93458c965292ca0b9acacabfcd67 |
|
MD5 | dde1410328b0145144f67666c5a28a3c |
|
BLAKE2b-256 | f195dcebe44f1bf3c199351d7b457609ee0cf2de085169e6bf38da0fed961076 |
Hashes for with_as_a_function-1.0.8-py25-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 71321b98534f6a84aba1ab9dbb584aee2f5ad9752197cfe1897e01344a67b058 |
|
MD5 | 2b0a5f4676218507ceeaa2beb1db8d7c |
|
BLAKE2b-256 | 44a55b48e37ebce8ec7dcec221d536094a2abbe64e469a13924afc8f889a2727 |
Hashes for with_as_a_function-1.0.8-py20-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c412114e5c4f7895dd68199c3442a69fc2b052c731b0dcf7065aec7ef662a121 |
|
MD5 | 8a35fb146b10900d9c576dce1f375ccb |
|
BLAKE2b-256 | 6b5f27646719281f78a1aa84b5a0554d10a5634910ffacfb4768c66f0778eb9f |