I have written a code as follows that takes in different inputs in the __init__ of the class and functions that come afterwards use the key arguments for a certain process (I have brought an example here but the original code has more functions and more files to process).
import os
from config import data_directory #directory where the datasets are located
import pandas as pd
class Analysis(object):
def __init__(self, **kwargs):
self.data1 = kwargs.get('data1')
self.data2 = kwargs.get('data1')
def analysis1(self):
print(self.data1['col1'],
self.data2['col1'])
def analysis2(self):
print(self.data2['col3'])
def analyzing():
tobeanalyzed = Analysis()
tobeanalyzed.analysis1()
tobeanalyzed.analysis2()
return tobeanalyzed
if __name__ == '__main__':
analyzing(data1 =pd.read_csv(os.path.join(data_directory , 'file1.csv'), na_filter = False),
data2 = pd.read_csv(os.path.join(data_directory , 'file2.csv'), na_filter = False))
I guess there is something wrong with the way I have defined the data1 and data2 but do not know how to correct it.
Error:
TypeError: analyzing() got an unexpected keyword argument 'data1'
>Solution :
You need to allow **kwargs on the analyzing function as well and pass them on to the Analysis constructor:
import os
from config import data_directory #directory where the datasets are located
import pandas as pd
class Analysis(object):
def __init__(self, **kwargs):
self.data1 = kwargs.get('data1')
self.data2 = kwargs.get('data1')
def analysis1(self):
print(self.data1['col1'],
self.data2['col1'])
def analysis2(self):
print(self.data2['col3'])
def analyzing(**kwargs):
tobeanalyzed = Analysis(**kwargs)
tobeanalyzed.analysis1()
tobeanalyzed.analysis2()
return tobeanalyzed
if __name__ == '__main__':
analyzing(data1 =pd.read_csv(os.path.join(data_directory , 'file1.csv'), na_filter = False),
data2 = pd.read_csv(os.path.join(data_directory , 'file2.csv'), na_filter = False))