【发布时间】:2022-01-11 19:40:39
【问题描述】:
Link for my initial part of this question
我是面向对象编程的新手,我需要使用下面给出的类图在下面的代码中编写一个 BankDataWriterBase 基类。我无法理解类图所具有的完整内容,这里有任何人知道并向我解释他们使用类图实际在说什么
在我理解之后,我这样做了: 我知道我的方法是错误的,但我不知道错误发生在哪里
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))
【问题讨论】:
标签: python class oop inheritance object-oriented-analysis