Created
March 3, 2022 13:05
-
-
Save LukasWoodtli/c7f0a3893e563aedb56eef35a98c3927 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
"""Create labels with week numbers and dates""" | |
import os.path | |
from datetime import timedelta, date | |
import locale | |
locale.setlocale(locale.LC_ALL, 'DE_CH') | |
def get_first_monday_in_year(year): | |
current_day = date(year, 1, 1) | |
isoweek_day = current_day.isoweekday() | |
current_day += timedelta(days=1 - isoweek_day) | |
if current_day.year == year - 1: | |
current_day += timedelta(weeks=1) | |
return current_day | |
def get_mondays_in_year(year): | |
monday = get_first_monday_in_year(year) | |
yield monday | |
monday += timedelta(days=7) | |
while monday.year == year: | |
yield monday | |
monday += timedelta(days=7) | |
def create_week_texts(): | |
year = 2022 | |
texts = [] | |
for day in get_mondays_in_year(year): | |
if day < date(2022, 4, 1): continue | |
text = create_week_text(day) | |
texts.append(text) | |
return texts | |
def create_week_text(first_day_of_week): | |
week = first_day_of_week.isocalendar().week | |
text = f"Woche {week}: " | |
text += f"{first_day_of_week.strftime('%A %x')} - " | |
last_day_of_week = first_day_of_week + timedelta(days=6) | |
text += f"{last_day_of_week.strftime('%A %x')}" | |
return text | |
ADDITIONAL_LABELS = [ | |
"Anderes", | |
"Später / Vielleicht / Irgendwann", | |
"Warten auf", | |
"Eingang" | |
] | |
def create_all_labels(): | |
labels = create_week_texts() | |
labels.extend(ADDITIONAL_LABELS) | |
return labels | |
def write_all_labels_as_csv(labels): | |
csv_file_path = os.path.dirname(__file__) | |
csv_file_path = os.path.join(csv_file_path, "labels.csv") | |
with open(csv_file_path, 'w') as csvfile: | |
title = "Labels" | |
labels_list = labels | |
labels_list.insert(0, title) | |
csvfile.writelines('\n'.join(labels_list)) | |
def main(): | |
labels = create_all_labels() | |
write_all_labels_as_csv(labels) | |
if __name__ == "__main__": # pragma: no cover | |
main() | |
# test code | |
# inspired by: https://testandcode.com/147 | |
# To test: | |
# pip install pytest | |
# pytest week_numbers.py | |
# To test with coverage: | |
# put this file (week_numbers.py) in a directory by itself, say week_numbers | |
# then from the parent directory of foo: | |
# pip install pytest-cov | |
# pytest --cov=week_numbers week_numbers/week_numbers.py | |
# To show missing lines | |
# pytest --cov=week_numbers --cov-report=term-missing week_numbers/week_numbers.py | |
expected_week_list = ['Woche 14: Montag 04.04.2022 - Sonntag 10.04.2022', | |
'Woche 15: Montag 11.04.2022 - Sonntag 17.04.2022', | |
'Woche 16: Montag 18.04.2022 - Sonntag 24.04.2022', | |
'Woche 17: Montag 25.04.2022 - Sonntag 01.05.2022', | |
'Woche 18: Montag 02.05.2022 - Sonntag 08.05.2022', | |
'Woche 19: Montag 09.05.2022 - Sonntag 15.05.2022', | |
'Woche 20: Montag 16.05.2022 - Sonntag 22.05.2022', | |
'Woche 21: Montag 23.05.2022 - Sonntag 29.05.2022', | |
'Woche 22: Montag 30.05.2022 - Sonntag 05.06.2022', | |
'Woche 23: Montag 06.06.2022 - Sonntag 12.06.2022', | |
'Woche 24: Montag 13.06.2022 - Sonntag 19.06.2022', | |
'Woche 25: Montag 20.06.2022 - Sonntag 26.06.2022', | |
'Woche 26: Montag 27.06.2022 - Sonntag 03.07.2022', | |
'Woche 27: Montag 04.07.2022 - Sonntag 10.07.2022', | |
'Woche 28: Montag 11.07.2022 - Sonntag 17.07.2022', | |
'Woche 29: Montag 18.07.2022 - Sonntag 24.07.2022', | |
'Woche 30: Montag 25.07.2022 - Sonntag 31.07.2022', | |
'Woche 31: Montag 01.08.2022 - Sonntag 07.08.2022', | |
'Woche 32: Montag 08.08.2022 - Sonntag 14.08.2022', | |
'Woche 33: Montag 15.08.2022 - Sonntag 21.08.2022', | |
'Woche 34: Montag 22.08.2022 - Sonntag 28.08.2022', | |
'Woche 35: Montag 29.08.2022 - Sonntag 04.09.2022', | |
'Woche 36: Montag 05.09.2022 - Sonntag 11.09.2022', | |
'Woche 37: Montag 12.09.2022 - Sonntag 18.09.2022', | |
'Woche 38: Montag 19.09.2022 - Sonntag 25.09.2022', | |
'Woche 39: Montag 26.09.2022 - Sonntag 02.10.2022', | |
'Woche 40: Montag 03.10.2022 - Sonntag 09.10.2022', | |
'Woche 41: Montag 10.10.2022 - Sonntag 16.10.2022', | |
'Woche 42: Montag 17.10.2022 - Sonntag 23.10.2022', | |
'Woche 43: Montag 24.10.2022 - Sonntag 30.10.2022', | |
'Woche 44: Montag 31.10.2022 - Sonntag 06.11.2022', | |
'Woche 45: Montag 07.11.2022 - Sonntag 13.11.2022', | |
'Woche 46: Montag 14.11.2022 - Sonntag 20.11.2022', | |
'Woche 47: Montag 21.11.2022 - Sonntag 27.11.2022', | |
'Woche 48: Montag 28.11.2022 - Sonntag 04.12.2022', | |
'Woche 49: Montag 05.12.2022 - Sonntag 11.12.2022', | |
'Woche 50: Montag 12.12.2022 - Sonntag 18.12.2022', | |
'Woche 51: Montag 19.12.2022 - Sonntag 25.12.2022', | |
'Woche 52: Montag 26.12.2022 - Sonntag 01.01.2023'] | |
def test_texts(): | |
texts = create_week_texts() | |
assert expected_week_list == texts | |
def test_all(): | |
expected_week_list.extend(["Anderes", | |
"Später / Vielleicht / Irgendwann", | |
"Warten auf", | |
"Eingang"]) | |
labels = create_all_labels() | |
assert expected_week_list == labels |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment