cengal.data_manipulation.objects_counter.versions.v_0.objects_counter

 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
18from cengal.data_manipulation.tree_traversal import *
19from cengal.data_containers.stack import *
20
21"""
22Module Docstring
23Docstrings: http://www.python.org/dev/peps/pep-0257/
24"""
25
26__author__ = "ButenkoMS <gtalk@butenkoms.space>"
27__copyright__ = "Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>"
28__credits__ = ["ButenkoMS <gtalk@butenkoms.space>", ]
29__license__ = "Apache License, Version 2.0"
30__version__ = "4.4.1"
31__maintainer__ = "ButenkoMS <gtalk@butenkoms.space>"
32__email__ = "gtalk@butenkoms.space"
33# __status__ = "Prototype"
34__status__ = "Development"
35# __status__ = "Production"
36
37
38def objects_counter(data: AnAppropriateContainers):
39    obj_sum = 0
40    stack = Stack(TreeStackItem(data_2_tuple(data)))
41    # stack = StackUni(TreeStackItem(data_2_tuple(data)), remove_on_pop=False)
42    while stack:
43        # print(obj_summ)
44        # print(stack.top().node)
45        # print(stack.top().child)
46        # print()
47        if stack.top().child is None:
48            obj_sum += 1
49            if isinstance(stack.top().node, (tuple, list, deque)):
50                if stack.top().node:
51                    stack.top().child = -1
52                    continue
53                else:
54                    stack.pop()
55                    continue
56            else:
57                stack.pop()
58                continue
59        else:
60            stack.top().child += 1
61            if len(stack.top().node) > stack.top().child:
62                stack.push(TreeStackItem(data_2_tuple(stack.top().node[stack.top().child])))
63                continue
64            else:
65                stack.pop()
66                continue
67    return obj_sum
68
69
70def _objects_counter_recursive_impl(data: AnAppropriateContainers) -> int:
71    obj_sum = 1
72    if isinstance(data, (tuple, list, deque)):
73        for item in data:
74            obj_sum += _objects_counter_recursive_impl(data_2_tuple(item))
75    return obj_sum
76
77
78def objects_counter_recursive(data: AnAppropriateContainers) -> int:
79    return _objects_counter_recursive_impl(data_2_tuple(data))
80
81
82def object_counter_uni(data: AnAppropriateContainers, on_changed_to_drop_in_replacement: Optional[Callable] = None) -> int:
83    return recursion_insureness(objects_counter_recursive, objects_counter, on_changed_to_drop_in_replacement, data)
def objects_counter( data: Union[dict[Union[str, bytes, int, float, NoneType], Union[Union[Dict[Union[str, bytes, int, float, NoneType], Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], Set[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], List[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], Tuple[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]]], str, bytes, int, float, NoneType]], set[Union[Union[Dict[Union[str, bytes, int, float, NoneType], Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], Set[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], List[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], Tuple[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]]], str, bytes, int, float, NoneType]], list[Union[Union[Dict[Union[str, bytes, int, float, NoneType], Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], Set[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], List[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], Tuple[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]]], str, bytes, int, float, NoneType]], tuple[Union[Union[Dict[Union[str, bytes, int, float, NoneType], Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], Set[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], List[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], Tuple[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]]], str, bytes, int, float, NoneType]]]):
39def objects_counter(data: AnAppropriateContainers):
40    obj_sum = 0
41    stack = Stack(TreeStackItem(data_2_tuple(data)))
42    # stack = StackUni(TreeStackItem(data_2_tuple(data)), remove_on_pop=False)
43    while stack:
44        # print(obj_summ)
45        # print(stack.top().node)
46        # print(stack.top().child)
47        # print()
48        if stack.top().child is None:
49            obj_sum += 1
50            if isinstance(stack.top().node, (tuple, list, deque)):
51                if stack.top().node:
52                    stack.top().child = -1
53                    continue
54                else:
55                    stack.pop()
56                    continue
57            else:
58                stack.pop()
59                continue
60        else:
61            stack.top().child += 1
62            if len(stack.top().node) > stack.top().child:
63                stack.push(TreeStackItem(data_2_tuple(stack.top().node[stack.top().child])))
64                continue
65            else:
66                stack.pop()
67                continue
68    return obj_sum
def objects_counter_recursive( data: Union[dict[Union[str, bytes, int, float, NoneType], Union[Union[Dict[Union[str, bytes, int, float, NoneType], Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], Set[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], List[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], Tuple[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]]], str, bytes, int, float, NoneType]], set[Union[Union[Dict[Union[str, bytes, int, float, NoneType], Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], Set[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], List[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], Tuple[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]]], str, bytes, int, float, NoneType]], list[Union[Union[Dict[Union[str, bytes, int, float, NoneType], Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], Set[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], List[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], Tuple[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]]], str, bytes, int, float, NoneType]], tuple[Union[Union[Dict[Union[str, bytes, int, float, NoneType], Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], Set[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], List[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], Tuple[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]]], str, bytes, int, float, NoneType]]]) -> int:
79def objects_counter_recursive(data: AnAppropriateContainers) -> int:
80    return _objects_counter_recursive_impl(data_2_tuple(data))
def object_counter_uni( data: Union[dict[Union[str, bytes, int, float, NoneType], Union[Union[Dict[Union[str, bytes, int, float, NoneType], Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], Set[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], List[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], Tuple[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]]], str, bytes, int, float, NoneType]], set[Union[Union[Dict[Union[str, bytes, int, float, NoneType], Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], Set[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], List[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], Tuple[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]]], str, bytes, int, float, NoneType]], list[Union[Union[Dict[Union[str, bytes, int, float, NoneType], Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], Set[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], List[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], Tuple[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]]], str, bytes, int, float, NoneType]], tuple[Union[Union[Dict[Union[str, bytes, int, float, NoneType], Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], Set[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], List[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]], Tuple[Union[ForwardRef('AnAppropriateContainers'), str, bytes, int, float, NoneType]]], str, bytes, int, float, NoneType]]], on_changed_to_drop_in_replacement: Union[Callable, NoneType] = None) -> int:
83def object_counter_uni(data: AnAppropriateContainers, on_changed_to_drop_in_replacement: Optional[Callable] = None) -> int:
84    return recursion_insureness(objects_counter_recursive, objects_counter, on_changed_to_drop_in_replacement, data)