From 80261ff066c7704d0ff5b90ef496be2735a12e95 Mon Sep 17 00:00:00 2001 From: Midgard Date: Fri, 16 Oct 2020 16:07:03 +0200 Subject: [PATCH] Add what's hot --- README | 1 + whats_hot.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 README create mode 100755 whats_hot.py diff --git a/README b/README new file mode 100644 index 0000000..755a2c1 --- /dev/null +++ b/README @@ -0,0 +1 @@ +whats_hot.py is wat je wil diff --git a/whats_hot.py b/whats_hot.py new file mode 100755 index 0000000..21c1825 --- /dev/null +++ b/whats_hot.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python3 + +import json +from datetime import date + + +MONTHS = "JFMAMJJASOND" +LEVEL_SYMBOLS = " ░▓█" + +current_month_0 = date.today().month - 1 + + +def score(item): + name, months = item + # Sort by score of current month, then by name + return (months[current_month_0], name) + + +def ranked_data(data): + return list(sorted(data.items(), key=score)) + + +def color_for_month(month, text): + if month == current_month_0: + return text + return f"\033[90m{text}\033[0m" + + +def format(item): + name, months = item + + return "{} {}".format( + " ".join(color_for_month(month, LEVEL_SYMBOLS[level]) for month, level in enumerate(months)), + name + ) + + +def month_header(): + return " ".join(color_for_month(i, m) for i, m in enumerate(MONTHS)) + + +def main(): + with open("./fruit.json") as f_in: + fruit = json.load(f_in) + with open("./groenten.json") as f_in: + groenten = json.load(f_in) + + print("Fruit") + print(month_header()) + for item in ranked_data(fruit): + print(format(item)) + + print() + print("Groenten") + print(month_header()) + for item in ranked_data(groenten): + print(format(item)) + print(month_header()) + +if __name__ == '__main__': + main()