-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyaml2txt.py
61 lines (51 loc) · 2.2 KB
/
yaml2txt.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
import yaml
class Report:
def __init__(self, modality, region, shortcut, history, technique, findings):
self.modality = modality
self.region = region
self.shortcut = shortcut
self.history = history
self.technique = technique
self.findings = findings
def de_md(self):
md = []
md.append(f"# Röntgen {self.region if self.modality == 'cr' else ''}: {self.shortcut}")
md.append(f"## Klinische Informationen\n{self.history}")
md.append(f"\n## Technik\n{self.technique}")
if self.modality=='cr':
md.append("Die rechtfertigende Indikation gemäß StrSchV §119 wurde durch die im Strahlenschutz fachkundige unterzeichende Person gestellt.")
md.append("\n## Befund")
for finding_category in self.findings:
for finding_category_name,findings in finding_category.items():
md.append(f"\n### {finding_category_name}")
for finding in findings:
if isinstance(finding, str):
md.append(f"{finding}: Unauffällig")
else:
for finding, answers in finding.items():
md.append(f"{finding}:")
for answer in answers['answers']:
md.append(f"- [ ] {answer}")
md.append("\n## Beurteilung")
return "\n".join(md)
def read_yaml(file_path):
with open(file_path, 'r') as stream:
try:
data = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
data = None
return data
def parse_yaml(data):
for modality in data['modality']:
if isinstance(modality, dict):
for regions in modality.values():
for region in regions:
report = Report(list(modality.keys())[0], region['region'], region['shortcut'], region['history'], region['technique'], region['findings'])
print(report.de_md())
def main():
yaml_file = 'textbausteine.yaml'
data = read_yaml(yaml_file)
parse_yaml(data)
if __name__ == "__main__":
main()