Skip to content

Commit

Permalink
:enhancement: hashiter function in adding the hash of the iterable class
Browse files Browse the repository at this point in the history
  • Loading branch information
b3j0f committed Feb 20, 2016
1 parent e7712a3 commit 09852d9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ test
Iterable
--------

>>> from b3j0f.utils.iterable import is_iterable, first, last, itemat, sliceit
>>> from b3j0f.utils.iterable import is_iterable, first, last, itemat, sliceit, hashiter
>>> is_iterable(1)
False
>>> is_iterable('aze')
Expand Down Expand Up @@ -123,6 +123,9 @@ False
>>> sliceit(od, -2, -1)
['3']

>>> hashiter([1, 2])
8

Path
----

Expand Down
4 changes: 4 additions & 0 deletions b3j0f/utils/iterable.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ def hashiter(iterable):
Hash method on not iterable depends on type:
hash(iterable.__class__) + ...
- dict: sum of (hash(key) + 1) * (hash(value) + 1).
- Otherwise: sum of (pos + 1) * (hash(item) + 1)."""

Expand All @@ -263,6 +265,8 @@ def hashiter(iterable):

except TypeError:

result = hash(iterable.__class__)

isdict = isinstance(iterable, dict)

for index, entry in enumerate(list(iterable)):
Expand Down
3 changes: 3 additions & 0 deletions b3j0f/utils/test/iterable.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ def test_list(self):

self.assertEqual(
result,
hash(list) +
(hash('test') + 1) * 1 +
(hash(1) + 1) * 2 + (hashiter([]) + 1) * 3
)
Expand All @@ -393,6 +394,7 @@ def test_set(self):

self.assertEqual(
result,
hash(set) +
(hash(1) + 1) * 1 + (hash(2) + 1) * 2 + (hash(3) + 1) * 3
)

Expand All @@ -405,6 +407,7 @@ def test_dict(self):

self.assertEqual(
result,
hash(dict) +
(hash('test0') + 1) * (hash(0) + 1) +
(hash('test1') + 1) * (hash(1) + 1)
)
Expand Down

0 comments on commit 09852d9

Please sign in to comment.