-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from toumorokoshi/toum/append_unique_unhashable
make strategy_append_unique work for unhashables
- Loading branch information
Showing
6 changed files
with
60 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class ExtendedSet(set): | ||
""" | ||
ExtendedSet is an extension of set, which allows for usage | ||
of types that are typically not allowed in a set | ||
(e.g. unhashable). | ||
The following types that cannot be used in a set are supported: | ||
- unhashable types | ||
""" | ||
|
||
def __init__(self, elements): | ||
self._values_by_hash = {self._hash(e): e for e in elements} | ||
|
||
def _insert(self, element): | ||
self._values_by_hash[self._hash(element)] = element | ||
|
||
def _hash(self, element): | ||
if getattr(element, "__hash__") is not None: | ||
return hash(element) | ||
else: | ||
return hash(str(element)) | ||
|
||
def __contains__(self, obj): | ||
return self._hash(obj) in self._values_by_hash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters