py-interceptor


Namepy-interceptor JSON
Version 0.1.0 PyPI version JSON
download
home_pageNone
SummaryA library for intercepting and processing Python method calls.
upload_time2025-01-16 15:09:01
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseNone
keywords mocking testing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # PyInterceptor

A library for intercepting and processing Python method calls.

## Introduction

Sometimes it might be interesting to get detailed knowledge about which methods of an object have been called with which
args, kwargs, at which time, etc. but without changing the underlying code.
This is especially useful for:

- Debugging
- Logging
- Creating call statistics, etc.

PyInterceptor enables exactly this - it installs a handler function into a target object that intercepts specified
methods and stores (meta-) data about the calls in `CallInfo` objects. These objects are then handed over to a
user-defined `interceptor` callable.

![call_sequence_detailed.png](doc/images/call_sequence_detailed.png)

PyInterceptor distinguishes between 2 modi:

- blocking mode: In this mode the handler does not execute the actual method and returns the return value from the
  `interceptor`. This mode is very useful when creating mocks or stubs.
- non-blocking mode: In this mode the handler executed the actual method and forwards its return value to the
  `interceptor` callable. Then it continues like in the blocking mode.

## Installation

To install PyInterceptor from pypi using pip type:
`pip install py-interceptor`

To install PyInterceptor from source, do the following steps:

1. Create an environment, e.g. with venv
    - `python -m venv env`
    - `env\Scripts\activate` (windows)
    - `source env/bin/activate` (linux)
2. Install the package from source
    - `cd py-interceptor`
    - `pip install -e .` (without dev dependencies)
    - `pip install -e .[dev]` (with dev dependencies)
3. Execute unit tests (requires dev dependencies)
    - `pytest`

## Examples

The following example demonstrates how easy it is to intercept an object's method. Here we want to print the name of the
executed API method together with the args and the return value:

```
from interceptor import CallInfo, intercept


class API:
    def add(self, a, b):
        return a + b

def interceptor(info: CallInfo):
    print(f"Executed {info.name} with args {info.args} -> returned {info.ret_value}")

api = API()
intercept("add", api, interceptor, blocking=False)
api.add(1, 2)
```
The output should be:
`Executed add with args (1, 2) -> returned 3`

More example can be found in the [examples](examples) folder.


            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "py-interceptor",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "mocking, testing",
    "author": null,
    "author_email": "RustyTuna <prototype4711@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/99/79/e045ba208edf97b39a275f4049b7238da659b3e927d6316ea1e6e3685952/py_interceptor-0.1.0.tar.gz",
    "platform": null,
    "description": "# PyInterceptor\r\n\r\nA library for intercepting and processing Python method calls.\r\n\r\n## Introduction\r\n\r\nSometimes it might be interesting to get detailed knowledge about which methods of an object have been called with which\r\nargs, kwargs, at which time, etc. but without changing the underlying code.\r\nThis is especially useful for:\r\n\r\n- Debugging\r\n- Logging\r\n- Creating call statistics, etc.\r\n\r\nPyInterceptor enables exactly this - it installs a handler function into a target object that intercepts specified\r\nmethods and stores (meta-) data about the calls in `CallInfo` objects. These objects are then handed over to a\r\nuser-defined `interceptor` callable.\r\n\r\n![call_sequence_detailed.png](doc/images/call_sequence_detailed.png)\r\n\r\nPyInterceptor distinguishes between 2 modi:\r\n\r\n- blocking mode: In this mode the handler does not execute the actual method and returns the return value from the\r\n  `interceptor`. This mode is very useful when creating mocks or stubs.\r\n- non-blocking mode: In this mode the handler executed the actual method and forwards its return value to the\r\n  `interceptor` callable. Then it continues like in the blocking mode.\r\n\r\n## Installation\r\n\r\nTo install PyInterceptor from pypi using pip type:\r\n`pip install py-interceptor`\r\n\r\nTo install PyInterceptor from source, do the following steps:\r\n\r\n1. Create an environment, e.g. with venv\r\n    - `python -m venv env`\r\n    - `env\\Scripts\\activate` (windows)\r\n    - `source env/bin/activate` (linux)\r\n2. Install the package from source\r\n    - `cd py-interceptor`\r\n    - `pip install -e .` (without dev dependencies)\r\n    - `pip install -e .[dev]` (with dev dependencies)\r\n3. Execute unit tests (requires dev dependencies)\r\n    - `pytest`\r\n\r\n## Examples\r\n\r\nThe following example demonstrates how easy it is to intercept an object's method. Here we want to print the name of the\r\nexecuted API method together with the args and the return value:\r\n\r\n```\r\nfrom interceptor import CallInfo, intercept\r\n\r\n\r\nclass API:\r\n    def add(self, a, b):\r\n        return a + b\r\n\r\ndef interceptor(info: CallInfo):\r\n    print(f\"Executed {info.name} with args {info.args} -> returned {info.ret_value}\")\r\n\r\napi = API()\r\nintercept(\"add\", api, interceptor, blocking=False)\r\napi.add(1, 2)\r\n```\r\nThe output should be:\r\n`Executed add with args (1, 2) -> returned 3`\r\n\r\nMore example can be found in the [examples](examples) folder.\r\n\r\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A library for intercepting and processing Python method calls.",
    "version": "0.1.0",
    "project_urls": {
        "Documentation": "https://github.com/RustyTunaDev/py-interceptor#readme",
        "Issues": "https://github.com/RustyTunaDev/py-interceptor#readme/-/issues",
        "Source": "https://github.com/RustyTunaDev/py-interceptor#readme/src"
    },
    "split_keywords": [
        "mocking",
        " testing"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "379f802cfc609efe94a8e6e3fd98bd78629bb554f4b1a72055dbfc06d38516e6",
                "md5": "8bcf6d3ddcaa34f1a2ad5089207618db",
                "sha256": "d267da449810021849a7769e54930ee0486daf1bfc9d9bf4e873ef6d8d2e7e55"
            },
            "downloads": -1,
            "filename": "py_interceptor-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8bcf6d3ddcaa34f1a2ad5089207618db",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 4825,
            "upload_time": "2025-01-16T15:08:58",
            "upload_time_iso_8601": "2025-01-16T15:08:58.917255Z",
            "url": "https://files.pythonhosted.org/packages/37/9f/802cfc609efe94a8e6e3fd98bd78629bb554f4b1a72055dbfc06d38516e6/py_interceptor-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9979e045ba208edf97b39a275f4049b7238da659b3e927d6316ea1e6e3685952",
                "md5": "4c6bcc75ee8071baf01777811710bcda",
                "sha256": "11c0b517290aaf72269140f7a0a49b7393d312b24de6cc0c2a7fffd898b1c7bf"
            },
            "downloads": -1,
            "filename": "py_interceptor-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "4c6bcc75ee8071baf01777811710bcda",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 5372,
            "upload_time": "2025-01-16T15:09:01",
            "upload_time_iso_8601": "2025-01-16T15:09:01.465628Z",
            "url": "https://files.pythonhosted.org/packages/99/79/e045ba208edf97b39a275f4049b7238da659b3e927d6316ea1e6e3685952/py_interceptor-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-01-16 15:09:01",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "RustyTunaDev",
    "github_project": "py-interceptor#readme",
    "github_not_found": true,
    "lcname": "py-interceptor"
}
        
Elapsed time: 0.88342s