Greatly cleaned up and simplified core logic
This commit is contained in:
@@ -59,13 +59,16 @@ class StatsModel:
|
||||
return total
|
||||
|
||||
def calculate_total_original_post_notes_by_month_and_year(self) -> Dict[str, int]:
|
||||
# https://docs.python.org/3/library/collections.html#defaultdict-objects
|
||||
date_map: Dict[str, Any] = {}
|
||||
date_map = defaultdict(lambda: {'note_count': 0,
|
||||
'post_count': 0},
|
||||
date_map)
|
||||
|
||||
# Gathering the results.
|
||||
for post_key in self.original_post_map:
|
||||
post = self.original_post_map[post_key]
|
||||
# Format is like 2025-12-28 20:00:34 GMT
|
||||
# Format is like '2025-12-28 20:00:34 GMT'
|
||||
post_date: datetime = datetime.strptime(
|
||||
post['date'], '%Y-%m-%d %H:%M:%S %Z')
|
||||
post_date_key = f"{post_date.year}-{post_date.month:02}"
|
||||
@@ -74,6 +77,7 @@ class StatsModel:
|
||||
sts['post_count'] += 1
|
||||
sts['note_count'] += post['note_count']
|
||||
|
||||
# Results postprocessing.
|
||||
for date in date_map:
|
||||
sts = date_map[date]
|
||||
post_count = sts['post_count']
|
||||
@@ -83,10 +87,13 @@ class StatsModel:
|
||||
return date_map
|
||||
|
||||
def determine_most_popular_tags(self) -> List[Dict[str, Any]]:
|
||||
# https://docs.python.org/3/library/collections.html#defaultdict-objects
|
||||
tag_dict: Dict[str, Any] = {}
|
||||
tag_dict = defaultdict(lambda: {'note_count': 0,
|
||||
'post_count': 0},
|
||||
tag_dict)
|
||||
|
||||
# Gathering the results.
|
||||
for post_key in self.original_post_map:
|
||||
post = self.original_post_map[post_key]
|
||||
tags = post['tags']
|
||||
@@ -96,12 +103,13 @@ class StatsModel:
|
||||
sts['post_count'] += 1
|
||||
sts['note_count'] += post['note_count']
|
||||
|
||||
# Results postprocessing.
|
||||
for tag in tag_dict:
|
||||
sts = tag_dict[tag]
|
||||
post_count = sts['post_count']
|
||||
note_count = sts['note_count']
|
||||
sts['notes_to_posts_ratio'] = note_count / post_count
|
||||
|
||||
tag_list = sorted(list(tag_dict.values()), key=itemgetter('note_count'),
|
||||
reverse=True)
|
||||
return tag_list
|
||||
# https://stackoverflow.com/a/73050
|
||||
return sorted(list(tag_dict.values()), key=itemgetter('note_count'),
|
||||
reverse=True)
|
||||
|
||||
Reference in New Issue
Block a user