Home > Mobile >  Extend python staticmethod in other class
Extend python staticmethod in other class

Time:11-30

I have two classes which are responsible for some calculations. In first class I have calculateStatisticalFeatures static method which calculate come stuff and returns a DataFrame for me. In other class I would like to do almost the same but add one more calculation basing on the same new input data from second class. I found something like decorators but somehow I was not able to use it.

Method in first class:

@staticmethod
def calculateStatisticsFeatures(self, inputData) -> pd.DataFrame:
    #some calculations
    features = pd.DataFrame(np.array([[skewn, kurt, entropyVal, meanCalc]]), columns=['skewness', 'kurtosis','entropy', 'meanCalc'])
    return features

I was trying to use decorator like this to extend my first class method in second class but I can't paste the data.

@firstClass.calculateStatisticalFeatures(self.inputData)
def TestDecor(self):
    # new calculation

Is it somehow possible to add this calculations to second class? Thank you in advance :)

CodePudding user response:

Maybe this is what you want?

>>> import functools
>>> 
>>> class A():
...     @staticmethod
...     def test(func):
...             @functools.wraps(func)
...             def wrapper(*args, **kw):
...                     # features = pd.DataFrame(np.array([[skewn, kurt, entropyVal, meanCalc]]), columns=['skewness', 'kurtosis','entropy', 'meanCalc'])
...                     print(kw.get("inputData"))
...                     return func(*args, **kw)
...             return wrapper
... 
>>> class B(A):
...     @A.test
...     def testb(self, **kw):
...             print('test')
... 
>>> 
>>> B().testb(inputData="inputData")
inputData
test

CodePudding user response:

I removed the staticmethod and just inherited second class. I was not aware that in Python we can inherit from classes in one class.

  • Related