dimanche 3 septembre 2017

Refactor some python code to strategy design pattern

Here are two functions,which is quite similiar.The type of dtIndex is pandas DatetimeIndex the only difference is the i.dayofyear part.There are day of week,month of year,week of month,week of year still need to implement.

def extract_day_of_year(dtIndex):
    dayofyear_arr = []
    for i in dtIndex:
        dayofyear_arr.append(i.dayofyear)
    return dayofyear_arr

def extract_day_of_month(dtIndex):
    dayofmonth_arr = []
    for i in dtIndex:
        dayofmonth_arr.append(i.dayofmonth)
    return dayofmonth_arr

Is there any way better than switch according to a additional str parameter?

Aucun commentaire:

Enregistrer un commentaire