Skip to main content

Contexter is a full replacement of the contextlib [1] standard library module. It comes with more features, a nicer API and full support for Python 2.5 up to 3.x from a single source file.

To keep it short: Contexter allows you to nest and stack context managers in an easy and intuitive way.

Enough talk, let’s see an example:

''' Copy the content of one file to another
     and protect everything with a lock. '''

with Contexter(lock) as ctx:
    in_file  = ctx << open('a.txt')
    out_file = ctx << open('b.txt', 'w')
    out_file.write(in_file.read())

Look at that. It’s beautiful, isn’t it? Let me explain: You call Contexter() with any number of context managers as arguments and later attach additional managers with the neat value = ctx << thing syntax. That’s it. Only one level of indentation, no matter how many managers you need.

Just for comparison:

# Python 2.5 and 2.6
with lock:
    with open('a.txt') as in_file:
        with open('b.txt', 'w') as out_file:
            out_file.write(in_file.read())

# Starting with Python 2.7 and 3.2
with lock, open('a.txt') as in_file, open('b.txt', 'w') as out_file:
    out_file.write(in_file.read())

# Deprecated since Python 2.7 and 3.2
with contextlib.nested(lock, open('a.txt'), open('b.txt', 'w')) as values:
    in_file, out_file = values
    out_file.write(in_file.read())

# Since Python 3.3 (not backported to 2.7)
with contextlib.ExitStack() as stack:
    stack.enter_context(lock)
    in_file  = stack.enter_context(open('a.txt'))
    out_file = stack.enter_context(open('b.txt', 'w'))
    out_file.write(in_file.read())

Replacing contextlib.nested [2]

You can use Contexter(*managers) as a drop-in replacement for contextlib.nested(*managers), just without the confusing error prone quirks mentioned in the official documentation.

Replacing contextlib.closing [3]

Just forget about it. Contexter turns close-able objects into context managers automatically.

Replacing contextlib.ExitStack [4]

Contexter offeres everything contextlib.ExitStack does (and more). If you want a drop-in replacement that also works for Python 2.x and 3.2, you can use our backported ExitStack, a subclass of Contexter that is API compatible to the contextlib variant.

Replacing everything else from contextlib

If you really want to stick with the standard API, you can. Contexter implements all public APIs from contextlib and backports new features as soon as they are introduced.

More examples:

Contexter keeps track of the results of all invoked context managers. You can access the results later and don’t have to unpack them all at the beginning.

with Contexter(open('a.txt'), open('b.txt', 'w')) as ctx:
    in_file, out_file = ctx.values()
    assert ctx.value(0) is in_file
    assert ctx[0] is in_file
    assert ctx[0:2] == [in_file, out_file]
    assert len(ctx) == 2

If you don’t like the << syntax, there is a method that does the same.

with Contexter() as ctx:
    in_file = ctx << open('a.txt')
    out_file = ctx.append(open('b.txt', 'w'))

Contexter contexts are nestable. Each level of nesting maintains its own stack of context managers and result values. This allows you to control the lifetime of contexts very precisely.

with Contexter() as ctx:
    out_file = ctx << open('b.txt', 'w')

    with ctx:
        in_file = ctx << open('a.txt')
        copy_data(in_file, out_file)

    assert in_file.closed == True
    assert out_file.closed == False

Release files for contexter 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for contexter 0.1.4
File Size Uploaded
contexter-0.1.4.tar.gz 4.5 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for contexter 0.1.4
File Interpreter ABI Platform
contexter-0.1.4-py2.py3-none-any.whl Python 2, Python 3 none any Details
contexter-0.1.4-py2-none-any.whl Python 2 none any Details

Total release size: 18.5 kB

Release files / contexter-0.1.4.tar.gz

Download URL contexter-0.1.4.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
c730890b1a915051414a6350d8ea1cddca7d01d8f756badedb30b9bf305ea0a8
BLAKE2b-256 checksum
How to use checksums
838efa97d40616c8d1bda8f83b12ab0ccf9fe0420cb89cf73184436bd3c581fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / contexter-0.1.4-py2.py3-none-any.whl

Download URL contexter-0.1.4-py2.py3-none-any.whl
Size 7.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
9d268cf13dfef3c1eb28d791b650b959126c500abc5b6ad6349ec502ac32a19c
BLAKE2b-256 checksum
How to use checksums
517dec4d7e39f2d941b37a8d629c9c5da5f350088ea8e7e145c338753739b04a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / contexter-0.1.4-py2-none-any.whl

Download URL contexter-0.1.4-py2-none-any.whl
Size 7.0 kB
Tags Python 2
SHA-256 checksum
How to use checksums
36a3a6bb80c6e4bab33ec98c6f394fa8deb433eb2154346e4244deef44230822
BLAKE2b-256 checksum
How to use checksums
840f6368331608d39498fac773e22d4de23c304181f9718b6065161a555e0c1b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.4 This release

3 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page