Open masterPiece93 opened 9 months ago
def isnamedtupleinstance(x)->bool: """checks if object is namedtuple""" t = type(x) b = t.__bases__ if len(b) != 1 or b[0] != tuple: return False f = getattr(t, '_fields', None) if not isinstance(f, tuple): return False return all(type(n)==str for n in f) class Unpack: """[Python Spreading & assigning] (**Inspired from TypeScript) d: dict = {"name":"ankit", "age":29, "sex":"Male"} e.g 1 [ unpack on fly, only the desired keys ] :- >> name, age = Unpack('name', 'age') << d e.g 2 [ make unpack, apply on multple dicts at multiple places ] :- >> _un = Unpack('name', 'age') >> name, age = _un << d >> print(_un.values.name) >> print(_un.values.age) v1.0.0 -(ankit.kumar05) """ def __init__(self,*keys): self.keys=keys self._obj = collections.namedtuple('obj', self.keys, defaults=[None]*len(self.keys)) self._values = self._obj() @property def values(self): return self._values def __lshift__(self, other): out_values = () if isinstance(other, dict): for key in self.keys: out_values += (other[key],) self._values = self._obj(*out_values) return out_values class Pipe: """[Python Piping Support] series of Transformation functions can be applied intutively. **Must Not be confused with Builder Design Pattern v1.0.1 -(ankit.kumar05) """ def __init__(self,obj): self._obj = obj @property def value(self): return self._obj def __rshift__(self, other): if callable(other): self._obj = other(self._obj) return self def __str__(self): return str(self._obj) class Print: bcolors = dict( HEADING = '\033[95m' , OKBLUE = '\033[94m' , OKCYAN = '\033[96m' , OKGREEN = '\033[92m' , WARNING = '\033[93m' , FAIL = '\033[91m' , ENDC = '\033[0m' , BOLD = '\033[1m' , UNDERLINE = '\033[4m' ) def __init__(self,color=None): color: str = color.upper() if color else None self.start = self.bcolors.get(color) self.end = self.bcolors['ENDC'] def __lshift__(self, obj): if self.start: print(self.start,obj,self.end) else: print(obj) return obj