From f14776b5763d3ed877190c4dbff48cc867fe8592 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?tr=C3=A9meur?= Date: Sun, 22 Jan 2023 16:01:58 +0000 Subject: [PATCH] Add function for averaging ratings --- year-summary.py | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/year-summary.py b/year-summary.py index a3ea73a..ddf594e 100644 --- a/year-summary.py +++ b/year-summary.py @@ -6,6 +6,24 @@ from datetime import datetime thisyear = datetime.now().strftime("%Y") home = str(Path.home()) stats = [] +rates = [] + +rating = [] +rated = [] + +def rate(line): + if "and rated" in line: + rated.append(1) + if "★★★★★" in line: + rating.append(5) + elif "★★★★" in line: + rating.append(4) + elif "★★★" in line: + rating.append(3) + elif "★★" in line: + rating.append(2) + elif "★" in line: + rating.append(1) def iterate(category,action,year=thisyear,date=False): basedir = home + "/Documents/drive/org/journal/" + str(year) @@ -36,6 +54,7 @@ def iterate(category,action,year=thisyear,date=False): print(lines[num].strip() + " " + lines[0].strip()) else: print(lines[num].strip()) + rate(lines[num]) count += 1 else: break @@ -54,20 +73,28 @@ def iterate(category,action,year=thisyear,date=False): print(lines[num].strip() + " " + lines[0].strip()) else: print(lines[num].strip()) - count += 1 + rate(lines[num]) + count += 1 else: break else: break month += 1 stats.append(count) + if (sum(rated)) != 0: + avg = round((sum(rating)/sum(rated)),2) + else: + avg = "n/a" + rates.append(avg) + rating.clear() + rated.clear() def yeargrab(year=thisyear): iterate("books","read",year) iterate("films","watched",year) print("* stats") - print("** books read: " + str(stats[0])) - print("** films watched: " + str(stats[1])) + print("** books read: " + str(stats[0]) + ", average rating: " + str(rates[0])) + print("** films watched: " + str(stats[1]) + ", average rating: " + str(rates[1])) if __name__ == "__main__": yeargrab()