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)

Changelog

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.

Source Distribution

conditional-1.1.zip (12.0 kB view details)

Uploaded Source

File details

Details for the file conditional-1.1.zip.

File metadata

  • Download URL: conditional-1.1.zip
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for conditional-1.1.zip
Algorithm Hash digest
SHA256 5553104de2b076ae6425a98c80e5c497de484f6041124519d6d1f7314bc8f79f
MD5 dd794ae0c0e4c527c9f43dd622f7ad8a
BLAKE2b-256 d2adf35e70d150b022bfe8272758d7d4b0963b5cb8fd5a26133d6f6adb3973b3

See more details on using hashes here.

Supported by

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