Skip to content

Commit

Permalink
update tree.py to pass pylint
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbailey committed Feb 4, 2019
1 parent 68bb352 commit 6ba21be
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
31 changes: 26 additions & 5 deletions rail/tree.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,57 @@
"""
A class to implement a tree structure
"""
from collections import OrderedDict, UserDict

class Tree(UserDict):
def __init__(self, name: str, parent=None, sort: bool=True) -> None:
class Tree(UserDict): # pylint: disable=too-many-ancestors
"""
A class to implement a tree structure
"""
def __init__(self, name: str, parent=None, sort: bool = True) -> None:
UserDict.__init__(self)
self.data = {}
self.name = name
self.parent = parent
self.sort = sort

def addChild(self, name: str) -> 'Tree':
def add_child(self, name: str) -> 'Tree':
"""
Add a child to the tree
"""
self.data[name] = Tree(name=name, parent=self, sort=self.sort)
if self.sort:
self.data = OrderedDict(sorted(self.data.items(), key=lambda x: x[1].name))
return self.data[name]

def path(self) -> str:
"""
Print the path from the root to the child
"""
if self.parent:
return self.parent.path() + '/' + self.name
else:
return '/' + self.name
return '/' + self.name

def to_print(self) -> None:
"""
Print all of a tree
"""
print(self.path())
for child in self.data.values():
child.to_print()

def to_latex(self) -> None:
"""
Print a tree in LaTeX format
"""
print('child { node{' + self.name + '}')
for child in self.data.values():
child.to_latex()
print('}')

def to_dict_list(self) -> dict:
"""
Print a tree in an alternating dict list format
"""
outdict = {}
outdict['name'] = self.name
outdict['children'] = []
Expand Down
2 changes: 1 addition & 1 deletion tests/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TestTree(unittest.TestCase):
"""
def setUp(self):
self.tree = Tree(name='test tree')
self.tree.addChild('test child')
self.tree.add_child('test child')

def test_tree(self):
"""
Expand Down

0 comments on commit 6ba21be

Please sign in to comment.