Skip to main content

Conditionally enter a context manager

Project description

Package Contents

conditional(condition, contextmanager)
Enter contextmanager only if condition is true.

Overview

The conditional context manager comes handy when you always want to execute a with-block but only conditionally want to apply its context manager.

If you find yourself writing code like this:

if CONDITION:
    with CONTEXTMANAGER():
        BODY()
else:
    BODY()

Consider replacing it with:

with conditional(CONDITION, CONTEXTMANAGER()):
    BODY()

Examples

Say we want to ignore signals when a pager application is in the foreground, but not otherwise:

from conditional import conditional

with conditional(has_pager(cmd), ignoresignals()):
    os.system(cmd)

Documentation

For further details please refer to the API Documentation.

Changelog

1.3 - 2019-01-28

  • Add MANIFEST.in. [stefan]
  • Release as wheel. [stefan]

1.2 - 2017-02-05

  • Support Python 2.6-3.6 without 2to3. [stefan]
  • Add a LICENSE file. [stefan]

1.1 - 2014-04-19

  • Remove setuptools from install_requires because it isn’t. [stefan]

1.0 - 2012-05-16

  • Initial release. [stefan]

Project details


Download files

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

Files for conditional, version 1.3
Filename, size File type Python version Upload date Hashes
Filename, size conditional-1.3-py2.py3-none-any.whl (4.4 kB) File type Wheel Python version 3.7 Upload date Hashes View hashes
Filename, size conditional-1.3.zip (8.3 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page