groenten-en-fruit/whats_hot.py

62 lines
1.2 KiB
Python
Raw Normal View History

2020-10-16 16:07:03 +02:00
#!/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 how exclusive it is, then by name
return (months[current_month_0], -sum(months), name)
2020-10-16 16:07:03 +02:00
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()