Skip to content

Instantly share code, notes, and snippets.

@isidentical
Created March 11, 2019 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isidentical/9dda0ddce533451d26879cce2c7d4f02 to your computer and use it in GitHub Desktop.
Save isidentical/9dda0ddce533451d26879cce2c7d4f02 to your computer and use it in GitHub Desktop.
Catlizor in 18 lines!
def exc(self, result):
for callback in self.hook_spec[result.condition][1]:
try: callback(result)
except CallbackStop: break
def meth_wrapper(function, catlizor):
def wrapper(*args, **kwargs):
if HookConditions.PRE in catlizor.tracked(function.__name__): catlizor.exc(Result(function, args, kwargs, HookConditions.PRE))
res = function(*args, **kwargs)
if HookConditions.ON_CALL in catlizor.tracked(function.__name__): catlizor.exc(Result(function, args, kwargs, HookConditions.ON_CALL, res))
if HookConditions.POST in catlizor.tracked(function.__name__): catlizor.exc(Result(function, args, kwargs, HookConditions.POST, kwargs))
return res
return __import__('functools').wraps(function)(wrapper)
Result = type('Result', (), {'__init__': (lambda self, meth=None, args=None, kwargs=None, condition=None, result=None: [setattr(self, name, value) for name, value in locals().items()][0])})
HookSpec = type('HookSpec', (), {'__init__': (lambda self, methods=None, callbacks=None: [setattr(self, name, value) for name, value in locals().items()][0]), '__add__': (lambda self, other: self.__class__(**{k: (v | getattr(other, k)) for k, v in vars(self).items()})), '__radd__': (lambda self, other: self if not isinstance(other, self.__class__) else self.__class__.__add__(other, self))})
CallbackStop = type('CallbackStop', (Exception,), {})
HookConditions = __import__('enum').Enum('HookConditions', 'PRE POST ON_CALL')
Hook = type('Hook', (), {'pre': __import__('functools').partial((lambda cls, cond: [cls, getattr(cls, "__condition").append(cond) if hasattr(cls, "__condition") else setattr(cls, "__condition", [cond])][0]), cond=HookConditions.PRE), 'post': __import__('functools').partial((lambda cls, cond: [cls, getattr(cls, "__condition").append(cond) if hasattr(cls, "__condition") else setattr(cls, "__condition", [cond])][0]), cond=HookConditions.POST), 'on_call': __import__('functools').partial((lambda cls, cond: [cls, getattr(cls, "__condition").append(cond) if hasattr(cls, "__condition") else setattr(cls, "__condition", [cond])][0]), cond=HookConditions.ON_CALL), 'update_hookspec': classmethod(lambda cls: setattr(cls, "hook_spec", HookSpec(getattr(cls, "methods", []), getattr(cls, "callbacks", [])))), '__init_subclass__': (lambda cls: setattr(cls, "hook_spec", HookSpec(getattr(cls, "methods", []), getattr(cls, "callbacks", []))))})
Catlizor = type('Catlizor', (), {'__init__': (lambda self, klass, hook_spec, __catlized_methods = None: [setattr(self, name, value or set((y[0][1] for y in [[(setattr(self.klass, meth, __import__('functools').partial(meth_wrapper, catlizor=self)(getattr(self.klass, meth))), meth) for meth in spec[0]] for spec in hook_spec.values()]))) for name, value in locals().items()][0]), 'hook': (classmethod(lambda cls, klass, *hooks: cls(klass, {HookConditions.PRE: [(lambda cond, hooks: sum(getattr(hook, "hook_spec") for hook in filter(lambda hook: getattr(HookConditions, cond) in getattr(hook, "__condition"), hooks)) or HookSpec())("PRE", hooks).methods, (lambda cond, hooks: sum(getattr(hook, "hook_spec") for hook in filter(lambda hook: getattr(HookConditions, cond) in getattr(hook, "__condition"), hooks)) or HookSpec())("PRE", hooks).callbacks], HookConditions.POST: [(lambda cond, hooks: sum(getattr(hook, "hook_spec") for hook in filter(lambda hook: getattr(HookConditions, cond) in getattr(hook, "__condition"), hooks)) or HookSpec())("POST", hooks).methods, (lambda cond, hooks: sum(getattr(hook, "hook_spec") for hook in filter(lambda hook: getattr(HookConditions, cond) in getattr(hook, "__condition"), hooks)) or HookSpec())("POST", hooks).callbacks], HookConditions.ON_CALL: [(lambda cond, hooks: sum(getattr(hook, "hook_spec") for hook in filter(lambda hook: getattr(HookConditions, cond) in getattr(hook, "__condition"), hooks)) or HookSpec())("ON_CALL", hooks).methods, (lambda cond, hooks: sum(getattr(hook, "hook_spec") for hook in filter(lambda hook: getattr(HookConditions, cond) in getattr(hook, "__condition"), hooks)) or HookSpec())("ON_CALL", hooks).callbacks]}))), 'tracked': (lambda self, method_name: [keys for keys, values in self.hook_spec.items() if method_name in values[0]] ), 'reset': (lambda self: [setattr(self.klass, meth, getattr(self.klass, meth).__wrapped__) for meth in getattr(self.klass, "__catlized_methods") if hasattr(getattr(self.klass, meth), "__wrapped__")][0]), 'exc': exc})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment