Skip to content

Commit 24a0adb

Browse files
committed
-
1 parent 45b219e commit 24a0adb

File tree

4 files changed

+100
-70
lines changed

4 files changed

+100
-70
lines changed

source_py3/python_toolbox/nifty_collections/frozen_dict_and_frozen_ordered_dict.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
class _AbstractFrozenDict(collections.Mapping):
13+
is_frozen = True
1314
_hash = None # Overridden by instance when calculating hash.
1415

1516
def __init__(self, *args, **kwargs):

source_py3/python_toolbox/nifty_collections/ordered_dict.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
class OrderedDict(StdlibOrderedDict):
1616

17+
is_ordered = True
18+
is_frozen = False
19+
1720
def sort(self, key=None, reverse=False):
1821
'''
1922
Sort the items according to their keys, changing the order in-place.
@@ -35,5 +38,4 @@ def index(self, key):
3538
for i, key_ in enumerate(self):
3639
if key_ == key:
3740
return i
38-
raise RuntimeError
39-
41+
raise RuntimeError

source_py3/python_toolbox/nifty_collections/tallying.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,12 @@ def __ge__(self, other):
283283

284284
class _TallyMixin(_BaseTallyMixin):
285285
# blocktodo: add all mutable methods, like __iadd__ and everything
286-
pass
286+
is_ordered = False
287+
287288

288289

289290
class _OrderedTallyMixin:
291+
is_ordered = True
290292
def __repr__(self):
291293
if not self:
292294
return '%s()' % type(self).__name__

source_py3/test_python_toolbox/test_nifty_collections/test_frozen_tally_and_frozen_ordered_tally.py renamed to source_py3/test_python_toolbox/test_nifty_collections/test_tallying.py

Lines changed: 92 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Copyright 2009-2014 Ram Rachum.
22
# This program is distributed under the MIT license.
33

4-
'''Testing module for `python_toolbox.nifty_collections.LazyTuple`.'''
5-
64
import uuid
75
import re
86
import pickle
@@ -15,82 +13,102 @@
1513
from python_toolbox import cute_testing
1614

1715

18-
from python_toolbox.nifty_collections import (FrozenTally,
19-
FrozenOrderedTally,
16+
from python_toolbox.nifty_collections import (Tally, OrderedTally,
17+
FrozenTally, FrozenOrderedTally,
2018
OrderedDict)
2119

2220
infinity = float('inf')
2321
infinities = (infinity, -infinity)
2422

23+
_check_functions = []
24+
def _test_on(arguments):
25+
def decorator(check_function):
26+
check_function.arguments = arguments
27+
_check_functions.append(check_function)
28+
return check_function
29+
return decorator
30+
2531

26-
def test_common():
27-
_check_common(FrozenTally)
28-
_check_common(FrozenOrderedTally)
29-
_check_comparison(FrozenTally)
30-
_check_comparison(FrozenOrderedTally)
31-
_check_ignores_zero(FrozenTally)
32-
_check_ignores_zero(FrozenOrderedTally)
33-
_check_immutable(FrozenTally)
34-
_check_immutable(FrozenOrderedTally)
35-
_check_only_positive_ints_or_zero(FrozenTally)
36-
_check_only_positive_ints_or_zero(FrozenOrderedTally)
37-
3832

39-
def _check_common(frozen_tally_type):
40-
frozen_tally = frozen_tally_type('abracadabra')
41-
assert frozen_tally == collections.Counter('abracadabra') == \
42-
collections.Counter(frozen_tally) == \
43-
frozen_tally_type(collections.Counter('abracadabra'))
44-
45-
assert len(frozen_tally) == 5
46-
assert set(frozen_tally) == set(frozen_tally.keys()) == set('abracadabra')
47-
assert set(frozen_tally.values()) == {1, 2, 5}
48-
assert set(frozen_tally.items()) == \
33+
def test():
34+
assert Tally.is_ordered is False
35+
assert Tally.is_frozen is False
36+
assert OrderedTally.is_ordered is True
37+
assert OrderedTally.is_frozen is False
38+
assert FrozenTally.is_ordered is False
39+
assert FrozenTally.is_frozen is True
40+
assert FrozenOrderedTally.is_ordered is True
41+
assert FrozenOrderedTally.is_frozen is True
42+
assert OrderedDict.is_ordered is True
43+
assert OrderedDict.is_frozen is False
44+
45+
for check_function in _check_functions:
46+
for argument in _check_function.arguments:
47+
_check_function(argument)
48+
49+
50+
@_test_on(Tally, OrderedTally, FrozenTally, FrozenOrderedTally)
51+
def _check_common(tally_type):
52+
tally = tally_type('abracadabra')
53+
assert tally == collections.Counter('abracadabra') == \
54+
collections.Counter(tally) == \
55+
tally_type(collections.Counter('abracadabra'))
56+
57+
assert len(tally) == 5
58+
assert set(tally) == set(tally.keys()) == set('abracadabra')
59+
assert set(tally.values()) == {1, 2, 5}
60+
assert set(tally.items()) == \
4961
{('a', 5), ('r', 2), ('b', 2), ('c', 1), ('d', 1)}
50-
assert frozen_tally['a'] == 5
51-
assert frozen_tally['missing value'] == 0
52-
assert len(frozen_tally) == 5
53-
assert {frozen_tally, frozen_tally} == {frozen_tally}
54-
assert {frozen_tally: frozen_tally} == {frozen_tally: frozen_tally}
55-
assert isinstance(hash(frozen_tally), int)
56-
57-
assert set(frozen_tally.most_common()) == \
58-
set(collections.Counter(frozen_tally).most_common())
59-
60-
assert frozen_tally + frozen_tally == \
61-
frozen_tally_type('abracadabra' * 2)
62-
assert frozen_tally - frozen_tally == frozen_tally_type()
63-
assert frozen_tally - frozen_tally_type('a') == \
64-
frozen_tally_type('abracadabr')
65-
assert frozen_tally - frozen_tally_type('a') == \
66-
frozen_tally_type('abracadabr')
67-
assert frozen_tally | frozen_tally_type('a') == frozen_tally
68-
assert frozen_tally | frozen_tally == \
69-
frozen_tally | frozen_tally | frozen_tally == frozen_tally
70-
assert frozen_tally & frozen_tally_type('a') == frozen_tally_type('a')
71-
assert frozen_tally & frozen_tally == \
72-
frozen_tally & frozen_tally & frozen_tally == frozen_tally
73-
74-
assert frozen_tally_type(frozen_tally.elements()) == frozen_tally
75-
76-
assert +frozen_tally == frozen_tally
77-
with cute_testing.RaiseAssertor(TypeError):
78-
- frozen_tally
79-
80-
assert re.match('^Frozen(Ordered)?Tally\(.*$',
81-
repr(frozen_tally))
82-
83-
assert frozen_tally.copy({'meow': 9}) == \
84-
frozen_tally.copy(meow=9) == \
85-
frozen_tally_type(OrderedDict(
62+
assert tally['a'] == 5
63+
assert tally['missing value'] == 0
64+
assert len(tally) == 5
65+
if tally.is_frozen:
66+
assert {tally, tally} == {tally}
67+
assert {tally: tally} == {tally: tally}
68+
assert isinstance(hash(tally), int)
69+
else:
70+
with cute_testing.RaiseAssertor(TypeError):
71+
{tally}
72+
with cute_testing.RaiseAssertor(TypeError):
73+
{tally: None,}
74+
with cute_testing.RaiseAssertor(TypeError):
75+
assert isinstance(hash(tally), int)
76+
77+
78+
assert set(tally.most_common()) == \
79+
set(collections.Counter(tally).most_common()) == \
80+
set(collections.Counter(tally.elements()).most_common())
81+
82+
assert tally + tally == tally_type('abracadabra' * 2)
83+
assert tally - tally == tally_type()
84+
assert tally - tally_type('a') == tally_type('abracadabr')
85+
assert tally - tally_type('a') == tally_type('abracadabr')
86+
assert tally | tally_type('a') == tally
87+
assert tally | tally == tally | tally | tally == tally
88+
assert tally & tally_type('a') == tally_type('a')
89+
assert tally & tally == \
90+
tally & tally & tally == tally
91+
92+
assert tally_type(tally.elements()) == tally
93+
94+
assert +tally == tally
95+
with cute_testing.RaiseAssertor(TypeError):
96+
- tally
97+
98+
assert re.match('^(Frozen)?(Ordered)?Tally\(.*$', repr(tally))
99+
100+
assert tally.copy({'meow': 9}) == \
101+
tally.copy(meow=9) == \
102+
tally_type(OrderedDict(
86103
[('a', 5), ('b', 2), ('r', 2), ('c', 1), ('d', 1), ('meow', 9)])
87104
)
88105

89-
assert pickle.loads(pickle.dumps(frozen_tally)) == frozen_tally
106+
assert pickle.loads(pickle.dumps(tally)) == tally
90107

91-
assert frozen_tally_type({'a': 0, 'b': 1,}) == \
92-
frozen_tally_type({'c': 0, 'b': 1,})
108+
assert tally_type({'a': 0, 'b': 1,}) == \
109+
tally_type({'c': 0, 'b': 1,})
93110

111+
@_test_on(Tally, OrderedTally, FrozenTally, FrozenOrderedTally)
94112
def _check_comparison(frozen_tally_type):
95113
tally_0 = frozen_tally_type('c')
96114
tally_1 = frozen_tally_type('abc')
@@ -120,6 +138,7 @@ def _check_comparison(frozen_tally_type):
120138
for not_smaller_item in not_smaller_items:
121139
assert not item < smaller_item
122140

141+
@_test_on(Tally, OrderedTally, FrozenTally, FrozenOrderedTally)
123142
def _check_ignores_zero(frozen_tally_type):
124143
frozen_tally_0 = frozen_tally_type({'a': 0,})
125144
frozen_tally_1 = frozen_tally_type()
@@ -138,6 +157,7 @@ def _check_ignores_zero(frozen_tally_type):
138157
{frozen_tally_3}
139158

140159

160+
@_test_on(Tally, OrderedTally, FrozenTally, FrozenOrderedTally)
141161
def _check_immutable(frozen_tally_type):
142162
frozen_tally = frozen_tally_type('abracadabra')
143163
with cute_testing.RaiseAssertor(TypeError):
@@ -148,6 +168,7 @@ def _check_immutable(frozen_tally_type):
148168
frozen_tally['a'] = 7
149169

150170

171+
@_test_on(Tally, OrderedTally, FrozenTally, FrozenOrderedTally)
151172
def _check_immutable(frozen_tally_type):
152173
frozen_tally = frozen_tally_type('abracadabra')
153174
with cute_testing.RaiseAssertor(TypeError):
@@ -157,6 +178,7 @@ def _check_immutable(frozen_tally_type):
157178
with cute_testing.RaiseAssertor(TypeError):
158179
frozen_tally['a'] = 7
159180

181+
@_test_on(Tally, OrderedTally, FrozenTally, FrozenOrderedTally)
160182
def _check_only_positive_ints_or_zero(frozen_tally_type):
161183
assert frozen_tally_type(
162184
OrderedDict([('a', 0), ('b', 0.0), ('c', 1), ('d', 2.0),
@@ -182,8 +204,9 @@ def _check_only_positive_ints_or_zero(frozen_tally_type):
182204
frozen_tally_type({'a': ('still', 'nope'),})
183205

184206

185-
186-
def test_ordered():
207+
208+
@_test_on(Tally, OrderedTally, FrozenTally, FrozenOrderedTally)
209+
def _check_ordered():
187210
frozen_tally_0 = FrozenTally('ababb')
188211
frozen_tally_1 = FrozenTally('bbbaa')
189212
assert frozen_tally_0 == frozen_tally_1
@@ -199,6 +222,8 @@ def test_ordered():
199222
assert frozen_ordered_tally_0 <= frozen_ordered_tally_1
200223
assert frozen_ordered_tally_0 >= frozen_ordered_tally_1
201224

225+
226+
@_test_on(Tally, OrderedTally, FrozenTally, FrozenOrderedTally)
202227
def test_repr():
203228
assert re.match(
204229
"^FrozenTally\\({(?:(?:'b': 3, 'a': 2)|(?:'a': 2, 'b': 3))}\\)$",

0 commit comments

Comments
 (0)