Add what's hot

This commit is contained in:
Midgard 2020-10-16 16:07:03 +02:00
parent 3091d4759b
commit 80261ff066
Signed by: midgard
GPG key ID: 511C112F1331BBB4
2 changed files with 62 additions and 0 deletions

1
README Normal file
View file

@ -0,0 +1 @@
whats_hot.py is wat je wil

61
whats_hot.py Executable file
View file

@ -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()