-
Notifications
You must be signed in to change notification settings - Fork 0
/
StackOverFlow Question prep.txt
88 lines (76 loc) · 2.74 KB
/
StackOverFlow Question prep.txt
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
I'm using Python 3 to parse a file that contains a list of key value pairs. One pair per line.
The keys are known as markers, and begin with a backslash.
This file describes the keys used in other files in something like the way a dtd describes an xml file.
I've omitted most key value pairs for clarity.
The key value pairs are grouped together, each group starts with a key: \+mkr and ends with a key \-mkr
The value of the first key is the marker that is being described.
The data describes the hierarchy of the markers that's the information I'm trying to parse.
\+mkr lx
\nam Entry
\mkrOverThis
\-mkr
\+mkr ps
\nam Part of speech
\mkrOverThis lx
\-mkr
\+mkr ex
\nam Example
\mkrOverThis ps
\-mkr
This is code I have:
def read_typ_file(filein):
typ, filetype = process_input_file(filein, '.typ')
defined_markers = []
Marker = namedtuple('Marker',['mkr','nam','mkrOverThis'])
# Little error checking here, but file computer generated so usually consistent.
for entry in typ:
mkr = name = parent = None
for field in entry:
key , value = field
print('key is : {}, and value is {}'.format(key,value))
if key == '\\+mkr':
mkr = value
if key == '\\nam':
name = value
if key == '\\mkrOverThis':
parent = value
if key == '\\-mkr':
this_marker = Marker(mkr,name,parent)
defined_markers.append(this_marker)
groups = dict()
print("HERE")
for marker in defined_markers:
print(marker)
print(marker[2])
#continue
for marker in defined_markers:
print("There are {} defined markers.\nGroups are: {}".format(len(defined_markers),groups))
if marker.mkrOverThis not in groups.keys():
groups[marker.mkrOverThis] = [marker.mkr]
elif marker.mkrOverThis:
groups[marker.mkrOverThis].append(marker.mkr)
else :
raise ValueError("There seems to be a problem with marker {} in read_typ_file.".format(marker))
print("\nGroups found are as follows:")
for group in groups:
print("Marker for parent group: {}".format(group))
print("{}".format(groups[group]))
# Little error checking here, but file computer generated so usually consistent.
for entry in typ:
for field in entry:
key , value = field
print('key is : {}, and value is {}'.format(key,value))
marker_dict = {'marker' : None,'name' : None,'language' : None,'parent_marker' : None,'character_style' : None}
if key == '\\+mkr':
marker_dict.update([['marker', value]])
if key == '\\nam':
marker_dict['name'] = value
if key == '\\lng':
marker_dict['language'] = value
if key == '\\mkrOverThis':
marker_dict.update([['parent_marker', value]])
if key == '\\CharStyle':
marker_dict.update([['character_style', value]])
print('marker_dict is {}'.format(marker_dict))
if key == '\\-mkr':
defined_markers.append(marker_dict)