mercredi 18 décembre 2019

Conditional parameters while using factory pattern

I was using old fashioned functions to process payroll but now I am moving it to a web-app with more features such as adjustable settings in the processing of the payroll.

Currently in the middle of reading about design patterns and decided to try to implement the factory pattern in one of my projects modules though I am slightly stuck. The goal is to apply conditional parameters to a classes attribute.

To give an idea of how my old function looked it simply converting excel files to dataframes using pandas read_excel() method:

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

As you can see there are different amounts of rows being skipped on each file.

Currently I am trying to implement an Excel factory I wrote in place of the function so that it can handle several Excel file types:

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

As you can see I need to pass in the rows as a parameter to the class attributes (I think). How can I implement this? I am having a bit of a struggle getting past this part.

Aucun commentaire:

Enregistrer un commentaire