Skip to content
This repository was archived by the owner on Nov 26, 2021. It is now read-only.

ybibaev/hookinator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hookinator

Requirements

  • Python 3.6+

Examples

from hookinator import hookable, hook


class BaseUser:
    def __init__(self, first_name: str, last_name: str):
        self.first_name = first_name
        self.last_name = last_name
    ...

# with in-class declaration

@hookable
class User(BaseUser):
    full_name: str
    
    # create post __init__ hook
    @hook(method="__init__", post=True)
    def set_full_name(self, context):
        self.full_name = f'{self.first_name} {self.last_name}'


user = User(first_name="First", last_name="Last")
assert user.full_name == "First Last"


# or ex-class declaration

@hook(method="__init__", post=True)
def post_init_hook(self, context):
    pass

@hook(method="__init__", post=True)
def validate_first_name(context):
    self.first_name = self.first_name.lower()

post_inti_hook.bind(BaseUser)

user = BaseUser(first_name="First", last_name="Last")
assert user.first_name == "first"

About

Inheritance is too boring. This is a simple tool for hooks.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages