【发布时间】:2019-12-19 02:55:11
【问题描述】:
我使用老式函数来处理工资单,但现在我将其转移到具有更多功能的网络应用程序,例如工资单处理中的可调整设置。
目前正在阅读有关设计模式的信息,并决定尝试在我的一个项目模块中实现工厂模式,尽管我有点卡住了。目标是将条件参数应用于类属性。
要了解我的旧函数的外观,只需使用 pandas read_excel() 方法将 excel 文件转换为数据框:
def read_excel_files():
df_stylist_analysis = pd.read_excel(
Reports.objects.latest('stylist_analysis').stylist_analysis.path,
sheet_name=0, header=None, skiprows=4)
df_tips = pd.read_excel(
Reports.objects.latest('tips_by_employee').tips_by_employee.path,
sheet_name=0, header=None, skiprows=0)
df_hours1 = pd.read_excel(
Reports.objects.latest('hours_week_1').hours_week_1.path,
header=None, skiprows=5)
df_hours2 = pd.read_excel(
Reports.objects.latest('hours_week_2').hours_week_2.path,
header=None, skiprows=5)
df_retention = pd.read_excel(
Reports.objects.latest('client_retention').client_retention.path,
sheet_name=0, header=None, skiprows=8)
df_efficiency = pd.read_excel(
Reports.objects.latest('employee_service_efficiency').employee_service_efficiency.path,
sheet_name=0, header=None, skiprows=5)
return df_stylist_analysis, df_tips, df_hours1, df_hours2, df_retention, df_efficiency
如您所见,每个文件中跳过的行数不同。
目前我正在尝试实现一个我编写的 Excel 工厂来代替该函数,以便它可以处理多种 Excel 文件类型:
class XLSDataExtractor:
def __init__(self, filepath, rows):
self.data = pd.read_excel(filepath, sheet_name=0, header=None, skiprows=rows)
@property
def parsed_data(self):
return self.data
class XLSXDataExtractor:
def __init__(self, filepath, rows):
self.data = pd.read_excel(filepath, sheet_name=0, header=None, skiprows=rows)
@property
def parsed_data(self):
return self.data
def data_extraction_factory(filepath):
if filepath.endswith('xls'):
extractor = XLSDataExtractor
elif filepath.endswith('xlsx'):
extractor = XLSXDataExtractor
else:
raise ValueError(f'Cannot open {filepath}')
return extractor(filepath)
def extract_data_from(filepath):
factory_obj = None
try:
factory_obj = data_extraction_factory(filepath)
except ValueError as e:
print(e)
return factory_obj
def rows():
# if file name == human1 and filepath.endswith('xls') return 4
# if file name == human1 and filepath.endswith('xlsx') return 1
如您所见,我需要将rows 作为参数传递给类属性(我认为)。我该如何实施?我在通过这部分时有点挣扎。
【问题讨论】:
标签: python django python-3.x algorithm design-patterns