cengal.data_containers.simple_tree.versions.v_0.simple_tree

Module Docstring Docstrings: http://www.python.org/dev/peps/pep-0257/

  1#!/usr/bin/env python
  2# coding=utf-8
  3
  4# Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>
  5# 
  6# Licensed under the Apache License, Version 2.0 (the "License");
  7# you may not use this file except in compliance with the License.
  8# You may obtain a copy of the License at
  9# 
 10#     http://www.apache.org/licenses/LICENSE-2.0
 11# 
 12# Unless required by applicable law or agreed to in writing, software
 13# distributed under the License is distributed on an "AS IS" BASIS,
 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15# See the License for the specific language governing permissions and
 16# limitations under the License.
 17
 18
 19"""
 20Module Docstring
 21Docstrings: http://www.python.org/dev/peps/pep-0257/
 22"""
 23
 24
 25__author__ = "ButenkoMS <gtalk@butenkoms.space>"
 26__copyright__ = "Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>"
 27__credits__ = ["ButenkoMS <gtalk@butenkoms.space>", ]
 28__license__ = "Apache License, Version 2.0"
 29__version__ = "4.4.1"
 30__maintainer__ = "ButenkoMS <gtalk@butenkoms.space>"
 31__email__ = "gtalk@butenkoms.space"
 32# __status__ = "Prototype"
 33__status__ = "Development"
 34# __status__ = "Production"
 35
 36
 37__all__ = ['PathDoesNotExist', 'tree_to_list', 'travers_tree', 'Tree']
 38
 39#!/usr/bin/env python
 40# coding=utf-8
 41
 42
 43
 44from typing import Iterable, Dict, Callable, Hashable
 45from collections import deque
 46
 47
 48
 49
 50
 51
 52class PathDoesNotExist(Exception):
 53    pass
 54
 55
 56def tree_to_list(tree: Dict)->Iterable[Hashable]:
 57    """
 58    Returns all items from a tree in a iterable container.
 59        tree = Tree()
 60        ...
 61        all_items =  tree_to_list(tree.get_subtree())
 62    Is an implementation of the Tree.get_all_children() method.
 63    :param tree: "dict of dict of ...". F
 64    :return: some fast Iterable container. deque() usually
 65    """
 66    result = deque()
 67    for key, subtree in tree.items():
 68        result.append(key)
 69        if subtree:
 70            result.extend(tree_to_list(subtree))
 71    return result
 72
 73
 74def travers_tree(tree: Dict, functor: Callable):
 75    """
 76    Will call 'functor' for an each item in a tree
 77    :param tree: current traversed subtree
 78    :param functor: any callable with one parameter: functor(key: Hashable)
 79    """
 80    for key, subtree in tree.items():
 81        functor(key)
 82        if subtree:
 83            travers_tree(subtree, functor)
 84
 85
 86def travers_tree_with_path(tree: Dict, functor: Callable, path: Iterable[Hashable]=None):
 87    """
 88    The same as travers_tree(). But also gives current path to a 'functor'
 89    :param tree: current traversed subtree
 90    :param functor: any callable with two parameters: functor(key: Hashable, path: Iterable[Hashable])
 91    :param path: initial actual path for the current traversed subtree
 92    """
 93    current_path = path or deque()
 94    for key, subtree in tree.items():
 95        functor(key, current_path + deque((key,)))
 96        if subtree:
 97            travers_tree_with_path(subtree, functor, current_path + deque((key,)))
 98
 99
100class Tree:
101    def __init__(self):
102        self._data = dict()
103
104    def put(self, path: Iterable[Hashable]):
105        current_node = self._data
106        for item in path:
107            if item not in current_node:
108                current_node[item] = dict()
109            current_node = current_node[item]
110
111    def get_subtree(self, path: Iterable[Hashable]=None)->Dict:
112        path = path or deque()
113        current_path = deque()
114        current_node = self._data
115        for item in path:
116            current_path.append(item)
117            if item in current_node:
118                current_node = current_node[item]
119            else:
120                raise PathDoesNotExist(current_path)
121        return current_node
122
123    def get_immediate_children(self, path: Iterable[Hashable]=None)->Iterable[Hashable]:
124        """
125        Will return immediate children list of the given path: without children of children
126        :param path:
127        :return:
128        """
129        return deque(self.get_subtree(path))
130
131    def get_all_children(self, path: Iterable[Hashable]=None)->Iterable[Hashable]:
132        """
133        Will rerun all children and children of children for the given path
134        The same as tree_to_list() function but for current object and for the given path
135        :param path: path to subtree
136        :return:
137        """
138        return tree_to_list(self.get_subtree(path))
139
140    def travers_subtree(self, functor: Callable, path: Iterable[Hashable]=None):
141        """
142        The same as travers_tree() function but for current object and for the given path
143        :param path:
144        :param functor:
145        """
146        travers_tree(self.get_subtree(path), functor)
147
148    def travers_tree_with_path(self, functor: Callable, path: Iterable[Hashable]=None):
149        """
150        The same as travers_tree_with_path() function but for current object and for the given path
151        :param functor:
152        :param path:
153        """
154        travers_tree_with_path(self.get_subtree(path), functor, path)
class PathDoesNotExist(builtins.Exception):
53class PathDoesNotExist(Exception):
54    pass

Common base class for all non-exit exceptions.

