-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtopic_file_reader_task2.py
55 lines (44 loc) · 1.87 KB
/
topic_file_reader_task2.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
import csv
import xml.etree.ElementTree as ET
from Visualization.generate_html_file import HtmlGenerator
class Topic:
"""
This class shows a topic for task 1. Each topic has an topic_id which is str, a title and question which
is the question body and a list of tags.
"""
def __init__(self, topic_id, formula_id, formula_latex, title, question, tags):
self.topic_id = topic_id
self.formula_id = formula_id
self.formula_latex = formula_latex
self.title = title
self.question = question
self.lst_tags = tags
class TopicReader:
"""
This class takes in the topic file path and read all the topics into a map. The key in this map is the topic id
and the values are Topic which has 4 attributes: id, title, question and list of tags for each topic.
To see each topic, use the get_topic method, which takes the topic id and return the topic in Topic object and
you have access to the 4 attributes mentioned above.
"""
def __init__(self, topic_file_path):
self.map_topics = self.__read_topics(topic_file_path)
def __read_topics(self, topic_file_path):
map_topics = {}
tree = ET.parse(topic_file_path)
root = tree.getroot()
for child in root:
topic_id = child.attrib['number']
formula_id = child[0].text
formula_latex = child[1].text
title = child[2].text
question = child[3].text
tags = child[4].text.split(",")
map_topics[topic_id] = Topic(topic_id, formula_id, formula_latex, title, question, tags)
return map_topics
def get_topic(self, topic_id):
if topic_id in self.map_topics:
return self.map_topics[topic_id]
return None
# topic_id = "A.1"
# print(topic_reader.get_topic(topic_id).question)
# print(topic_reader.get_topic(topic_id).lst_tags)