-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstudent_record.py
89 lines (75 loc) · 2.72 KB
/
student_record.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
import os
def read_student_record(file_path):
if not os.path.exists(file_path):
print(f"File {file_path} does not exist. Creating a new file with default content.")
default_content = """
# Student Record
## Student Information
**Name:** Tim Lee
## Alerts
_No alerts yet._
## Knowledge
- **Variables:** Not demonstrated
- **Loops:** Not demonstrated
- **Recursion:** Not demonstrated
"""
with open(file_path, "w") as file:
file.write(default_content)
return default_content
with open(file_path, "r") as file:
return file.read()
def write_student_record(file_path, content):
with open(file_path, "w") as file:
file.write(content)
def format_student_record(student_info, alerts, knowledge):
record = "# Student Record\n\n## Student Information\n"
for key, value in student_info.items():
record += f"**{key}:** {value}\n"
record += "\n## Alerts\n"
if alerts:
for alert in alerts:
record += f"- **{alert['date']}:** {alert['note']}\n"
else:
record += "_No alerts yet._\n"
record += "\n## Knowledge\n"
for key, value in knowledge.items():
record += f"- **{key}:** {value}\n"
return record
def parse_student_record(markdown_content):
student_info = {}
alerts = []
knowledge = {}
current_section = None
lines = markdown_content.split("\n")
for line in lines:
line = line.strip() # Strip leading/trailing whitespace
if line.startswith("## "):
current_section = line[3:].strip()
elif current_section == "Student Information" and line.startswith("**"):
if ":** " in line:
key, value = line.split(":** ", 1)
key = key.strip("**").strip()
value = value.strip()
student_info[key] = value
elif current_section == "Alerts":
if "_No alerts yet._" in line:
alerts = []
elif line.startswith("- **"):
if ":** " in line:
date, note = line.split(":** ", 1)
date = date.strip("- **").strip()
note = note.strip()
alerts.append({"date": date, "note": note})
elif current_section == "Knowledge" and line.startswith("- **"):
if ":** " in line:
key, value = line.split(":** ", 1)
key = key.strip("- **").strip()
value = value.strip()
knowledge[key] = value
final_record = {
"Student Information": student_info,
"Alerts": alerts,
"Knowledge": knowledge
}
print(f"Final parsed record: {final_record}")
return final_record