Skip to content

Instantly share code, notes, and snippets.

@dcollien
Created December 11, 2018 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcollien/c06339ae6c2c66b0db3e3b39819e4715 to your computer and use it in GitHub Desktop.
Save dcollien/c06339ae6c2c66b0db3e3b39819e4715 to your computer and use it in GitHub Desktop.
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