-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
74 lines (64 loc) · 2.2 KB
/
utils.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
import math
NO_PATH_EXISTS = "N/A"
INFINITY = math.inf
FRANK_WOLFE_STEPSIZE_PRECISION = 1e-5
class NotYetAttemptedException(Exception):
"""
This exception is used as a placeholder for code you should fill in.
"""
pass
class BadFileFormatException(Exception):
"""
This exception is raised if a network or demand file is in the wrong format or
expresses an invalid network.
"""
pass
def readMetadata(lines):
"""
Read metadata tags and values from a TNTP file, returning a dictionary whose
keys are the tags (strings between the <> characters) and corresponding values.
The last metadata line (reading <END OF METADATA>) is stored with a value giving
the line number this tag was found in. You can use this to proceed with reading
the rest of the file after the metadata.
"""
metadata = dict()
lineNumber = 0
for line in lines:
lineNumber += 1
line.strip()
commentPos = line.find("~")
if commentPos >= 0: # strip comments
line = line[:commentPos]
if len(line) == 0:
continue
startTagPos = line.find("<")
endTagPos = line.find(">")
if startTagPos < 0 or endTagPos < 0 or startTagPos >= endTagPos:
print("Error reading this metadata line, ignoring: '%s'" % line)
metadataTag = line[startTagPos+1 : endTagPos]
metadataValue = line[endTagPos+1:]
if metadataTag == 'END OF METADATA':
metadata['END OF METADATA'] = lineNumber
return metadata
metadata[metadataTag] = metadataValue.strip()
print("Warning: END OF METADATA not found in file")
return metadata
def path2linkTuple(pathString):
"""
Converts a path expressed as a sequence of nodes, e.g. [1,2,3,4] into a tuple
of link IDs for use with the Path object (see path.py), in this case
((1,2),(2,3),(3,4))
"""
# Remove braces
pathString = pathString[1:-1]
# Parse into node list
nodeList = pathString.split(",")
# Now create tuple
linkList = list()
prevNode = nodeList[0]
for i in nodeList[1:]:
curNode = i
linkID = '(' + prevNode + ',' + curNode + ')'
linkList.append(linkID)
prevNode = i
return tuple(linkList)