skip to navigation
skip to content

Not Logged In

conditional 1.1

Conditionally enter a context manager

Package Documentation

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]
 
File Type Py Version Uploaded on Size
conditional-1.1.zip (md5) Source 2014-04-19 11KB
  • Downloads (All Versions):
  • 33 downloads in the last day
  • 156 downloads in the last week
  • 485 downloads in the last month