and( name_is_short, or( older_than( 30 ), not( is_active ) ) )
True
Last active
January 24, 2026 01:55
-
-
Save thehappycheese/c3360d81ccfe650e3e6039d50ca19d1c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from dataclasses import dataclass | |
| import functools | |
| from typing import Callable | |
| @dataclass | |
| class ReprFunc: | |
| func:Callable | |
| name:str|None | |
| arg_names:list | |
| def __call__(self, *args, **kwargs): | |
| return self.func(*args, **kwargs) | |
| def __repr__(self) -> str: | |
| args = f"( {', '.join(map(repr,self.arg_names))} )" if len(self.arg_names)>0 else "" | |
| return f"{self.name if self.name is not None else self.func.__name__}{args}" | |
| def repr_func(name:str|None, inputs:list): | |
| def _dec(func): | |
| wrapper = ReprFunc(func, name, inputs) | |
| return functools.update_wrapper(wrapper, func) | |
| return _dec | |
| def pand[T](a:Callable[[T],bool],b:Callable[[T],bool]) -> Callable[[T],bool]: | |
| @repr_func("and",[a,b]) | |
| def _and(c:T)->bool: | |
| return a(c) and b(c) | |
| return _and | |
| def por[T](a:Callable[[T],bool],b:Callable[[T],bool]) -> Callable[[T],bool]: | |
| @repr_func("or",[a,b]) | |
| def _or(c:T)->bool: | |
| return a(c) or b(c) | |
| return _or | |
| def pnot[T](a:Callable[[T],bool]) -> Callable[[T],bool]: | |
| @repr_func("not",[a]) | |
| def _not(c:T)->bool: | |
| return not a(c) | |
| return _not | |
| @dataclass | |
| class User: | |
| name:str | |
| age:int | |
| active:bool | |
| @repr_func(None,[]) | |
| def is_active(u:User): | |
| return u.active==True | |
| def older_than(age:int): | |
| @repr_func("older_than",[age]) | |
| def _is_older_than(u:User): | |
| return u.age>=age | |
| return _is_older_than | |
| @repr_func(None,[]) | |
| def name_is_short(u:User): | |
| return len(u.name)==4 | |
| is_special_user = pand( | |
| name_is_short, | |
| por( | |
| older_than(30), | |
| pnot(is_active) | |
| ) | |
| ) | |
| print(is_special_user) | |
| print(is_special_user(User("nick", 35,False))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment