Skip to content

Instantly share code, notes, and snippets.

@samclane
Created April 17, 2017 18:04
Show Gist options
  • Save samclane/bd0fc9c21a478381e4a32cafe4abafa4 to your computer and use it in GitHub Desktop.
Save samclane/bd0fc9c21a478381e4a32cafe4abafa4 to your computer and use it in GitHub Desktop.
Code for extracting date information from schedule format
WEEKDAY_CODES = {
'U': 6,
'M': 0,
'T': 1,
'W': 2,
'R': 3,
'F': 4,
'S': 5
}
@staticmethod
def _extract_datetime(schedule: str):
'''
:param schedule: str
:return: list
'''
schedule_regex = r"([UMTWRFS]+)([\d{2}:\d{2}]+)"
time_regex = r"(\d{2}:\d{2})+"
datetime = namedtuple('datetime', 'day hour')
datetime_schedule = []
for dt in re.findall(schedule_regex, schedule):
# for each match group
days = dt[0]
hours = dt[1]
for d in days:
dcode = WEEKDAY_CODES[d]
for time in re.findall(time_regex, hours):
datetime_schedule.append(datetime(dcode, time))
return datetime_schedule
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment