Skip to content

Commit b5da6bf

Browse files
committed
-
1 parent 4b6dca5 commit b5da6bf

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

source_py3/python_toolbox/math_tools/sequences.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,16 @@ def abs_stirling(n, k):
6363
def get_sub_counters_counter(counter):
6464
from python_toolbox import nifty_collections
6565
assert isinstance(counter, nifty_collections.FrozenCounter)
66+
# d = {}
67+
# for key_to_reduce, value_of_key_to_reduce in counter.items():
68+
69+
# pass
6670
return nifty_collections.FrozenCounter({
6771
nifty_collections.FrozenCounter(
68-
{key: (value - (key == key_to_reduce)) for
72+
{(key - (key == key_to_reduce)): value for
6973
key, value in counter.items()}
7074
): value_of_key_to_reduce
71-
for key_to_reduce, value_of_key_to_reduce in counter.items()
75+
7276
})
7377

7478

source_py3/python_toolbox/math_tools/types.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ class PossiblyInfiniteReal(numbers.Number,
2222
metaclass=_PossiblyInfiniteRealType):
2323
pass
2424

25-
25+
class _NaturalType(abc.ABCMeta):
26+
def __instancecheck__(self, thing):
27+
return isinstance(thing, numbers.Integral) and thing >= 1
28+
class Natural(numbers.Number,
29+
metaclass=_NaturalType):
30+
pass
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2009-2014 Ram Rachum.,
2+
# This program is distributed under the MIT license.
3+
4+
from python_toolbox import math_tools
5+
6+
from .frozen_counter import FrozenCounter
7+
8+
9+
class FrozenChunkCounter(FrozenCounter):
10+
def __init__(self, iterable):
11+
super().__init__()
12+
13+
# All zero values were already fileterd out by `FrozenCounter`, we'll
14+
# filter out just the non-natural-number keys.
15+
for key in [key for key in self if not isinstance(key, math_tools.Natural)]:
16+
if key == 0:
17+
del self._dict[key]
18+
else:
19+
raise TypeError('Keys to `FrozenChunkCounter` must be '
20+
'non-negative integers.')

0 commit comments

Comments
 (0)