#for visual purpose : with row/col labels def data_frame(ary,rows,cols): frame = pd.DataFrame(ary,columns=cols) frame.insert(0,'x', rows ) return frame
x
SINGLETON factory
You can define Singleton class manually, but if you want to create multiple different Singleton classes on the fly then you can use the function below :
#Factory : to create Singleton classes def singleton(new_klass, klass): def new(cls, *args, **kvargs): if not hasattr(cls, 'instance'): cls.instance = klass(*args, **kvargs) return cls.instance globals()[new_klass] = type(new_klass, (object,), {'__new__' : new})
Deep REDUCE
#apply different reduce-fun at every lvl of hierarchy #! on firt call len(lol) must be > 2 def deep_reduce(funs, LoL): def isi(a): return isinstance(a, (list,tuple,types.GeneratorType)) #or is_iter(a) def calc(a,b): # print('--=', funs[0],a,b) a1 = deep_reduce(funs[1:], a) if isi(a) else a b1 = deep_reduce(funs[1:], b) if isi(b) else b return funs[0](a1,b1) if isi(LoL): return reduce(calc,LoL) else: return LoL
Check if INTEGER
#.isdigit() and .isnumeric() dont detect negative ints def is_int(astr): isit = True try: int(astr) except ValueError: isit = False return isit