Guest User

Untitled

a guest
Aug 8th, 2018
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.56 KB | None | 0 0
  1. class Node:
  2.     """
  3.    Tree data-structure that has each directory as a node,
  4.    each subdirectory as children and files as leaves.
  5.    """
  6.  
  7.     def __init__(self, name, parent, children=[]):
  8.         self.name = name
  9.         self.parent = parent
  10.         self.children = children
  11.  
  12.     def add_children(self, children):
  13.         self.children.append(children)
  14.  
  15.     def search_name(self, name):
  16.         if name == self.name:
  17.             return self
  18.         for child in self.children:
  19.             if child.name == name:
  20.                 return child
  21.         for child in self.children:
  22.             found_node = child.search_name(name)
  23.             if found_node is not None:
  24.                 return found_node
  25.         return None
  26.  
  27.     def get_files(self, initials):
  28.         print('---------------------------------------')
  29.         for child in self.children:
  30.             name = initials + os.sep+child.name
  31.             # FIXME: What happens in case of empty directories?
  32.             print('child:', child.name)
  33.             print('     children:', child.children)
  34.             for x in child.children:
  35.                 print('\t\t\t', x.name)
  36.             if child.children == []:
  37.                 print('yielding')n
  38.                 yield name
  39.             else:
  40.                 print('inside else')
  41.                 child.get_files(name)
  42.                 print('returned')
  43.  
  44.     def get_files_cd(self, initials):
  45.         for child in self.children:
  46.             name = initials+os.sep+child.name
  47.             if child.children == []:
  48.                 yield name
Add Comment
Please, Sign In to add comment