-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.py
63 lines (52 loc) · 1.77 KB
/
Node.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
from Fragment import Fragment
class Node(object):
"""Has one parent;
Unless root; not _parent
Level represents _len() ******
Index represents _parent._children.index(self)
May have children
Sufficient conditions to create a tree structure"""
#Illustrate the purpose of this class
#Shouldn't use for each Node in a tree;
#Repeats calculations
@property
def is_root(self):
return not self._parent
@property
def index(self):
"""Which element in its sublings"""
#http://stackoverflow.com/q/8197323/1175496
#Be careful of exception; though it shouldn't happen
if not self._parent:
return 0
else:
#http://docs.python.org/2/glossary.html#term-hashable
#They equal themselves; it's taken care of
return self._parent._children.index(self)
@property
def level(self):
"""How 'far away from the root';
i.e. length of ancestor_chain"""
if not self._parent:
return 0
else:
return len(list(self.ancestor_chain))
@property
def ancestor_chain(self):
"""Recurse through each ancestor"""
if not self._parent:
return
else:
parent = self._parent
while parent:
yield parent
try:
parent = parent._parent
except Exception as e:
parent = None #Exit the loop
return
def __init__(self, *args, **kwargs):
self._parent = None
self._level = 0
self._index = 0
self._children = []