Last active
January 31, 2025 14:26
-
-
Save tim-schilling/16d16a5b4f86e954da2fbaa708429747 to your computer and use it in GitHub Desktop.
Djangonaut Space applicant availability parser
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
import csv | |
DAYS = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] | |
# Monday is 0 and Sunday is 6 for weekday. | |
SHIFTS = { | |
day: i*24 | |
for i, day in enumerate(DAYS) | |
} | |
MAX_HOURS = 168 # 24 * 7 | |
def timezone_to_utc(timezone: str) -> float: | |
# strip GMT from the start, split on the space | |
offset, _ = timezone.strip("GMT").split(" ", maxsplit=1) | |
# I'm too lazy to do this properly. Seems like most timezones are either | |
# at the hour or half-hour. | |
return float(offset.replace(':00', '').replace(':30', '.5')) | |
def parse_utc_hours(values: str, offset: float, shift: int) -> list[float]: | |
def adjust_for_offset(value: int) -> float: | |
adjusted = value + shift - offset | |
# We're treating the hours on a 0 to 167 scale. | |
# Monday at midnight to Sunday at 11pm. | |
# 168 hours takes you back to 0, Monday at Midnight. | |
return adjusted % MAX_HOURS | |
return [adjust_for_offset(int(value)) for value in values.split(', ')] | |
def generate_weekly_hours(hours: list[int], days: list[str]=None) -> list[int]: | |
""" | |
Take a list of hours in a 0-23 period and generate what those are for the | |
week. | |
This should be used when we collect hours for Navigators and Captains. | |
""" | |
if days is None: | |
days = DAYS | |
hours = list(sorted([value + SHIFTS[day] for value in hours for day in days])) | |
print(", ".join([str(v) for v in hours])) | |
return hours | |
# data.csv should be copied from the responses. | |
# Rename the columns to: | |
# Timezone, | |
with open('session_timezones.csv', mode='r') as f: | |
reader = csv.DictReader(f) | |
for row in reader: | |
offset = timezone_to_utc(row['Timezone']) | |
utc_hours = [] | |
for day in DAYS: | |
# Ignore cells with no data | |
if row[day]: | |
utc_hours += parse_utc_hours(row[day], offset, SHIFTS[day]) | |
print(",".join([str(v) for v in utc_hours])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment