Created
September 21, 2022 02:44
-
-
Save samiyuru/65cc4b18c1c7cdf4cce1774025191ccf to your computer and use it in GitHub Desktop.
This file contains 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 | |
# | |
# Author: Sami Menik, Fall 2022 | |
# | |
# The program parses the time stamp of the current directory and calculate extra credit for | |
# the submission. | |
# | |
# Usage: extra_credit [<extra-credit-deadline> <extra-credit>] ... | |
# | |
# Format of <extra-credit-deadline>: yyyy/mm/dd HH:MM:SS | |
# Type of <extra-credit> should be an int | |
import re | |
import os | |
import sys | |
from datetime import datetime | |
def cur_dir_name(): | |
directory_path = os.getcwd() | |
return os.path.basename(directory_path) | |
def extract_datetime_suffix(dir_name): | |
return dir_name[-31:-11] | |
def parse_datetime_str(datetime_str): | |
# Format: yyyy/mm/dd HH:MM:SS | |
return datetime.strptime(datetime_str, '%Y/%m/%d %H:%M:%S') | |
def parse_dir_datetime_suffix(dir_datetime_suffix): | |
datetime_parts = re.search('([a-z]+)_+([0-9]+)_+([0-9]+)_+([0-9]+)_+([0-9]+)_+([0-9]+)', dir_datetime_suffix, re.IGNORECASE) | |
month = datetime_parts.group(1) | |
date = datetime_parts.group(2) | |
hour = datetime_parts.group(3) | |
min = datetime_parts.group(4) | |
sec = datetime_parts.group(5) | |
year = datetime_parts.group(6) | |
parsable_datetime_str = '{}/{}/{} {}:{}:{}'.format(year, month, date, hour, min, sec) | |
return datetime.strptime(parsable_datetime_str, '%Y/%b/%d %H:%M:%S') | |
def parse_cur_dir_datetime_suffix(): | |
curr_dir_name = cur_dir_name() | |
dir_datetime_suffix = extract_datetime_suffix(curr_dir_name) | |
date_time = parse_dir_datetime_suffix(dir_datetime_suffix) | |
return date_time | |
def parse_extra_credit_deadline(deadline_str, credit): | |
deadline = parse_datetime_str(deadline_str) | |
credit = int(credit) | |
return (deadline, credit) | |
def parse_extra_credit_deadlines(args): | |
extra_credit_deadlines = [] | |
for i in range(0, len(args), 2): | |
deadline_str = args[i] | |
credit = args[i + 1] | |
deadline_credit_pair = parse_extra_credit_deadline(deadline_str, credit) | |
extra_credit_deadlines.append(deadline_credit_pair) | |
return extra_credit_deadlines | |
def find_best_extra_credit(extra_creidt_deadlines, curr_dir_datetime): | |
# Sort extra credit deadlines from earliest to latest. | |
extra_creidt_deadlines.sort(key=lambda deadline_credit_pair: deadline_credit_pair[0]) | |
# Find the earliest applicable deadline. | |
for deadline_credit_pair in extra_creidt_deadlines: | |
(deadline, credit) = deadline_credit_pair | |
if curr_dir_datetime <= deadline: | |
return credit | |
# No applicable extra credit deadline was found. | |
return 0 | |
def print_arg_format_error_msg(): | |
print('Call the program with the following args:', file=sys.stderr) | |
print('extra_credit [<extra-credit-deadline> <extra-credit>] ...', file=sys.stderr) | |
print('Format of <extra-credit-deadline>: yyyy/mm/dd HH:MM:SS', file=sys.stderr) | |
def print_extra_creidt_msg(extra_credit): | |
if extra_credit > 0: | |
print('The submission receives {} extra credits.'.format(extra_credit)) | |
else: | |
print('The submission does not receive extra credit.') | |
def main(): | |
args = sys.argv[1:] | |
if len(args) % 2 != 0: | |
print_arg_format_error_msg() | |
sys.exit(1) | |
try: | |
curr_dir_datetime = parse_cur_dir_datetime_suffix() | |
except: | |
print('Current directory does not have a valid date time suffix.', file=sys.stderr) | |
sys.exit(1) | |
try: | |
extra_creidt_deadlines = parse_extra_credit_deadlines(args) | |
except: | |
print_arg_format_error_msg() | |
sys.exit(1) | |
extra_credit = find_best_extra_credit(extra_creidt_deadlines, curr_dir_datetime) | |
print_extra_creidt_msg(extra_credit) | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment