Created
December 11, 2018 05:55
-
-
Save dcollien/c06339ae6c2c66b0db3e3b39819e4715 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
from datetime import datetime | |
import re | |
import string | |
def to_course_code(course_path, creation_date): | |
""" | |
Turns a course path into a short course code. | |
Max 14 characters (but likely under 10). | |
Not guaranteed to be unique | |
but has month/year of creation date, and single character hash | |
to prevent collisions in most situations | |
""" | |
course_slug = course_path.strip('/').split('/')[-1] | |
course_slug = course_slug.replace('-', ' ').replace('_', ' ') | |
course_slug = re.sub('['+string.punctuation+']', '', course_slug) | |
course_slug = re.sub("[^\w]", ' ', course_slug) | |
words = course_slug.split() | |
abbrv = ''.join([word[0] for word in words][:9]).upper() | |
chars = string.ascii_letters + digits | |
return abbrv + creation_date.strftime(r"%m%y") + chars[hash(course_path) % len(chars)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment