Skip to content

Commit b678cd8

Browse files
BishalsarangBishal Sarangkoti
authored and
Bishal Sarangkoti
committed
feat-custom-node: Add custom node class for node operations
1 parent 811a5a7 commit b678cd8

File tree

4 files changed

+89
-3
lines changed

4 files changed

+89
-3
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
test*
21
.idea
32
.idea
43
img

tests/test_node.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from visualiser import *
2+
3+
"""
4+
Test case class for nodes
5+
"""
6+
class TestNode:
7+
def test_create_new_node(self):
8+
node = Node('bishal', 'Bishal label', color='red', style='filled')
9+
10+
assert node.label == 'Bishal label'
11+
assert node.name == 'bishal'
12+
assert node.get_attribute('color') == 'red'
13+
assert node.get_attribute('style') == 'filled'
14+
assert node.to_string() == 'bishal [label="Bishal label", color="red", style="filled"];'
15+
16+
def test_set_attributes(self):
17+
node = Node('bishal', 'Bishal label', color='red', style='filled')
18+
assert node.get_attribute('color') == 'red'
19+
node.set_attribute('color', 'green')
20+
assert node.get_attribute('color') == 'green'
21+
node.set_attribute('background', 'grey')
22+
assert node.get_attribute('background') == 'grey'
23+
assert node.to_string() == 'bishal [label="Bishal label", color="green", style="filled", background="grey"];'
24+
25+
def test_rename_name_label(self):
26+
node = Node('bishal', 'Bishal label', color='red', style='filled')
27+
28+
node.set_attribute('color', 'green')
29+
30+
assert node.label == 'Bishal label'
31+
node.label = 'Bishal renamed label'
32+
assert node.label == 'Bishal renamed label'
33+
34+
assert node.name == 'bishal'
35+
node.name = 'Bishal renamed'
36+
assert node.name == 'Bishal renamed'
37+
38+
assert node.to_string() == 'Bishal renamed [label="Bishal renamed label", color="green", style="filled"];'
39+
40+

visualiser/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
21
# Version of the recursion-visualiser
3-
__version__ = "1.0.1"
2+
__version__ = "1.0.1"
3+
4+
from .node import Node
5+
from visualiser import *

visualiser/node.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Node:
2+
def __init__(self, name, label='', **attrs):
3+
# TODO: Add support for initialization of attributes from attribute dict
4+
self._name = name
5+
self._attrs = attrs
6+
7+
if len(label) == 0:
8+
self._label = name
9+
else:
10+
self._label = label
11+
12+
def __repr__(self):
13+
return f"Node('{self.name}')"
14+
15+
@property
16+
def name(self):
17+
return self._name
18+
19+
@name.setter
20+
def name(self, _name):
21+
self._name = _name
22+
23+
@property
24+
def label(self):
25+
return self._label
26+
27+
@label.setter
28+
def label(self, _label):
29+
self._label = _label
30+
31+
def get_attribute(self, key):
32+
return self._attrs[key]
33+
34+
def set_attribute(self, key, value):
35+
self._attrs[key] = value
36+
37+
def remove_attribute(self, key):
38+
del self._attrs[key]
39+
40+
def get_attributes_string(self):
41+
return '[' + f'label="{self._label}", ' + ', '.join(
42+
[f'{key}="{value}"' for key, value in self._attrs.items()]) + '];'
43+
44+
def to_string(self):
45+
return f'{self.name} {self.get_attributes_string()}'

0 commit comments

Comments
 (0)