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()

Async

conditional supports asynchronous context managers:

async with conditional(CONDITION, ASYNCCONTEXTMANAGER()):
    BODY()

Typing

The package ships with type annotations. Type checkers and IDEs can use this information to implement type safety and auto completion.

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

2.1 - 2027-07-04

  • Remove deprecated license classifier. [stefan]

  • Upgrade GitHub workflow. [stefan]

  • Upgrade .readthedocs.yaml. [stefan]

  • Upgrade sphinx and sphinx-rtd-theme in docs extra. [stefan]

2.0 - 2024-05-14

  • Support asynchronous context managers. Thanks to Dawid Wolski. [stefan]

  • Require Python >= 3.5. [stefan]

1.5 - 2023-09-14

  • Add type annotations to the context manager. [stefan]

  • Update tox.ini for latest tox. [stefan]

  • Add GitHub CI workflow. [stefan]

  • Add .readthedocs.yaml file. [stefan]

  • Pin sphinx and sphinx-rtd-theme versions in docs extra. [stefan]

  • Add mypy extra which installs mypy. [stefan]

1.4 - 2022-03-09

  • Add Python 3.8-3.10 to tox.ini. Remove old Python versions. [stefan]

  • Replace deprecated python setup.py test in tox.ini. [stefan]

  • Remove deprecated test_suite and tests_require from setup.py. [stefan]

  • Move metadata to setup.cfg and add a pyproject.toml file. [stefan]

  • Include tests in sdist but not in wheel. [stefan]

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.

Source Distribution

conditional-2.1.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

conditional-2.1-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file conditional-2.1.tar.gz.

File metadata

  • Download URL: conditional-2.1.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.2

File hashes

Hashes for conditional-2.1.tar.gz
Algorithm Hash digest
SHA256 15567332288bbac48a54c8b83ad0880fa026d5dfd95c0ce86f9d959a92cb7a73
MD5 5147a210dd687a59e781f11bfb55d32e
BLAKE2b-256 3344cdc2c8941eea26c6cb0438d55f6d6279f52fa12be0566ed8fe4fb92c9087

See more details on using hashes here.

File details

Details for the file conditional-2.1-py3-none-any.whl.

File metadata

  • Download URL: conditional-2.1-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.2

File hashes

Hashes for conditional-2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 df3fca87ff2798d4b2971250937b0b6f1cb32d85d1b33d6571f8d56a7833e66f
MD5 b5eef9abb9f326ed8fb018658ea69c23
BLAKE2b-256 e4d88d00408d028bbed798fa7f4537d13026c9b20cba4a54bce21087125adb2e

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