aiorem


Nameaiorem JSON
Version 0.0.2 PyPI version JSON
download
home_pageNone
Summaryasyncio Context Manager with Explicit Interface
upload_time2024-06-15 14:24:41
maintainerNone
docs_urlNone
authorNone
requires_python>=3.11
licenseNone
keywords asyncio contextmanager resources
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ``aiorem``
==========

.. image:: https://img.shields.io/pypi/v/aiorem.svg
   :target: https://pypi.org/project/aiorem/
   :alt: PyPi Package Version

.. image:: https://img.shields.io/pypi/pyversions/aiorem.svg
   :target: https://pypi.org/project/aiorem/
   :alt: Supported Python versions

.. image:: https://readthedocs.org/projects/aiorem/badge/?version=stable
   :target: https://aiorem.readthedocs.io/
   :alt: Documentation Status

.. image:: https://img.shields.io/pypi/l/aiorem.svg
   :target: https://mit-license.org/
   :alt: MIT License

.. image:: https://github.com/Bibo-Joshi/aiorem/actions/workflows/unit_tests.yml/badge.svg?branch=main
   :target: https://github.com/Bibo-Joshi/aiorem/
   :alt: Github Actions workflow

.. image:: https://codecov.io/gh/Bibo-Joshi/aiorem/graph/badge.svg?token=H1HUA2FDR3
 :target: https://codecov.io/gh/Bibo-Joshi/aiorem
   :alt: Code coverage

.. image:: https://results.pre-commit.ci/badge/github/Bibo-Joshi/aiorem/main.svg
   :target: https://results.pre-commit.ci/latest/github/Bibo-Joshi/aiorem/main
   :alt: pre-commit.ci status

.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
   :target: https://github.com/psf/black
   :alt: Code Style: Black

A simple asyncio context manager with explicit interface.

Introduction
------------

This library provides a simple context manager for managing resources in an asyncio environment.
It's designed to have an explicit interface, which makes it easy to use both as context manager and as a regular object for custom use cases.

Installing
----------

You can install or upgrade ``aiorem`` via

.. code:: shell

    $ pip install aiorem --upgrade

Motivation
----------

When working with ``asyncio`` Python libraries, async context managers are a common pattern for managing resources and snippets like

.. code:: python

    async with some_lib.Client() as client:
        ...

are often seen in the quickstart.
However, there are two use cases, where this pattern is hard to use and an explicit interface for acquiring and releasing resources is desirable.

1. Nested context managers: When writing a class that manages several resources, acquiring and releasing these resources should each usually be bundled in a single method.
2. Low level event loop usage: In some advanced cases, it can be desirable to use things like ``loop.run_until_complete`` than ``await``-ing a coroutine.

For both cases, one can then either explicitly call ``Client.__aenter__`` and ``Client.__aexit__`` or duplicate whatever logic is used within these methods.
Unfortunately, the behavior of ``Client.__aenter__`` and ``Client.__aexit__`` is not always well documented.
Moreover, using magic/dunder might be viewed as bad practice, as they are mostly intended to be used by the Python interpreter.

This shortcoming is what ``aiorem`` aims to improve.
As the Quick Start below shows, subclasses of ``aiorem.AbstractResourceManager`` can be used in different ways according to the needs of the use case while the behavior is well documented and explicit.
For the case of nested context managers, ``aiorem`` provides the `AbstractResourceManagerCollection <https://aiorem.readthedocs.io/stable/aiorem.html#aiorem.AbstractResourceManagerCollection>`_ class as a natural extension of `AbstractResourceManager <https://aiorem.readthedocs.io/stable/aiorem.html#aiorem.AbstractResourceManager>`_.

Quick Start
-----------

Here is a simple example of how to use ``aiorem``:

.. code:: python

    import asyncio
    from aiorem import AbstractResourceManager


    class ResourceManager(AbstractResourceManager):
        async def acquire_resources(self):
            print("Resource acquired")

        async def release_resources(self):
            print("Resource released")


    async def context_manager():
        async with ResourceManager():
            print("Context manager block")


    @ResourceManager()
    async def decorator():
        print("Decorator block")


    async def explicit_interface():
        rm = ResourceManager()
        await rm.acquire_resources()
        print("Explicit interface block")
        await rm.release_resources()


    async def main():
        await context_manager()
        await decorator()
        await explicit_interface()


    if __name__ == "__main__":
        asyncio.run(main())


For more information on how to use ``aiorem``, please refer to the `documentation <https://aiorem.readthedocs.io/>`_.
            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "aiorem",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": null,
    "keywords": "asyncio, contextmanager, resources",
    "author": null,
    "author_email": "Hinrich Mahler <aiorem@mahlerhome.de>",
    "download_url": "https://files.pythonhosted.org/packages/ee/c0/272ac4c7f23572dacc448daafb4294ac1967df361cb93c0cff1afd98a7c3/aiorem-0.0.2.tar.gz",
    "platform": null,
    "description": "``aiorem``\n==========\n\n.. image:: https://img.shields.io/pypi/v/aiorem.svg\n   :target: https://pypi.org/project/aiorem/\n   :alt: PyPi Package Version\n\n.. image:: https://img.shields.io/pypi/pyversions/aiorem.svg\n   :target: https://pypi.org/project/aiorem/\n   :alt: Supported Python versions\n\n.. image:: https://readthedocs.org/projects/aiorem/badge/?version=stable\n   :target: https://aiorem.readthedocs.io/\n   :alt: Documentation Status\n\n.. image:: https://img.shields.io/pypi/l/aiorem.svg\n   :target: https://mit-license.org/\n   :alt: MIT License\n\n.. image:: https://github.com/Bibo-Joshi/aiorem/actions/workflows/unit_tests.yml/badge.svg?branch=main\n   :target: https://github.com/Bibo-Joshi/aiorem/\n   :alt: Github Actions workflow\n\n.. image:: https://codecov.io/gh/Bibo-Joshi/aiorem/graph/badge.svg?token=H1HUA2FDR3\n :target: https://codecov.io/gh/Bibo-Joshi/aiorem\n   :alt: Code coverage\n\n.. image:: https://results.pre-commit.ci/badge/github/Bibo-Joshi/aiorem/main.svg\n   :target: https://results.pre-commit.ci/latest/github/Bibo-Joshi/aiorem/main\n   :alt: pre-commit.ci status\n\n.. image:: https://img.shields.io/badge/code%20style-black-000000.svg\n   :target: https://github.com/psf/black\n   :alt: Code Style: Black\n\nA simple asyncio context manager with explicit interface.\n\nIntroduction\n------------\n\nThis library provides a simple context manager for managing resources in an asyncio environment.\nIt's designed to have an explicit interface, which makes it easy to use both as context manager and as a regular object for custom use cases.\n\nInstalling\n----------\n\nYou can install or upgrade ``aiorem`` via\n\n.. code:: shell\n\n    $ pip install aiorem --upgrade\n\nMotivation\n----------\n\nWhen working with ``asyncio`` Python libraries, async context managers are a common pattern for managing resources and snippets like\n\n.. code:: python\n\n    async with some_lib.Client() as client:\n        ...\n\nare often seen in the quickstart.\nHowever, there are two use cases, where this pattern is hard to use and an explicit interface for acquiring and releasing resources is desirable.\n\n1. Nested context managers: When writing a class that manages several resources, acquiring and releasing these resources should each usually be bundled in a single method.\n2. Low level event loop usage: In some advanced cases, it can be desirable to use things like ``loop.run_until_complete`` than ``await``-ing a coroutine.\n\nFor both cases, one can then either explicitly call ``Client.__aenter__`` and ``Client.__aexit__`` or duplicate whatever logic is used within these methods.\nUnfortunately, the behavior of ``Client.__aenter__`` and ``Client.__aexit__`` is not always well documented.\nMoreover, using magic/dunder might be viewed as bad practice, as they are mostly intended to be used by the Python interpreter.\n\nThis shortcoming is what ``aiorem`` aims to improve.\nAs the Quick Start below shows, subclasses of ``aiorem.AbstractResourceManager`` can be used in different ways according to the needs of the use case while the behavior is well documented and explicit.\nFor the case of nested context managers, ``aiorem`` provides the `AbstractResourceManagerCollection <https://aiorem.readthedocs.io/stable/aiorem.html#aiorem.AbstractResourceManagerCollection>`_ class as a natural extension of `AbstractResourceManager <https://aiorem.readthedocs.io/stable/aiorem.html#aiorem.AbstractResourceManager>`_.\n\nQuick Start\n-----------\n\nHere is a simple example of how to use ``aiorem``:\n\n.. code:: python\n\n    import asyncio\n    from aiorem import AbstractResourceManager\n\n\n    class ResourceManager(AbstractResourceManager):\n        async def acquire_resources(self):\n            print(\"Resource acquired\")\n\n        async def release_resources(self):\n            print(\"Resource released\")\n\n\n    async def context_manager():\n        async with ResourceManager():\n            print(\"Context manager block\")\n\n\n    @ResourceManager()\n    async def decorator():\n        print(\"Decorator block\")\n\n\n    async def explicit_interface():\n        rm = ResourceManager()\n        await rm.acquire_resources()\n        print(\"Explicit interface block\")\n        await rm.release_resources()\n\n\n    async def main():\n        await context_manager()\n        await decorator()\n        await explicit_interface()\n\n\n    if __name__ == \"__main__\":\n        asyncio.run(main())\n\n\nFor more information on how to use ``aiorem``, please refer to the `documentation <https://aiorem.readthedocs.io/>`_.",
    "bugtrack_url": null,
    "license": null,
    "summary": "asyncio Context Manager with Explicit Interface",
    "version": "0.0.2",
    "project_urls": {
        "Documentation": "https://aiorem.readthedocs.io/",
        "Issues": "https://github.com/Bibo-Joshi/aiorem/issues",
        "Source": "https://github.com/Bibo-Joshi/aiorem"
    },
    "split_keywords": [
        "asyncio",
        " contextmanager",
        " resources"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "044778b9f28bf3aa11e6b6483667ca0d93d8b35be2aa84a791e2dcfa1ea7fa0c",
                "md5": "6fcb87651425495077885796598b9701",
                "sha256": "c405be3606270ed4549ea883aaab670df13aa25a3d3d6d754f865f98ea1bc6cc"
            },
            "downloads": -1,
            "filename": "aiorem-0.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6fcb87651425495077885796598b9701",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 6662,
            "upload_time": "2024-06-15T14:24:39",
            "upload_time_iso_8601": "2024-06-15T14:24:39.901801Z",
            "url": "https://files.pythonhosted.org/packages/04/47/78b9f28bf3aa11e6b6483667ca0d93d8b35be2aa84a791e2dcfa1ea7fa0c/aiorem-0.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "eec0272ac4c7f23572dacc448daafb4294ac1967df361cb93c0cff1afd98a7c3",
                "md5": "8bc95702731783b1fd1e0c8f36f1c144",
                "sha256": "4a5b3b1a77a0e55d8e08fbd59f04dfcffecae999e4ac1b7e8ec110484950e09d"
            },
            "downloads": -1,
            "filename": "aiorem-0.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "8bc95702731783b1fd1e0c8f36f1c144",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 6744,
            "upload_time": "2024-06-15T14:24:41",
            "upload_time_iso_8601": "2024-06-15T14:24:41.406271Z",
            "url": "https://files.pythonhosted.org/packages/ee/c0/272ac4c7f23572dacc448daafb4294ac1967df361cb93c0cff1afd98a7c3/aiorem-0.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-15 14:24:41",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Bibo-Joshi",
    "github_project": "aiorem",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "aiorem"
}
        
Elapsed time: 0.62632s