pybooster


Namepybooster JSON
Version 0.0.4 PyPI version JSON
download
home_pageNone
SummaryDependency injection without the boilerplate.
upload_time2024-12-07 20:32:33
maintainerNone
docs_urlNone
authorNone
requires_python<4,>=3.11
licenseMIT License Copyright (c) 2024-present Ryan Morshead <ryan.morshead@gmail.com> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords dependency framework injection
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # PyBooster 💉

[![PyPI - Version](https://img.shields.io/pypi/v/pybooster.svg)](https://pypi.org/project/pybooster)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

> [!WARNING]
> This project is still under development - use at your own risk.

PyBooster - dependency injection without the boilerplate.

## Documentation

Learn more here: https://ryanmorshead.com/pybooster

## Install

```bash
pip install -U pybooster
```

## At a Glance

Getting started with PyBooster involves a few steps:

1. Define a [provider](https://ryanmorshead.com/pybooster/concepts#providers) function
    for a [dependency](https://ryanmorshead.com/pybooster/conceptsd#dependencies).
1. Add an [injector](https://ryanmorshead.com/pybooster/conceptsd#injectors) to a
    function that will use that dependency.
1. Activate a [solution](https://ryanmorshead.com/pybooster/conceptsd#solutions) and
    call the dependent function in its context.

The example below injects a `sqlite3.Connection` into a function that executes SQL:

```python
import sqlite3
from collections.abc import Iterator
from tempfile import NamedTemporaryFile

from pybooster import injector
from pybooster import provider
from pybooster import required
from pybooster import solved


@provider.iterator
def sqlite_connection(database: str) -> Iterator[sqlite3.Connection]:
    with sqlite3.connect(database) as conn:
        yield conn


@injector.function
def sql(cmd: str, *, conn: sqlite3.Connection = required) -> sqlite3.Cursor:
    return conn.execute(cmd)


tempfile = NamedTemporaryFile()
with solved(sqlite_connection.bind(tempfile.name)):
    sql("CREATE TABLE example (id INTEGER PRIMARY KEY, name TEXT)")
    sql("INSERT INTO example (name) VALUES ('alice')")
    cursor = sql("SELECT * FROM example")
    assert cursor.fetchone() == (1, "alice")
```

This works by inspecting the type hints of the provider `sqlite_connection` to see that
it produces a `sqlite3.Connection`. Simarly, the signature of the dependant function
`query_database` is inspected to see that it requires a `sqlite3.Connection`. At that
point, when `query_database` is called it checks to see if there's a
`sqlite3.Connection` provider in the current solution and, if so, injects it into the
function.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "pybooster",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4,>=3.11",
    "maintainer_email": null,
    "keywords": "dependency, framework, injection",
    "author": null,
    "author_email": "Ryan Morshead <ryan.morshead@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/66/0c/7cae96c7f0b65f47f554f48e36151d197802fdae82f1161f0f36ebd3ab99/pybooster-0.0.4.tar.gz",
    "platform": null,
    "description": "# PyBooster \ud83d\udc89\n\n[![PyPI - Version](https://img.shields.io/pypi/v/pybooster.svg)](https://pypi.org/project/pybooster)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\n> [!WARNING]\n> This project is still under development - use at your own risk.\n\nPyBooster - dependency injection without the boilerplate.\n\n## Documentation\n\nLearn more here: https://ryanmorshead.com/pybooster\n\n## Install\n\n```bash\npip install -U pybooster\n```\n\n## At a Glance\n\nGetting started with PyBooster involves a few steps:\n\n1. Define a [provider](https://ryanmorshead.com/pybooster/concepts#providers) function\n    for a [dependency](https://ryanmorshead.com/pybooster/conceptsd#dependencies).\n1. Add an [injector](https://ryanmorshead.com/pybooster/conceptsd#injectors) to a\n    function that will use that dependency.\n1. Activate a [solution](https://ryanmorshead.com/pybooster/conceptsd#solutions) and\n    call the dependent function in its context.\n\nThe example below injects a `sqlite3.Connection` into a function that executes SQL:\n\n```python\nimport sqlite3\nfrom collections.abc import Iterator\nfrom tempfile import NamedTemporaryFile\n\nfrom pybooster import injector\nfrom pybooster import provider\nfrom pybooster import required\nfrom pybooster import solved\n\n\n@provider.iterator\ndef sqlite_connection(database: str) -> Iterator[sqlite3.Connection]:\n    with sqlite3.connect(database) as conn:\n        yield conn\n\n\n@injector.function\ndef sql(cmd: str, *, conn: sqlite3.Connection = required) -> sqlite3.Cursor:\n    return conn.execute(cmd)\n\n\ntempfile = NamedTemporaryFile()\nwith solved(sqlite_connection.bind(tempfile.name)):\n    sql(\"CREATE TABLE example (id INTEGER PRIMARY KEY, name TEXT)\")\n    sql(\"INSERT INTO example (name) VALUES ('alice')\")\n    cursor = sql(\"SELECT * FROM example\")\n    assert cursor.fetchone() == (1, \"alice\")\n```\n\nThis works by inspecting the type hints of the provider `sqlite_connection` to see that\nit produces a `sqlite3.Connection`. Simarly, the signature of the dependant function\n`query_database` is inspected to see that it requires a `sqlite3.Connection`. At that\npoint, when `query_database` is called it checks to see if there's a\n`sqlite3.Connection` provider in the current solution and, if so, injects it into the\nfunction.\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2024-present Ryan Morshead <ryan.morshead@gmail.com>  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "Dependency injection without the boilerplate.",
    "version": "0.0.4",
    "project_urls": {
        "Documentation": "https://ryanmorshead.com/pybooster",
        "Source": "https://github.com/rmorshea/pybooster"
    },
    "split_keywords": [
        "dependency",
        " framework",
        " injection"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "42f9e96ba2f9f3b897f7c00777b7c5bb183cb88e3d935ec9bf9c520231a6b4fd",
                "md5": "7c07019da32dcb82cf570dc54dd97101",
                "sha256": "ae5397965acf46e6dcfd9ccc57aa1e69ac05f9ae545c6d30cb0ce63629975fa2"
            },
            "downloads": -1,
            "filename": "pybooster-0.0.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7c07019da32dcb82cf570dc54dd97101",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4,>=3.11",
            "size": 20115,
            "upload_time": "2024-12-07T20:32:32",
            "upload_time_iso_8601": "2024-12-07T20:32:32.405101Z",
            "url": "https://files.pythonhosted.org/packages/42/f9/e96ba2f9f3b897f7c00777b7c5bb183cb88e3d935ec9bf9c520231a6b4fd/pybooster-0.0.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "660c7cae96c7f0b65f47f554f48e36151d197802fdae82f1161f0f36ebd3ab99",
                "md5": "6d6766792d7594f8c2215a3b516fe654",
                "sha256": "9d06e45b2718eccfc2f916aeba322ccffcb43fc699752a9caae0017046a96041"
            },
            "downloads": -1,
            "filename": "pybooster-0.0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "6d6766792d7594f8c2215a3b516fe654",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4,>=3.11",
            "size": 110789,
            "upload_time": "2024-12-07T20:32:33",
            "upload_time_iso_8601": "2024-12-07T20:32:33.430870Z",
            "url": "https://files.pythonhosted.org/packages/66/0c/7cae96c7f0b65f47f554f48e36151d197802fdae82f1161f0f36ebd3ab99/pybooster-0.0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-07 20:32:33",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "rmorshea",
    "github_project": "pybooster",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "pybooster"
}
        
Elapsed time: 0.50292s