Inherited Members
builtins.Exception
Exception
builtins.BaseException
with_traceback
args
def tree_to_list(tree: Dict) -> Iterable[Hashable]:
57def tree_to_list(tree: Dict)->Iterable[Hashable]:
58    """
59    Returns all items from a tree in a iterable container.
60        tree = Tree()
61        ...
62        all_items =  tree_to_list(tree.get_subtree())
63    Is an implementation of the Tree.get_all_children() method.
64    :param tree: "dict of dict of ...". F
65    :return: some fast Iterable container. deque() usually
66    """
67    result = deque()
68    for key, subtree in tree.items():
69        result.append(key)
70        if subtree:
71            result.extend(tree_to_list(subtree))
72    return result

Returns all items from a tree in a iterable container. tree = Tree() ... all_items = tree_to_list(tree.get_subtree()) Is an implementation of the Tree.get_all_children() method. :param tree: "dict of dict of ...". F :return: some fast Iterable container. deque() usually

def travers_tree(tree: Dict, functor: Callable):
75def travers_tree(tree: Dict, functor: Callable):
76    """
77    Will call 'functor' for an each item in a tree
78    :param tree: current traversed subtree
79    :param functor: any callable with one parameter: functor(key: Hashable)
80    """
81    for key, subtree in tree.items():
82        functor(key)
83        if subtree:
84            travers_tree(subtree, functor)

Will call 'functor' for an each item in a tree :param tree: current traversed subtree :param functor: any callable with one parameter: functor(key: Hashable)

class Tree:
101class Tree:
102    def __init__(self):
103        self._data = dict()
104
105    def put(self, path: Iterable[Hashable]):
106        current_node = self._data
107        for item in path:
108            if item not in current_node:
109                current_node[item] = dict()
110            current_node = current_node[item]
111
112    def get_subtree(self, path: Iterable[Hashable]=None)->Dict:
113        path = path or deque()
114        current_path = deque()
115        current_node = self._data
116        for item in path:
117            current_path.append(item)
118            if item in current_node:
119                current_node = current_node[item]
120            else:
121                raise PathDoesNotExist(current_path)
122        return current_node
123
124    def get_immediate_children(self, path: Iterable[Hashable]=None)->Iterable[Hashable]:
125        """
126        Will return immediate children list of the given path: without children of children
127        :param path:
128        :return:
129        """
130        return deque(self.get_subtree(path))
131
132    def get_all_children(self, path: Iterable[Hashable]=None)->Iterable[Hashable]:
133        """
134        Will rerun all children and children of children for the given path
135        The same as tree_to_list() function but for current object and for the given path
136        :param path: path to subtree
137        :return:
138        """
139        return tree_to_list(self.get_subtree(path))
140
141    def travers_subtree(self, functor: Callable, path: Iterable[Hashable]=None):
142        """
143        The same as travers_tree() function but for current object and for the given path
144        :param path:
145        :param functor:
146        """
147        travers_tree(self.get_subtree(path), functor)
148
149    def travers_tree_with_path(self, functor: Callable, path: Iterable[Hashable]=None):
150        """
151        The same as travers_tree_with_path() function but for current object and for the given path
152        :param functor:
153        :param path:
154        """
155        travers_tree_with_path(self.get_subtree(path), functor, path)
def put(self, path: Iterable[Hashable]):
105    def put(self, path: Iterable[Hashable]):
106        current_node = self._data
107        for item in path:
108            if item not in current_node:
109                current_node[item] = dict()
110            current_node = current_node[item]
def get_subtree(self, path: Iterable[Hashable] = None) -> Dict:
112    def get_subtree(self, path: Iterable[Hashable]=None)->Dict:
113        path = path or deque()
114        current_path = deque()
115        current_node = self._data
116        for item in path:
117            current_path.append(item)
118            if item in current_node:
119                current_node = current_node[item]
120            else:
121                raise PathDoesNotExist(current_path)
122        return current_node
def get_immediate_children(self, path: Iterable[Hashable] = None) -> Iterable[Hashable]:
124    def get_immediate_children(self, path: Iterable[Hashable]=None)->Iterable[Hashable]:
125        """
126        Will return immediate children list of the given path: without children of children
127        :param path:
128        :return:
129        """
130        return deque(self.get_subtree(path))

Will return immediate children list of the given path: without children of children :param path: :return:

def get_all_children(self, path: Iterable[Hashable] = None) -> Iterable[Hashable]:
132    def get_all_children(self, path: Iterable[Hashable]=None)->Iterable[Hashable]:
133        """
134        Will rerun all children and children of children for the given path
135        The same as tree_to_list() function but for current object and for the given path
136        :param path: path to subtree
137        :return:
138        """
139        return tree_to_list(self.get_subtree(path))

Will rerun all children and children of children for the given path The same as tree_to_list() function but for current object and for the given path :param path: path to subtree :return:

def travers_subtree(self, functor: Callable, path: Iterable[Hashable] = None):
141    def travers_subtree(self, functor: Callable, path: Iterable[Hashable]=None):
142        """
143        The same as travers_tree() function but for current object and for the given path
144        :param path:
145        :param functor:
146        """
147        travers_tree(self.get_subtree(path), functor)

The same as travers_tree() function but for current object and for the given path :param path: :param functor:

def travers_tree_with_path(self, functor: Callable, path: Iterable[Hashable] = None):
149    def travers_tree_with_path(self, functor: Callable, path: Iterable[Hashable]=None):
150        """
151        The same as travers_tree_with_path() function but for current object and for the given path
152        :param functor:
153        :param path:
154        """
155        travers_tree_with_path(self.get_subtree(path), functor, path)

The same as travers_tree_with_path() function but for current object and for the given path :param functor: :param path: