Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

with-as-a-function-1.0.9.tar.gz (4.1 kB view hashes)

Uploaded Source

Built Distributions

with_as_a_function-1.0.9-py26.py3-none-any.whl (3.2 kB view hashes)

Uploaded Python 2.6 Python 3

with_as_a_function-1.0.9-py25-none-any.whl (3.2 kB view hashes)

Uploaded Python 2.5

with_as_a_function-1.0.9-py20-none-any.whl (3.4 kB view hashes)

Uploaded Python 2.0

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page