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
If you need to get it manually, or you need the “no traceback” variant, see the Manual Installation section for tips.
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 can be installed manually.
Manual Installation
Depending on your needs, either:
Take one of these files and save it as with_.py:
normal.py for Python implementations that already have the with statement.
from_future_import.py for Python implementations that need the line from __future__ import with_statement to get the with statement.
manual.py for Python implementations that don’t have the with statement.
manual_no_traceback.py for Python implementations that have neither the with statement nor sys.exc_info.
Take the above files that you need, and save them in a folder called with_ along with a custom __init__.py that conditionally imports from the right file as needed.
That way you can always do from with_ import with_ in all of your other code and it’ll just work.
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.4-py26.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 407766cf9efcdb35b8b9236e542bb37abeace7c42d4b4333d7d7ecd2be62603b |
|
MD5 | 18f373fddb3bc9d1ba7647898db0d41c |
|
BLAKE2b-256 | 9386946dc6eef622705360d4d2e329aec956f7a9ad01b04338db4fec831941fd |
Hashes for with_as_a_function-1.0.4-py25-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc37782af2fc864058df07deb1cd69f5662b65d9b55a351eecca5c4038b3309a |
|
MD5 | 2284c6db23e09684a46811070645f772 |
|
BLAKE2b-256 | 82b78fca7105be90a1e1fe59596b28c1735ab96d9c46c500202eeedf9c9d1321 |
Hashes for with_as_a_function-1.0.4-py20-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d9644fbca53da19cd55ff6538fbac280f66b451d2f0fa0541f3834384ca2e46 |
|
MD5 | ad11990fb3d96412cd1a8676018395ad |
|
BLAKE2b-256 | d77ed95876e451756e19f3daf18aade7012a73a655fdb88fae55c54fac2832af |