conditional


Nameconditional JSON
Version 2.0 PyPI version JSON
download
home_pagehttps://github.com/stefanholek/conditional
SummaryConditionally enter a context manager
upload_time2024-05-14 10:47:53
maintainerNone
docs_urlhttps://pythonhosted.org/conditional/
authorStefan H. Holek
requires_python>=3.5
licenseBSD-2-Clause
keywords conditional context manager contextmanager with async async with enter exit
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ===========
conditional
===========
-------------------------------------------------------------------
Conditionally enter a context manager
-------------------------------------------------------------------

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:

.. code-block:: python

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

Consider replacing it with:

.. code-block:: python

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

Async
=====

`conditional` supports asynchronous context managers:

.. code-block:: python

    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:

.. code-block:: python

    from conditional import conditional

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

Documentation
=============

For further details please refer to the `API Documentation`_.

.. _`API Documentation`: https://conditional.readthedocs.io/en/stable/


Changelog
=========

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]

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/stefanholek/conditional",
    "name": "conditional",
    "maintainer": null,
    "docs_url": "https://pythonhosted.org/conditional/",
    "requires_python": ">=3.5",
    "maintainer_email": null,
    "keywords": "conditional, context manager, contextmanager, with, async, async with, enter, exit",
    "author": "Stefan H. Holek",
    "author_email": "stefan@epy.co.at",
    "download_url": "https://files.pythonhosted.org/packages/e6/b6/0fe3956c1eab81a77fb61c206fe1d09ce1b2a7e8a6957f379d7bead13d38/conditional-2.0.tar.gz",
    "platform": null,
    "description": "===========\nconditional\n===========\n-------------------------------------------------------------------\nConditionally enter a context manager\n-------------------------------------------------------------------\n\nPackage Contents\n================\n\nconditional(condition, contextmanager)\n    Enter contextmanager only if condition is true.\n\nOverview\n========\n\nThe `conditional` context manager comes handy when you always want to\nexecute a with-block but only conditionally want to apply its context\nmanager.\n\nIf you find yourself writing code like this:\n\n.. code-block:: python\n\n    if CONDITION:\n        with CONTEXTMANAGER():\n            BODY()\n    else:\n        BODY()\n\nConsider replacing it with:\n\n.. code-block:: python\n\n    with conditional(CONDITION, CONTEXTMANAGER()):\n        BODY()\n\nAsync\n=====\n\n`conditional` supports asynchronous context managers:\n\n.. code-block:: python\n\n    async with conditional(CONDITION, ASYNCCONTEXTMANAGER()):\n        BODY()\n\nTyping\n======\n\nThe package ships with type annotations. Type checkers and IDEs can\nuse this information to implement type safety and auto completion.\n\nExamples\n========\n\nSay we want to ignore signals when a pager application is in the\nforeground, but not otherwise:\n\n.. code-block:: python\n\n    from conditional import conditional\n\n    with conditional(has_pager(cmd), ignoresignals()):\n        os.system(cmd)\n\nDocumentation\n=============\n\nFor further details please refer to the `API Documentation`_.\n\n.. _`API Documentation`: https://conditional.readthedocs.io/en/stable/\n\n\nChangelog\n=========\n\n2.0 - 2024-05-14\n----------------\n\n- Support asynchronous context managers. Thanks to Dawid Wolski.\n  [stefan]\n\n- Require Python >= 3.5.\n  [stefan]\n\n1.5 - 2023-09-14\n----------------\n\n- Add type annotations to the context manager.\n  [stefan]\n\n- Update tox.ini for latest tox.\n  [stefan]\n\n- Add GitHub CI workflow.\n  [stefan]\n\n- Add .readthedocs.yaml file.\n  [stefan]\n\n- Pin sphinx and sphinx-rtd-theme versions in docs extra.\n  [stefan]\n\n- Add mypy extra which installs mypy.\n  [stefan]\n\n1.4 - 2022-03-09\n----------------\n\n- Add Python 3.8-3.10 to tox.ini. Remove old Python versions.\n  [stefan]\n\n- Replace deprecated ``python setup.py test`` in tox.ini.\n  [stefan]\n\n- Remove deprecated ``test_suite`` and ``tests_require`` from setup.py.\n  [stefan]\n\n- Move metadata to setup.cfg and add a pyproject.toml file.\n  [stefan]\n\n- Include tests in sdist but not in wheel.\n  [stefan]\n\n1.3 - 2019-01-28\n----------------\n\n- Add MANIFEST.in.\n  [stefan]\n\n- Release as wheel.\n  [stefan]\n\n1.2 - 2017-02-05\n----------------\n\n- Support Python 2.6-3.6 without 2to3.\n  [stefan]\n\n- Add a LICENSE file.\n  [stefan]\n\n1.1 - 2014-04-19\n----------------\n\n- Remove setuptools from install_requires because it isn't.\n  [stefan]\n\n1.0 - 2012-05-16\n----------------\n\n- Initial release.\n  [stefan]\n",
    "bugtrack_url": null,
    "license": "BSD-2-Clause",
    "summary": "Conditionally enter a context manager",
    "version": "2.0",
    "project_urls": {
        "Documentation": "https://conditional.readthedocs.io/en/stable/",
        "Homepage": "https://github.com/stefanholek/conditional"
    },
    "split_keywords": [
        "conditional",
        " context manager",
        " contextmanager",
        " with",
        " async",
        " async with",
        " enter",
        " exit"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "673ae5cc2452e8447a5c6bb7cb329137677d2cce593eaa11be72a6548fb6e755",
                "md5": "954e636a9e9f0437ca507761b596ef76",
                "sha256": "8c98299df54486f6e8db36ddfecbc441d0b0bda92d394f2e32b7dd10d63db7f3"
            },
            "downloads": -1,
            "filename": "conditional-2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "954e636a9e9f0437ca507761b596ef76",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.5",
            "size": 4695,
            "upload_time": "2024-05-14T10:47:52",
            "upload_time_iso_8601": "2024-05-14T10:47:52.007407Z",
            "url": "https://files.pythonhosted.org/packages/67/3a/e5cc2452e8447a5c6bb7cb329137677d2cce593eaa11be72a6548fb6e755/conditional-2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e6b60fe3956c1eab81a77fb61c206fe1d09ce1b2a7e8a6957f379d7bead13d38",
                "md5": "c8a452a7d6300541c55721c5771d7d53",
                "sha256": "7e6745ca293976bf1bde5e993180aa0bb095814c7af9c517ecc6d0ed0b841096"
            },
            "downloads": -1,
            "filename": "conditional-2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "c8a452a7d6300541c55721c5771d7d53",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.5",
            "size": 7957,
            "upload_time": "2024-05-14T10:47:53",
            "upload_time_iso_8601": "2024-05-14T10:47:53.359320Z",
            "url": "https://files.pythonhosted.org/packages/e6/b6/0fe3956c1eab81a77fb61c206fe1d09ce1b2a7e8a6957f379d7bead13d38/conditional-2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-14 10:47:53",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "stefanholek",
    "github_project": "conditional",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "conditional"
}
        
Elapsed time: 0.51307s