classDataDict(NestedDict,_Arithmetics):"""A NestedDict that supports arithmetics. Other methods are included that make DataDict similar to DataFrames."""def_arithmetic_operation(self,other,operation:str,symbol:str):"""Implements any arithmetic operation, just pass the underlying method as string The symbol, passed as a string, will appear in the exception message if any The operation is performed only between NestedProperties or with numbers"""result=self.copy()ifisinstance(other,self.__class__):forother_key,other_valueinother.items():ifother_keyinself:forkey,valueinself.extract[other_key].items():result[key]=getattr(value,operation).__call__(other_value)else:raiseTypeError(f"unsupported operand type(s) for {symbol}: incompatible keys")returnresultelifisinstance(other,Number):forkey,valueinself.items():result[key]=getattr(value,operation).__call__(other)returnresultreturnTypeError(f"unsupported operand type(s) for {symbol}: {type(self)} and {type(other)}")defapply(self,func:Callable,inplace:bool=False):"""Apply func to all values."""ifinplace:forkey,leafinself.items():self[key]=func(leaf)else:new_self=self.copy()forkey,leafinnew_self.items():new_self[key]=func(leaf)returnnew_selfdefreduce(self,func:Callable,*initial:Any):"""Pass func and initial to functools.reduce and apply it to all values."""returnreduce(func,self.values(),*initial)deftotal(self):"""Returns sum of all values."""returnsum(self.values())defmean(self)->Number:"""Returns mean of all values."""returnself.total()/len(self)defstd(self)->Number:"""Returns standard deviation of all values."""step=self.reduce(lambdaa,b:a+(b-self.mean())**2,0)step/=len(self)-1returnstep**0.5
apply(func,inplace=False)
Apply func to all values.
Source code in ndicts\data_dict.py
77787980818283848586
defapply(self,func:Callable,inplace:bool=False):"""Apply func to all values."""ifinplace:forkey,leafinself.items():self[key]=func(leaf)else:new_self=self.copy()forkey,leafinnew_self.items():new_self[key]=func(leaf)returnnew_self
mean()
Returns mean of all values.
Source code in ndicts\data_dict.py
969798
defmean(self)->Number:"""Returns mean of all values."""returnself.total()/len(self)
reduce(func,*initial)
Pass func and initial to functools.reduce and apply it to all values.
Source code in ndicts\data_dict.py
888990
defreduce(self,func:Callable,*initial:Any):"""Pass func and initial to functools.reduce and apply it to all values."""returnreduce(func,self.values(),*initial)
std()
Returns standard deviation of all values.
Source code in ndicts\data_dict.py
100101102103104
defstd(self)->Number:"""Returns standard deviation of all values."""step=self.reduce(lambdaa,b:a+(b-self.mean())**2,0)step/=len(self)-1returnstep**0.5
total()
Returns sum of all values.
Source code in ndicts\data_dict.py
929394
deftotal(self):"""Returns sum of all values."""returnsum(self.values())