This repository was archived by the owner on Mar 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdot_dsl.py
More file actions
55 lines (41 loc) · 1.48 KB
/
Copy pathdot_dsl.py
File metadata and controls
55 lines (41 loc) · 1.48 KB
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
NODE, EDGE, ATTR = range(3)
class Node:
def __init__(self, name, attrs):
self.name = name
self.attrs = attrs
def __eq__(self, other):
return self.name == other.name and self.attrs == other.attrs
class Edge:
def __init__(self, src, dst, attrs):
self.src = src
self.dst = dst
self.attrs = attrs
def __eq__(self, other):
return (self.src == other.src and
self.dst == other.dst and
self.attrs == other.attrs)
class Graph:
def __init__(self, data=[]):
self.nodes = []
self.edges = []
self.attrs = {}
if type(data) != list:
raise TypeError(r".+")
for d in data:
if type(d) != tuple or len(d) not in (3, 4):
raise TypeError(r".+")
if d[0] == NODE:
if len(d) != 3 or type(d[1]) != str or type(d[2]) != dict:
raise ValueError(r".+")
self.nodes.append(Node(*d[1:]))
elif d[0] == EDGE:
if len(d) != 4 or type(d[1]) != str or \
type(d[2]) != str or type(d[3]) != dict:
raise ValueError(r".+")
self.edges.append(Edge(*d[1:]))
elif d[0] == ATTR:
if len(d) != 3 or type(d[1]) != str or type(d[2]) != str:
raise ValueError(r".+")
self.attrs[d[1]] = d[2]
else:
raise ValueError(r".+")