snippetpythonMinor
Parse data into three logs
Viewed 0 times
threelogsintoparsedata
Problem
In the bit of code below:
```
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)
- How can I make
add_day,add_ weekandadd_monthwork 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)
Solution
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')
...Code Snippets
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')
...Context
StackExchange Code Review Q#77827, answer score: 4
Revisions (0)
No revisions yet.