Kaydet (Commit) 88ea7536 authored tarafından Batuhan Taşkaya's avatar Batuhan Taşkaya

Add defaults to hookspec and fix empty problem, add readme

üst 71cdabe3
# Catlizor (v1)
Action Hooks
## Example
```py
from catlizor import Hook, Catlizor
class TaskManager:
def __init__(self):
self.tasks = {}
def add_task(self, task: str, *items):
self.tasks[task] = items
def pop_task(self):
return self.tasks.popitem()
def get_tasks(self, task: str):
return self.tasks[task]
@Hook.pre
class PreLoggingHook(Hook):
methods = ['add_task']
callbacks = [lambda result: print(result.args, result.kwargs)]
@Hook.on_call
class PostLoggingHook(Hook):
methods = ['pop_task', 'get_tasks']
callbacks = [lambda result: print(result.result)]
tm_catlizor = Catlizor.hook(TaskManager, PreLoggingHook, PostLoggingHook)
tm = TaskManager()
tm.add_task("süt al", "markete git", "süt reyonuna ulaş")
tm.get_tasks("süt al")
tm.pop_task()
```
Result (stdout);
```
(<__main__.TaskManager object at 0x7fa851743748>, 'süt al', 'markete git', 'süt reyonuna ulaş') {}
('markete git', 'süt reyonuna ulaş')
('süt al', ('markete git', 'süt reyonuna ulaş'))
```
```
......@@ -2,7 +2,7 @@ from __future__ import annotations
from collections.abc import Sequence as SequenceBase
from contextlib import contextmanager
from dataclasses import dataclass
from dataclasses import dataclass, field
from enum import Enum, auto
from functools import partial, reduce, wraps
from types import FunctionType
......@@ -18,7 +18,7 @@ def get_hooks(cond, hooks: Tuple[Sequence[Hook], ...]):
res = sum(getattr(hook, HOOK_SPEC) for hook in filter(compare_hook, hooks))
if res == 0:
res = HookSpec(None, None)
res = HookSpec()
return res
......@@ -42,8 +42,8 @@ class Result:
@dataclass
class HookSpec:
methods: Optional[Sequence[str]]
callbacks: Optional[Sequence[Callable]]
methods: Optional[Sequence[str]] = field(default_factory=set)
callbacks: Optional[Sequence[Callable]] = field(default_factory=set)
def __post_init__(self):
for attr in vars(self).keys():
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment