Name | barriers JSON |
Version |
1.2.0
JSON |
| download |
home_page | |
Summary | Python decorators for including/excluding type checks, value/bounds checks, and other code blocks within the compiled bytecode of functions and methods. |
upload_time | 2023-10-28 04:24:11 |
maintainer | |
docs_url | None |
author | Andrei Lapets |
requires_python | >=3.7 |
license | MIT |
keywords |
|
VCS |
 |
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
========
barriers
========
Python decorators for including/excluding type checks, value/bounds checks, and other code blocks within the compiled bytecode of functions and methods.
|pypi| |readthedocs| |actions| |coveralls|
.. |pypi| image:: https://badge.fury.io/py/barriers.svg
:target: https://badge.fury.io/py/barriers
:alt: PyPI version and link.
.. |readthedocs| image:: https://readthedocs.org/projects/barriers/badge/?version=latest
:target: https://barriers.readthedocs.io/en/latest/?badge=latest
:alt: Read the Docs documentation status.
.. |actions| image:: https://github.com/reity/barriers/workflows/lint-test-cover-docs/badge.svg
:target: https://github.com/reity/barriers/actions/workflows/lint-test-cover-docs.yml
:alt: GitHub Actions status.
.. |coveralls| image:: https://coveralls.io/repos/github/reity/barriers/badge.svg?branch=main
:target: https://coveralls.io/github/reity/barriers?branch=main
:alt: Coveralls test coverage summary.
Installation and Usage
----------------------
This library is available as a `package on PyPI <https://pypi.org/project/barriers>`__:
.. code-block:: bash
python -m pip install barriers
The library can be imported in the usual ways:
.. code-block:: python
import barriers
from barriers import barriers
Examples
^^^^^^^^
.. |barriers| replace:: ``barriers``
.. _barriers: https://barriers.readthedocs.io/en/1.1.0/_source/barriers.html#barriers.barriers.barriers
.. |globals| replace:: ``globals``
.. _globals: https://docs.python.org/3/library/functions.html#globals
Consider the function below (defined within a file ``f.py``). The body of this function contains a code block that raises an exception if either of the two inputs is a negative integer:
.. code-block:: python
def f(x: int, y: int) -> int:
if x < 0 or y < 0:
raise ValueError('inputs must be nonnegative')
return x + y
We can import the module above and invoke the function to observe its behavior:
.. code-block:: python
>>> from f import f
>>> f(1, 2)
3
>>> f(-1, -2)
Traceback (most recent call last):
...
ValueError: inputs must be nonnegative
An instance of the |barriers|_ class should normally be introduced near the top of a Python module using a pair of statements such as those below:
.. code-block:: python
>>> from barriers import barriers
>>> example = barriers(False) @ globals() # Remove marked code blocks (i.e., "disable barriers").
The |barriers|_ instance ``example`` defined above is a decorator that transforms any decorated function by removing any designated code blocks in the body of that function.
* The ``False`` argument in the expression ``barriers(False)`` above should be interpreted to mean that **this barrier is disabled** (*i.e.*, that the marked code blocks in the bodies of functions decorated by this decorator **should be removed**). The default value for this optional argument is ``True``; this should be interpreted to mean that **this barrier is enabled** (and, thus, that marked code blocks **should not be removed** from decorated functions).
* The notation ``@ globals()`` ensures that the namespace returned by |globals|_ is used when compiling the abstract syntax trees of transformed functions.
Consider the modified version of ``f.py`` below. A statement can be designated for automatic removal by placing a marker -- in this case, the ``example`` variable -- on the line directly above that statement. Note that in the body of the function ``f`` defined below, the ``if`` block is immediately preceded by a line that contains the variable ``example``:
.. code-block:: python
from barriers import barriers
example = barriers(False) @ globals()
@example
def f(x: int, y: int) -> int:
example
if x < 0 or y < 0:
raise ValueError('inputs must be nonnegative')
return x + y
The decorator ``@example`` automatically removes the ``if`` block in the function above. As a result, the function does not raise an exception when it is applied to negative inputs:
.. code-block:: python
>>> from f import f
>>> f(1, 2)
3
>>> f(-1, -2)
-3
It is also possible to define and use individually named markers (which are created as attributes of the |barriers|_ instance):
.. code-block:: python
>>> from barriers import barriers
>>> checks = barriers(types=True, bounds=False) @ globals()
Given the above definitions, it is possible to introduce named markers such as those in the variant of ``f.py`` presented below. When a marker definition has been assigned ``True``, the statements immediately below that named marker **are not removed** (*i.e.*, the marked barrier statements are enabled). When a marker definition has been assigned ``False``, the corresponding marked statements **are removed**:
.. code-block:: python
from barriers import barriers
checks = barriers(types=True, bounds=False) @ globals()
@checks
def f(x: int, y: int) -> int:
checks.types
if not isinstance(x, int) and not isinstance(y, int):
raise TypeError('inputs must be integers')
checks.bounds
if x < 0 or y < 0:
raise ValueError('inputs must be nonnegative')
return x + y
The described behavior can be observed when evaluating the function:
.. code-block:: python
>>> from f import f
>>> f('a', 'b')
Traceback (most recent call last):
...
TypeError: inputs must be integers
>>> f(-1, -2)
-3
Many additional details and examples are presented in the `documentation <https://barriers.readthedocs.io/en/1.1.0>`__.
Development
-----------
All installation and development dependencies are fully specified in ``pyproject.toml``. The ``project.optional-dependencies`` object is used to `specify optional requirements <https://peps.python.org/pep-0621>`__ for various development tasks. This makes it possible to specify additional options (such as ``docs``, ``lint``, and so on) when performing installation using `pip <https://pypi.org/project/pip>`__:
.. code-block:: bash
python -m pip install .[docs,lint]
Documentation
^^^^^^^^^^^^^
The documentation can be generated automatically from the source files using `Sphinx <https://www.sphinx-doc.org>`__:
.. code-block:: bash
python -m pip install .[docs]
cd docs
sphinx-apidoc -f -E --templatedir=_templates -o _source .. && make html
Testing and Conventions
^^^^^^^^^^^^^^^^^^^^^^^
All unit tests are executed and their coverage is measured when using `pytest <https://docs.pytest.org>`__ (see the ``pyproject.toml`` file for configuration details):
.. code-block:: bash
python -m pip install .[test]
python -m pytest
Alternatively, all unit tests are included in the module itself and can be executed using `doctest <https://docs.python.org/3/library/doctest.html>`__:
.. code-block:: bash
python src/barriers/barriers.py -v
Style conventions are enforced using `Pylint <https://pylint.readthedocs.io>`__:
.. code-block:: bash
python -m pip install .[lint]
python -m pylint src/barriers
Contributions
^^^^^^^^^^^^^
In order to contribute to the source code, open an issue or submit a pull request on the `GitHub page <https://github.com/reity/barriers>`__ for this library.
Versioning
^^^^^^^^^^
The version number format for this library and the changes to the library associated with version number increments conform with `Semantic Versioning 2.0.0 <https://semver.org/#semantic-versioning-200>`__.
Publishing
^^^^^^^^^^
This library can be published as a `package on PyPI <https://pypi.org/project/barriers>`__ by a package maintainer. First, install the dependencies required for packaging and publishing:
.. code-block:: bash
python -m pip install .[publish]
Ensure that the correct version number appears in ``pyproject.toml``, and that any links in this README document to the Read the Docs documentation of this package (or its dependencies) have appropriate version numbers. Also ensure that the Read the Docs project for this library has an `automation rule <https://docs.readthedocs.io/en/stable/automation-rules.html>`__ that activates and sets as the default all tagged versions. Create and push a tag for this version (replacing ``?.?.?`` with the version number):
.. code-block:: bash
git tag ?.?.?
git push origin ?.?.?
Remove any old build/distribution files. Then, package the source into a distribution archive:
.. code-block:: bash
rm -rf build dist src/*.egg-info
python -m build --sdist --wheel .
Finally, upload the package distribution archive to `PyPI <https://pypi.org>`__:
.. code-block:: bash
python -m twine upload dist/*
Raw data
{
"_id": null,
"home_page": "",
"name": "barriers",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "",
"author": "Andrei Lapets",
"author_email": "a@lapets.io",
"download_url": "https://files.pythonhosted.org/packages/e4/43/ba276fa8a1e4db8e76d4d8efae6bd79175d201543cac9a64556b3eb1545f/barriers-1.2.0.tar.gz",
"platform": null,
"description": "========\r\nbarriers\r\n========\r\n\r\nPython decorators for including/excluding type checks, value/bounds checks, and other code blocks within the compiled bytecode of functions and methods.\r\n\r\n|pypi| |readthedocs| |actions| |coveralls|\r\n\r\n.. |pypi| image:: https://badge.fury.io/py/barriers.svg\r\n :target: https://badge.fury.io/py/barriers\r\n :alt: PyPI version and link.\r\n\r\n.. |readthedocs| image:: https://readthedocs.org/projects/barriers/badge/?version=latest\r\n :target: https://barriers.readthedocs.io/en/latest/?badge=latest\r\n :alt: Read the Docs documentation status.\r\n\r\n.. |actions| image:: https://github.com/reity/barriers/workflows/lint-test-cover-docs/badge.svg\r\n :target: https://github.com/reity/barriers/actions/workflows/lint-test-cover-docs.yml\r\n :alt: GitHub Actions status.\r\n\r\n.. |coveralls| image:: https://coveralls.io/repos/github/reity/barriers/badge.svg?branch=main\r\n :target: https://coveralls.io/github/reity/barriers?branch=main\r\n :alt: Coveralls test coverage summary.\r\n\r\nInstallation and Usage\r\n----------------------\r\nThis library is available as a `package on PyPI <https://pypi.org/project/barriers>`__:\r\n\r\n.. code-block:: bash\r\n\r\n python -m pip install barriers\r\n\r\nThe library can be imported in the usual ways:\r\n\r\n.. code-block:: python\r\n\r\n import barriers\r\n from barriers import barriers\r\n\r\nExamples\r\n^^^^^^^^\r\n\r\n.. |barriers| replace:: ``barriers``\r\n.. _barriers: https://barriers.readthedocs.io/en/1.1.0/_source/barriers.html#barriers.barriers.barriers\r\n\r\n.. |globals| replace:: ``globals``\r\n.. _globals: https://docs.python.org/3/library/functions.html#globals\r\n\r\nConsider the function below (defined within a file ``f.py``). The body of this function contains a code block that raises an exception if either of the two inputs is a negative integer:\r\n\r\n.. code-block:: python\r\n\r\n def f(x: int, y: int) -> int:\r\n\r\n if x < 0 or y < 0:\r\n raise ValueError('inputs must be nonnegative')\r\n\r\n return x + y\r\n\r\nWe can import the module above and invoke the function to observe its behavior:\r\n\r\n.. code-block:: python\r\n\r\n >>> from f import f\r\n >>> f(1, 2)\r\n 3\r\n >>> f(-1, -2)\r\n Traceback (most recent call last):\r\n ...\r\n ValueError: inputs must be nonnegative\r\n\r\nAn instance of the |barriers|_ class should normally be introduced near the top of a Python module using a pair of statements such as those below:\r\n\r\n.. code-block:: python\r\n\r\n >>> from barriers import barriers\r\n >>> example = barriers(False) @ globals() # Remove marked code blocks (i.e., \"disable barriers\").\r\n\r\nThe |barriers|_ instance ``example`` defined above is a decorator that transforms any decorated function by removing any designated code blocks in the body of that function.\r\n\r\n* The ``False`` argument in the expression ``barriers(False)`` above should be interpreted to mean that **this barrier is disabled** (*i.e.*, that the marked code blocks in the bodies of functions decorated by this decorator **should be removed**). The default value for this optional argument is ``True``; this should be interpreted to mean that **this barrier is enabled** (and, thus, that marked code blocks **should not be removed** from decorated functions).\r\n\r\n* The notation ``@ globals()`` ensures that the namespace returned by |globals|_ is used when compiling the abstract syntax trees of transformed functions.\r\n\r\nConsider the modified version of ``f.py`` below. A statement can be designated for automatic removal by placing a marker -- in this case, the ``example`` variable -- on the line directly above that statement. Note that in the body of the function ``f`` defined below, the ``if`` block is immediately preceded by a line that contains the variable ``example``:\r\n\r\n.. code-block:: python\r\n\r\n from barriers import barriers\r\n example = barriers(False) @ globals()\r\n\r\n @example\r\n def f(x: int, y: int) -> int:\r\n\r\n example\r\n if x < 0 or y < 0:\r\n raise ValueError('inputs must be nonnegative')\r\n\r\n return x + y\r\n\r\nThe decorator ``@example`` automatically removes the ``if`` block in the function above. As a result, the function does not raise an exception when it is applied to negative inputs:\r\n\r\n.. code-block:: python\r\n\r\n >>> from f import f\r\n >>> f(1, 2)\r\n 3\r\n >>> f(-1, -2)\r\n -3\r\n\r\nIt is also possible to define and use individually named markers (which are created as attributes of the |barriers|_ instance):\r\n\r\n.. code-block:: python\r\n\r\n >>> from barriers import barriers\r\n >>> checks = barriers(types=True, bounds=False) @ globals()\r\n\r\nGiven the above definitions, it is possible to introduce named markers such as those in the variant of ``f.py`` presented below. When a marker definition has been assigned ``True``, the statements immediately below that named marker **are not removed** (*i.e.*, the marked barrier statements are enabled). When a marker definition has been assigned ``False``, the corresponding marked statements **are removed**:\r\n\r\n.. code-block:: python\r\n\r\n from barriers import barriers\r\n checks = barriers(types=True, bounds=False) @ globals()\r\n\r\n @checks\r\n def f(x: int, y: int) -> int:\r\n \r\n checks.types\r\n if not isinstance(x, int) and not isinstance(y, int):\r\n raise TypeError('inputs must be integers')\r\n \r\n checks.bounds\r\n if x < 0 or y < 0:\r\n raise ValueError('inputs must be nonnegative')\r\n \r\n return x + y\r\n\r\nThe described behavior can be observed when evaluating the function:\r\n\r\n.. code-block:: python\r\n\r\n >>> from f import f\r\n >>> f('a', 'b')\r\n Traceback (most recent call last):\r\n ...\r\n TypeError: inputs must be integers\r\n >>> f(-1, -2)\r\n -3\r\n\r\nMany additional details and examples are presented in the `documentation <https://barriers.readthedocs.io/en/1.1.0>`__.\r\n\r\nDevelopment\r\n-----------\r\nAll installation and development dependencies are fully specified in ``pyproject.toml``. The ``project.optional-dependencies`` object is used to `specify optional requirements <https://peps.python.org/pep-0621>`__ for various development tasks. This makes it possible to specify additional options (such as ``docs``, ``lint``, and so on) when performing installation using `pip <https://pypi.org/project/pip>`__:\r\n\r\n.. code-block:: bash\r\n\r\n python -m pip install .[docs,lint]\r\n\r\nDocumentation\r\n^^^^^^^^^^^^^\r\nThe documentation can be generated automatically from the source files using `Sphinx <https://www.sphinx-doc.org>`__:\r\n\r\n.. code-block:: bash\r\n\r\n python -m pip install .[docs]\r\n cd docs\r\n sphinx-apidoc -f -E --templatedir=_templates -o _source .. && make html\r\n\r\nTesting and Conventions\r\n^^^^^^^^^^^^^^^^^^^^^^^\r\nAll unit tests are executed and their coverage is measured when using `pytest <https://docs.pytest.org>`__ (see the ``pyproject.toml`` file for configuration details):\r\n\r\n.. code-block:: bash\r\n\r\n python -m pip install .[test]\r\n python -m pytest\r\n\r\nAlternatively, all unit tests are included in the module itself and can be executed using `doctest <https://docs.python.org/3/library/doctest.html>`__:\r\n\r\n.. code-block:: bash\r\n\r\n python src/barriers/barriers.py -v\r\n\r\nStyle conventions are enforced using `Pylint <https://pylint.readthedocs.io>`__:\r\n\r\n.. code-block:: bash\r\n\r\n python -m pip install .[lint]\r\n python -m pylint src/barriers\r\n\r\nContributions\r\n^^^^^^^^^^^^^\r\nIn order to contribute to the source code, open an issue or submit a pull request on the `GitHub page <https://github.com/reity/barriers>`__ for this library.\r\n\r\nVersioning\r\n^^^^^^^^^^\r\nThe version number format for this library and the changes to the library associated with version number increments conform with `Semantic Versioning 2.0.0 <https://semver.org/#semantic-versioning-200>`__.\r\n\r\nPublishing\r\n^^^^^^^^^^\r\nThis library can be published as a `package on PyPI <https://pypi.org/project/barriers>`__ by a package maintainer. First, install the dependencies required for packaging and publishing:\r\n\r\n.. code-block:: bash\r\n\r\n python -m pip install .[publish]\r\n\r\nEnsure that the correct version number appears in ``pyproject.toml``, and that any links in this README document to the Read the Docs documentation of this package (or its dependencies) have appropriate version numbers. Also ensure that the Read the Docs project for this library has an `automation rule <https://docs.readthedocs.io/en/stable/automation-rules.html>`__ that activates and sets as the default all tagged versions. Create and push a tag for this version (replacing ``?.?.?`` with the version number):\r\n\r\n.. code-block:: bash\r\n\r\n git tag ?.?.?\r\n git push origin ?.?.?\r\n\r\nRemove any old build/distribution files. Then, package the source into a distribution archive:\r\n\r\n.. code-block:: bash\r\n\r\n rm -rf build dist src/*.egg-info\r\n python -m build --sdist --wheel .\r\n\r\nFinally, upload the package distribution archive to `PyPI <https://pypi.org>`__:\r\n\r\n.. code-block:: bash\r\n\r\n python -m twine upload dist/*\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Python decorators for including/excluding type checks, value/bounds checks, and other code blocks within the compiled bytecode of functions and methods.",
"version": "1.2.0",
"project_urls": {
"Documentation": "https://barriers.readthedocs.io",
"Repository": "https://github.com/reity/barriers"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "70bdcb38bbc36de6a3789b938951172d8851e15f8bc6917388997ed98e542ddc",
"md5": "5efa4135fc7cbfabf7a9fe53850076e0",
"sha256": "d60eef457cd9709905b4ebca497c76689676a3929269958a143018990f5cceed"
},
"downloads": -1,
"filename": "barriers-1.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "5efa4135fc7cbfabf7a9fe53850076e0",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 9944,
"upload_time": "2023-10-28T04:24:10",
"upload_time_iso_8601": "2023-10-28T04:24:10.015741Z",
"url": "https://files.pythonhosted.org/packages/70/bd/cb38bbc36de6a3789b938951172d8851e15f8bc6917388997ed98e542ddc/barriers-1.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e443ba276fa8a1e4db8e76d4d8efae6bd79175d201543cac9a64556b3eb1545f",
"md5": "ef93e1c2ab3ea9e705c45b5a91c4a776",
"sha256": "fcf35ef2998cc5b00b9119569111a128305397208134d92e7bbf1632e06e9c31"
},
"downloads": -1,
"filename": "barriers-1.2.0.tar.gz",
"has_sig": false,
"md5_digest": "ef93e1c2ab3ea9e705c45b5a91c4a776",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 11485,
"upload_time": "2023-10-28T04:24:11",
"upload_time_iso_8601": "2023-10-28T04:24:11.356688Z",
"url": "https://files.pythonhosted.org/packages/e4/43/ba276fa8a1e4db8e76d4d8efae6bd79175d201543cac9a64556b3eb1545f/barriers-1.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-10-28 04:24:11",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "reity",
"github_project": "barriers",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "barriers"
}