-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsessions.py
149 lines (119 loc) · 3.85 KB
/
sessions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import codecs
import re
from dateutil.parser import parse
session_pattern = re.compile("\s*!\s*(Session\s*\d+)")
session_name_pattern = re.compile("(\'.\d+)\:(.*)\'")
time_pattern = re.compile("(\d\d\:\d\d)\-(\d\d\:\d\d)")
schedule_block_info = []
SESSION_ID = 0
SESSION_NAME = 1
class Event(object):
def __init__(self, index, start_time):
self.start_time = start_time
self.index = index
def __repr__(self):
'%s %d' % (self.start_time, self.index)
class ScheduleBlock(object):
def __init__(self, start_time, end_time, block_name, sessions):
self.start_time = parse(start_time)
self.end_time = parse(end_time)
self.block_name = block_name
self.sessions = sessions
def traverse_schedule(schedule):
for line in schedule:
yield line
raise StopIteration()
def get_session_info(session, line):
block_name = None
start_time = ""
end_time = ""
block_name = session.group(1)
the_times = time_pattern.search(line)
if the_times:
start_time = the_times.group(1)
end_time = the_times.group(2)
return block_name, start_time, end_time
def generate_schedule_info(wiki):
"""
args
wiki: list of strings representing the wiki schedule
"""
def get_them_sessions(session_block):
them_sessions = []
for line in session_block:
if line[0] == "!":
break
if line.find("header") != -1:
# okay we are a session
result = session_name_pattern.search(line)
if result:
data = (result.group(1), result.group(2))
else:
data = line[2:].split("|")
data = ("bo", data[1])
them_sessions.append(data)
return them_sessions
session_blocks = []
schedule_gen = traverse_schedule(wiki)
for line in schedule_gen:
result = session_pattern.search(line)
if result:
block_name, start_time, end_time = get_session_info(result, line)
sessions = get_them_sessions(schedule_gen)
block = ScheduleBlock(start_time, end_time, block_name, sessions)
schedule_block_info.append(block)
return
def get_schedule_info(event):
"""
get schedule information associated with event
args:
event : data structure representing an event
return:
tuple : (name, session_id, session_name)
name - string
session_id - string
session_name - string
"""
def get_time(event):
# a way to abstract the internals of an event structure
# we don't know what events will eventually
# look like. We will override this function
return parse(event.start_time)
def get_index(event):
# ditto
return event.index
name = ""
session_id = ""
session_name = ""
result = None
event_time = get_time(event)
index = get_index(event)
for block in schedule_block_info:
if event_time >= block.start_time and event_time < block.end_time:
result = (block.block_name,
block.sessions[index][SESSION_ID],
block.sessions[index][SESSION_NAME])
break
return result
def open_schedule(file_name):
with codecs.open(file_name,encoding="utf-8") as fp:
wiki_schedule = [line.strip() for line in fp]
return wiki_schedule
def print_schedule_block():
"""
this is a test of the data structure
"""
return
def testSchedule():
wiki = open_schedule("data/friday.wiki")
generate_schedule_info(wiki)
e = Event(2,"11:00")
print get_schedule_info(e)
e = Event(9,"11:30")
print get_schedule_info(e)
e = Event(0,"12:30")
print get_schedule_info(e)
def main():
testSchedule()
if __name__ == "__main__":
main()