Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using **kwargs for multiple file upload

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading