Provides utilities to implement a hookable interface
Project description
PyTapable
A Library to Implement Hookable Interfaces
:corn: Table of Contents
:strawberry: About The Project
PyTapable provides a set of utility to help you implement hookable interfaces in your classes. This opens up the posibility for solving a number of usecases such as
- Providing plugable interfaces for your libraries and frameworks
- Code seperation by functional and service domains
:sun_with_face: Getting Started
This project can be used in python 2.7, 3.5 and greater
$ pip install pytapable
Example
Inline hooks
We first create our hook called my_hook
from pytapable import Hook
my_hook = Hook()
As a consumer, we can tap into this hook by passing a name for our tap and a callback function
def my_callback(context, greeting):
print(f"Hook says: {greeting}")
my_hook.tap('My Tap Name', my_callback)
Our callback is executed when the hook.call(...) is executed. The callback receives whatever args were passed in the
hook.call method in addition to a context dict
my_hook.call(greeting="Hi Callback")
Functional Hooks
Functional hooks are different from inline hooks in that the callback args are predefined.
from pytapable import CreateHook, HookableMixin, create_hook_name
class Car(HookableMixin):
HOOK_ON_MOVE = create_hook_name('on_move')
@CreateHook(name=HOOK_ON_MOVE)
def move(self, speed=10):
return f"Moving at {speed} Mph"
- Start adding the
HookableMixinto the Car Class. This is necessary to install hooks on class methods. - Decorate the
Car.movemethod using the@CreateHookdecorator. In the decorator, give it a name. As best practice we define the name as a Class Constant so consumers can easily refer to it. - The value of the hook can be anything. We use the
create_hook_name(str)utility to generate a unique name. Generating a unique name is not required but becomes important when inheriting hooks from other Classes.
def callback(context, fn_args, fn_output):
kmph_speed = fn_args['speed'] * 1.61
print(f"The car is moving {kmph_speed} kmph")
c = Car()
c.hooks[Car.HOOK_ON_MOVE].tap('log_metric_speed', callback, before=False)
c.move(10)
- Here we tap into the
on_movehook which fires our callback after thec.movemethod has executed - The
c.move()arguments are passed asfn_argsto the callback and return value, if any, is passed asfn_output - The context holds a
is_beforeandis_afterflag it signify if the callback was executed before or afterc.move()
:tropical_drink: Documentation
Full documentation is available here https://pytapable.readthedocs.io/en/latest
:satisfied: Contributing
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
To tests on your changes locally, run:
$ pip install -r test_requirements.txt
$ tox .
This will run your changes on python-2 and python-3
Documentation for any new changes are a must. We use Sphinx and to build the documentation locally, run:
$ cd docs/
$ make html
# or on windows
$ make.bat html
:v: License
Distributed under the Apache License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file PyTapable-0.1.0.tar.gz.
File metadata
- Download URL: PyTapable-0.1.0.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
663181e5693767857439150b8193d8245bcf5c22bff8b99f255b3f75119d2528
|
|
| MD5 |
140e267de89475f560ce23cc635bccfe
|
|
| BLAKE2b-256 |
992b5f127f247117998fda8dfda3482fc77a823049bf54788276700b1fb5dae9
|
File details
Details for the file PyTapable-0.1.0-py3-none-any.whl.
File metadata
- Download URL: PyTapable-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
557e9eb3246beca7b9c3bbd2cba9a48d921c933fe522493509b4221f1e631a94
|
|
| MD5 |
9b9356a43613c6dc81e6725392355d95
|
|
| BLAKE2b-256 |
14bafeb9e288018d101af69b68d823662c65dadfdeb9a86cdc5d285c409a5924
|