0

I have a function that will be used under different modules. There are two functions that take different arguments but the function logic is similar. I am trying to unite func1 and func2 functions into one.

Is there a way I can use the python functionality to handle this case?

func1

def create_data_file_removed_duplicate_item(packing_data, output_dir, warehouse_name, date, model_type):

    packing_data = packing_data.drop_duplicates(subset='item_id', keep="last")
    # create non duplicate item data file
    make_directory(output_dir)
    file_name_for_non_duplicate \
        = ap_conf.file_name_of_processed_data_non_duplicate_item\
        .format(warehouse_name=warehouse_name, date=date, model_type=model_type)
    file_path_for_non_duplicate = output_dir + file_name_for_non_duplicate
    packing_data.to_csv(file_path_for_non_duplicate, sep="\t", header=False, index=False)

    return file_path_for_non_duplicate

func2:

def create_data_file_without_duplicate_item(packing_data, output_dir, model_type):

    packing_data = packing_data.drop_duplicates(subset='item_id', keep="last")

    # create non-duplicate item data file

    make_directory(output_dir)
    file_name_for_non_duplicate \
        = ap_conf.file_name_of_processed_data_aggregated_warehouse_non_duplicate_item \
        .format(model_type=model_type)
    file_path_for_non_duplicate = output_dir + file_name_for_non_duplicate
    packing_data.to_csv(file_path_for_non_duplicate, sep="\t", header=False, index=False)

    logger.info('data count after removing duplicates: ' + str(len(packing_data)))
    return file_path_for_non_duplicate
2
  • 2
    Move warehouse_name and date to the end of the parameter list and make them optional. Then check if they are set inside the method. Something like create_data_file_without_duplicate_item(packing_data, output_dir, model_type, warehouse_name=None, date=None). Commented Jun 15, 2021 at 16:02
  • 2
    You can also just call one function from the other, though I can't necessarily speak to whether or not that is good practice. Commented Jun 15, 2021 at 16:04

1 Answer 1

1

Try this,

  • You can pass warehouse_name as default parameter.
  • Make a conditional call to file_name_for_non_duplicate and logger.info.

#New Func


def create_data_file_removed_duplicate_item(packing_data, output_dir,                                                
                                            model_type, warehouse_name = None, date = None):

    packing_data = packing_data.drop_duplicates(subset='item_id', keep="last")
    # create non duplicate item data file
    make_directory(output_dir)
    if warehouse_name:
        file_name_for_non_duplicate \
            = ap_conf.file_name_of_processed_data_non_duplicate_item\
            .format(warehouse_name=warehouse_name, date=date, model_type=model_type)
    else :
        file_name_for_non_duplicate \
            = ap_conf.file_name_of_processed_data_aggregated_warehouse_non_duplicate_item \
            .format(model_type=model_type)

    file_path_for_non_duplicate = output_dir + file_name_for_non_duplicate
    packing_data.to_csv(file_path_for_non_duplicate, sep="\t", header=False, index=False)

    if not warehouse_name:
        logger.info(
            'data count after removing duplicates: ' + str(len(packing_data)))

    return file_path_for_non_duplicate
Sign up to request clarification or add additional context in comments.

5 Comments

Can I usewarehouse_name = None, date=None instead? Actually, warehouse_name and date both are required in one case and not required in the else part.
Don't optional parameters have to be at the end of the list?
Right! both have to be at the end
@sam you can use warehouse_name = None, date=None. If string is empty or None it will return False. Also, Please correct that optional parameters should be at the end of the list.
It should be like if warehouse_name and date: Right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.