pythonclassoopinheritanceobject-oriented-analysis

Writing Base class using object oriented programming


Link for my initial part of this question

I am new to object-oriented programming I need to write a BankDataWriterBase base class in the following program using the class diagram given in below to the code. I cannot understand the complete thing that the class diagram has, anybody here to know & explain to me what they actually saying using the class diagram

enter image description here After my understanding I have done like this : I know my approach is wrong but i don't know where the mistake happening

import pandas as pd

class ExcelParser:
    def __init__(self):
        self.config = []       
    
    def extract(self, file_name):
        raw_excel=pd.read_excel(file_name,sheet_name=None, na_values= None, keep_default_na=False)
        return [x for k, v in raw_excel.items() for x in v[v.columns.intersection(conf)].to_dict(orient='records')]

class BankDataWriterBase:
    def __init__(self):
        self.input_path = file_name
        self.output_path = out_path
        self.bank_identifier = bank_id
    
    def write_file(self, file_name):
        res = True
        return res

if __name__ == "__main__":
    conf = list(input("ENTER THE LIST HERE : ").split(','))
    file_name = input("Enter the full input path here : ")
    out_path = input("Enter the full path for the output : ")
    bank_id = input("Enter the Bank ID : ")
    obj = ExcelParser()
    obj.config = conf
    print(obj.extract(file_name))
    
    obj1 = BankDataWriterBase()
    obj1.output_path =  out_path
    obj1.bank_identifier = bank_id    
    print(obj1.write_file(file_name))
    

After seeing some of the answers i changed my code like the following

import pandas as pd

class ExcelParser:
    def __init__(self):
        self.config = []       
    
    def extract(self, file_name):
        raw_excel=pd.read_excel(file_name,sheet_name=None, na_values= None, keep_default_na=False)
        return [x for k, v in raw_excel.items() for x in v[v.columns.intersection(conf)].to_dict(orient='records')]

class BankDataWriterBase:
    def __init__(self,file_name,out_path,bank_id):
        self.input_path = file_name
        self.output_path = out_path
        self.bank_identifier = bank_id
    
    def write_file(self, file_name:str):
        res = True
        return res
    
class BankDataWriterImpl(BankDataWriterBase):
    def __init__(self, file_name, out_path, bank_id):
        super().__init__(file_name, out_path, bank_id)
    
    def extrac_json(self, file_name):
        pass

if __name__ == "__main__":
    conf = list(input("ENTER THE LIST HERE : ").split(','))
    file_name = input("Enter the full input path here : ")
    out_path = input("Enter the full path for the output : ")
    bank_id = input("Enter the Bank ID : ")
    obj = ExcelParser()
    obj.config = conf
    print(obj.extract(file_name))
    
    obj1 = BankDataWriterBase()
    obj1.output_path =  out_path
    obj1.bank_identifier = bank_id    
    print(obj1.write_file(file_name))
    


Solution

  • What they mean is that BankDataWriterImpl should inherit from BankDataWriterBase like so :

    class BankDataWriterBase():
        ...
    
    class BankDataWriterImpl(BankDataWriterBase):
        # this class inherit from parent class BankDataWriterBase
        # when a `BankDataWriterBase` object is created, parent.__init__ method is executed.
        def extract_jon(self, filename: str):
            pass
    

    then in driver code, you can create a BankDataWriterImpl() object instead of the BankDataWriterBase() as you did.

    It will inherit its __init__ method from parent and have a new extract_json method.

    Another problem you didn't mention come from BankDataWriterBase attributes. Your code assume the existance of 3 global variables. They should be passed to the class when you create the object, like so :

    But watchout when creating a BankSomething object, since those arguments are now expected :

    class BankDataWriterBase:
         def __init__(self, input_path, output_path, bank_identifier):
            self.input_path = input_path
            self.output_path = output_path
            self.bank_identifier = bank_identifier
    
    ...
    
    obj1 = BankDataWriterImpl(input_path, output_path, bank_identifier)
    

    Edit after comment : but my lead said write class only for BankDataWriterBase()

    class BankDataWriterBase:
         def __init__(self, input_path, output_path, bank_identifier):
            self.input_path = input_path
            self.output_path = output_path
            self.bank_identifier = bank_identifier
    
    ...
    
    def write_file(file_name: str):
        pass
    
    obj = BankDataWriterBase(input_path, output_path, bank_identifier)
    # setattr add a new attribute to `obj`, first argument is the object,
    # second argument its name (obj.name)
    # third argument the attribute itself
    # here we attach a new method `obj.write_file` to the object
    setattr(obj, 'write_file', write_file)
    
    # now you can use it like that :
    # this line would have raised an exception before the `setattr` line
    obj.write_file("correct_file_path")