1
|
|
|
''' |
2
|
|
|
count_dict - This is a count the dictionary package. |
3
|
|
|
Copyright (C) 2019-2020 sosei |
4
|
|
|
|
5
|
|
|
This program is free software: you can redistribute it and/or modify |
6
|
|
|
it under the terms of the GNU Affero General Public License as published |
7
|
|
|
by the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
(at your option) any later version. |
9
|
|
|
|
10
|
|
|
This program is distributed in the hope that it will be useful, |
11
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
GNU Affero General Public License for more details. |
14
|
|
|
|
15
|
|
|
You should have received a copy of the GNU Affero General Public License |
16
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>. |
17
|
|
|
''' |
18
|
|
|
|
19
|
|
|
from collections import defaultdict |
20
|
|
|
|
21
|
|
|
__all__ = ['count_dict'] |
22
|
|
|
|
23
|
|
|
class count_dict(defaultdict): |
24
|
|
|
''' |
25
|
|
|
count_dict() -> new empty count dictionary |
26
|
|
|
|
27
|
|
|
count_dict(integer) -> new empty dictionary that sets the starting point of counting |
28
|
|
|
|
29
|
|
|
>>> accumulated = count_dict() |
30
|
|
|
>>> accumulated['x'] += 9 |
31
|
|
|
>>> accumulated.items() |
32
|
|
|
dict_items([('x', 9)]) |
33
|
|
|
|
34
|
|
|
>>> accumulated = count_dict(10) |
35
|
|
|
>>> accumulated['x'] += 9 |
36
|
|
|
>>> accumulated.items() |
37
|
|
|
dict_items([('x', 19)]) |
38
|
|
|
|
39
|
|
|
>>> accumulated = defaultdict(count_dict) |
40
|
|
|
>>> accumulated['x']['y'] += 9 |
41
|
|
|
>>> {'x': dict(accumulated['x'])} |
42
|
|
|
{'x': {'y': 9}} |
43
|
|
|
|
44
|
|
|
>>> accumulated = defaultdict(count_dict(10)) |
45
|
|
|
>>> accumulated['x']['y'] += 9 |
46
|
|
|
>>> {'x': dict(accumulated['x'])} |
47
|
|
|
{'x': {'y': 19}} |
48
|
|
|
''' |
49
|
|
|
|
50
|
|
|
version = '1.1.1' |
51
|
|
|
|
52
|
|
|
def __set_initial_value(self): |
53
|
|
|
return self.initial_value |
54
|
|
|
|
55
|
|
|
def __init__(self, initial_value:int = 0): |
56
|
|
|
if not isinstance(initial_value, (int, float)): |
57
|
|
|
raise TypeError(f"must be 'int' or 'float', not '{initial_value.__class__.__name__}'") |
58
|
|
|
self.initial_value = initial_value |
59
|
|
|
self.default_factory = self.__set_initial_value |
60
|
|
|
|
61
|
|
|
def __call__(self): |
62
|
|
|
return count_dict(self.initial_value) |
63
|
|
|
|