-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathedge.py
More file actions
123 lines (104 loc) · 3.19 KB
/
edge.py
File metadata and controls
123 lines (104 loc) · 3.19 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import copy
from typing import Union
from visualiser import Node
class Edge:
def __init__(self, source_node: Union[Node, str], destination_node: Union[Node, str], label: str = '',
**attrs: str) -> None:
self._source_node = Node(source_node) if isinstance(source_node, str) else copy.deepcopy(source_node)
self._destination_node = Node(destination_node) if isinstance(destination_node, str) else copy.deepcopy(
destination_node)
self._name = f"{self._source_node.name} -> {self._destination_node.name}"
self._label = label
self._attrs = attrs
@property
def label(self) -> str:
"""
Get label for edge.
:return: str
"""
return self._label
@label.setter
def label(self, _label: str) -> None:
"""
Set label for edge.
:param _label: str
"""
self._label = _label
@property
def name(self) -> str:
"""
Get name for edge.
:return: str
"""
return self._name
@name.setter
def name(self, _name: str) -> None:
"""
Set label for edge.
:param _name: str
"""
self._name = _name
@property
def source_node(self) -> Node:
"""
Get source node
:return: Node
"""
return self._source_node
@source_node.setter
def source_node(self, _source_node: Node) -> None:
"""
Set source node.
:param _source_node: Node
"""
self._source_node = _source_node
@property
def destination_node(self) -> Node:
"""
Get destination node.
:return: Node
"""
return self._destination_node
@destination_node.setter
def destination_node(self, _destination_node: Node) -> None:
"""
Sets destination node.
:param _destination_node: Node
"""
self._destination_node = _destination_node
def get_attribute(self, key: str) -> str:
"""
Get attribute for edge.
:param key: str
:return: The value of attribute with key
"""
return self._attrs.get(key)
def set_attribute(self, key: str, value: str) -> None:
"""
Set attribute for edge
:param key: str
:param value: str
"""
self._attrs[key] = value
def remove_attribute(self, key: str) -> None:
"""
Remove attribute from edge.
:param key: str
"""
if self._attrs.get(key):
del self._attrs[key]
def get_attributes_string(self) -> str:
"""
Get attributes string enclosed in []
:return:
"""
if len(self._label) == 0:
return '[' + ', '.join([f'{key}="{value}"' for key, value in self._attrs.items()]) + '];'
return '[' + f'label="{self._label}", ' + ', '.join(
[f'{key}="{value}"' for key, value in self._attrs.items()]) + '];'
def to_string(self) -> str:
"""
Converts dot string equivalent of the current edge.
:return: Str
"""
return f'{self._source_node.name} -> {self._destination_node.name} {self.get_attributes_string()}'