Skip to main content

Contexter is a full replacement of the contextlib 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.

Project description

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

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

contexter-0.1.3.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

contexter-0.1.3-py2-none-any.whl (7.0 kB view details)

Uploaded Python 2

File details

Details for the file contexter-0.1.3.tar.gz.

File metadata

  • Download URL: contexter-0.1.3.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for contexter-0.1.3.tar.gz
Algorithm Hash digest
SHA256 16b1999d24fb80c98fa4d875904d4425db2aa24d9378242704ca27a1bc9c3677
MD5 437efd28f5489cccfe929c08c6b269aa
BLAKE2b-256 f4a4a42a6401bfe2a05fe63e328a8e1b37881e4c286a8029fec1577949d6a8b5

See more details on using hashes here.

Provenance

File details

Details for the file contexter-0.1.3-py2-none-any.whl.

File metadata

File hashes

Hashes for contexter-0.1.3-py2-none-any.whl
Algorithm Hash digest
SHA256 da992ea91e82d814ce17c8eaf54d3444242783aa9c13de4155b6b96026e94bef
MD5 f0628758b9a70bcb9febb11029655e07
BLAKE2b-256 bb69a13bdcd956a4fa425009f3e28e480a50ffc6ba440045d1e4e1159040c366

See more details on using hashes here.

Provenance

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