1

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))
    

2 Answers2

2

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")
qkzk
  • 185
  • 1
  • 6
  • but my lead said write class only for `BankDataWriterBase()` why ??..is that possible?? –  Jan 11 '22 at 19:52
  • you can use `settattr` with a function I guess. That is quite strange :) – qkzk Jan 11 '22 at 19:56
  • I'll edit my answer to reflect this comment. Please update your question since it wasn't specified. – qkzk Jan 11 '22 at 19:56
  • What detail you want in this question?? –  Jan 11 '22 at 19:57
  • Exactly what you just said, "write class only for `BankDataWriterBase()`. My last edit does that. As I said, it's very strange since it doesn't look like a simple demonstration of `setattr`... – qkzk Jan 11 '22 at 20:02
0

the structure without implementations:

class Task:
    def __init__(self): # initialise all the instance variables (None in this case)
        pass # this this might need to be empty
    def run(self) -> None:
        pass

class BankDataWriterBase:
    def __init__(self, file_name: str, out_path: str, bank_id: str): 
    # you might wan't to set default values: file_name: str = "" for example
        self.input_path = file_name
        self.output_path = out_path
        self.bank_identifier = bank_id
    
    def write_file(self, file_name) -> str:
        pass

class BankDataWriterImpl(BankDataWriterBase): 
# inherit from BankDataWriterBase, all functions and variables from BankDataWriterBase are now callable from this class
    # as said in the other answer, this will be inherited so you don't need to have this
    def __init__(self, file_name: str, out_path: str, bank_id: str):
        super().__init__(file_name, out_path, bank_id) # call __init__ method of all the superclasses (in this case only BankDataWriterBase)

    def extract_json(self, filename: str):
        pass
Robin Dillen
  • 704
  • 5
  • 11