\$\begingroup\$
\$\endgroup\$
4
In the bit of code below:
- How can I make
add_day
,add_ week
andadd_month
work as a single function? They are almost copy-paste of one another. I'm trying to follow the DRY rule. - How can I set this so that I don't use the variables
day
/week
/cur_day
?
import datetime
from collections import defaultdict
class ParsedData:
def __init__(self, date, score, status, item_name):
year, month, self.day = date.split("-")
self.month = datetime.date(int(year), int(month), int(self.day)).strftime("%B")
self.score = int(score)
self.status = status
self.item_name = item_name
class Log:
def __init__(self):
self.day = {}
self.week = {}
self.month = {}
self.counter = {}
self.default_data
def add_day(self, day, status, item_name, score):
day = 'Day %i' % day
if day not in self.day:
self.day[day] = {"Comp": defaultdict(int),
"Miss": defaultdict(int),
"Post": defaultdict(int),
"Add": defaultdict(int),
"Score": 0}
self.day[day][status][item_name] += 1
self.day[day]['Score'] = score
def add_week(self, week, status, item_name, score):
week = 'Week %i' % week
if week not in self.week:
self.week[week] = {"Comp": defaultdict(int),
"Miss": defaultdict(int),
"Post": defaultdict(int),
"Add": defaultdict(int),
"Score": 0}
self.week[week][status][item_name] += 1
self.week[week]['Score'] = score
def add_month(self, month, status, item_name, score):
if month not in self.month:
self.month[month] = {"Comp": defaultdict(int),
"Miss": defaultdict(int),
"Post": defaultdict(int),
"Add": defaultdict(int),
"Score": 0}
self.month[month][status][item_name] += 1
self.month[month]['Score'] = score
def update_views(log_file):
log = Log()
day = 0
cur_day = None
week = 1
for line in log_file:
parsed = ParsedData(*line.strip().split(","))
if cur_day != parsed.day:
cur_day = parsed.day
day += 1
if day % 7 == 0:
week += 1
log.add_day(day, parsed.status, parsed.item_name, parsed.score)
log.add_week(week, parsed.status, parsed.item_name, parsed.score)
log.add_month(parsed.month, parsed.status, parsed.item_name, parsed.score)
log_file = """2015-01-1,0,Add,DW_05
2015年01月2日,-1,Post,CR_02
2015年01月3日,-1,Comp,DIY_01
2015年01月3日,-1,Post,CD_01
2015年01月4日,-1,Miss,D_03
2015年01月4日,0,Miss,D_03
2015年01月4日,-1,Miss,CD_01
2015年01月4日,0,Miss,LS_04
2015年01月5日,1,Comp,DW_05
2015年01月6日,1,Comp,ANI_06
2015年01月6日,1,Comp,LS_04
2015年01月7日,1,Comp,NMW_07
2015年01月7日,1,Post,DW_05
2015年01月7日,1,Miss,LP_08
2015年01月8日,2,Post,CR_02
2015年01月8日,2,Miss,SEV_09
2015年01月10日,3,Comp,M_10
2015年01月10日,3,Add,NID_11
2015年01月11日,2,Add,ANI_06
2015年01月12日,1,Add,VF_12
2015年01月12日,0,Miss,DIY_01
2015年01月12日,1,Add,NID_11
2015年01月12日,0,Miss,D_03
2015年01月13日,1,Miss,SEV_09
2015年01月13日,2,Add,DW_05
2015年01月13日,1,Comp,NMW_07
2015年01月13日,1,Add,CPC_12""".splitlines()
update_views(log_file)
f.rodriguesf.rodrigues
asked Jan 17, 2015 at 17:25
1 Answer 1
\$\begingroup\$
\$\endgroup\$
To avoid repetitive code, make the Log
class do only one of the three things it currently does, and create three instances instead:
class Log(object):
def __init__(self, name):
self.name = name
self.data = {}
def add_item(self, key, status, item_name, score):
key = '%s %i' % (self.name, key)
if key not in self.data:
self.data[key] = {"Comp": defaultdict(int),
"Miss": defaultdict(int),
"Post": defaultdict(int),
"Add": defaultdict(int),
"Score": 0}
self.data[key][status][item_name] += 1
self.data[key]['Score'] = score
def update_views(log_file):
daily_log = Log('Day')
weekly_log = Log('Week')
monthly_log = Log('Month')
...
answered Jan 18, 2015 at 10:57
lang-py
self.month['January']
isself.month['February']
isself.day['Day 2']
... and so on. Why? \$\endgroup\$