使用面向对象编程编写 Base class

Writing Base class using object oriented programming

我是面向对象编程的新手,我需要使用下面给出的 class 图在代码中编写 BankDataWriterBase 基础 class。我无法理解 class 图的全部内容,这里的任何人都可以知道并向我解释他们使用 class 图

实际上在说什么

在我的理解之后,我这样做了: 我知道我的做法是错误的,但我不知道错误发生在哪里

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

看到一些答案后,我更改了我的代码,如下所示

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

他们的意思是 BankDataWriterImpl 应该像这样继承自 BankDataWriterBase :

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

然后在驱动程序代码中,您可以创建一个 BankDataWriterImpl() 对象而不是像您那样创建 BankDataWriterBase()

它将从父级继承其 __init__ 方法并拥有一个新的 extract_json 方法。

您没有提到的另一个问题来自 BankDataWriterBase 属性。您的代码假定存在 3 个全局变量。 它们应该在您创建对象时传递给 class,如下所示:

但是在创建 BankSomething 对象时要小心,因为现在需要这些参数:

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)

评论后编辑:但我的领导说只为 BankDataWriterBase()

写 class
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")

没有实现的结构:

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