-
Notifications
You must be signed in to change notification settings - Fork 150
allow non hashable data to be used in unique #244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
martindurant
merged 6 commits into
python-streamz:master
from
CJ-Wright:unique_non_hashable
May 31, 2019
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f81489b
allow non hashable data to be used in unique
CJ-Wright 2301991
use hashable kwarg
CJ-Wright 1f425f3
remove unused import
CJ-Wright cb328f2
add history test and change history to maxsize
CJ-Wright 8c5679f
handle unique maxsize properly
CJ-Wright ba3c164
py3k only
CJ-Wright File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -7,7 +7,6 @@ language: python | |
|
|
||
| matrix: | ||
| include: | ||
| - python: 2.7 | ||
| - python: 3.6 | ||
|
|
||
| env: | ||
|
|
||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -1035,6 +1035,20 @@ class unique(Stream): | |
| parameter. For example setting ``history=1`` avoids sending through | ||
| elements when one is repeated right after the other. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| history : int or None, optional | ||
| number of stored unique values to check against | ||
| key : function, optional | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could call this hashfunc to make it clear: it is not a key (in the example, this would be simply "a"), and the purpose is to make the data hashable. |
||
| Function which returns a representation of the incoming data. | ||
| For example ``key=lambda x: x['a']`` could be used to allow only | ||
| pieces of data with unique ``'a'`` values to pass through. | ||
| hashable : bool, optional | ||
| If True then data is assumed to be hashable, else it is not. This is | ||
| used for determining how to cache the history, if hashable then | ||
| either dicts or LRU caches are used, otherwise a deque is used. | ||
| Defaults to True. | ||
|
|
||
| Examples | ||
| -------- | ||
| >>> source = Stream() | ||
|
|
@@ -1046,20 +1060,36 @@ class unique(Stream): | |
| 1 | ||
| 3 | ||
| """ | ||
| def __init__(self, upstream, history=None, key=identity, **kwargs): | ||
| self.seen = dict() | ||
| def __init__(self, upstream, maxsize=None, key=identity, hashable=True, | ||
| **kwargs): | ||
| self.key = key | ||
| if history: | ||
| from zict import LRU | ||
| self.seen = LRU(history, self.seen) | ||
| self.maxsize = maxsize | ||
| if hashable: | ||
| self.seen = dict() | ||
| if self.maxsize: | ||
| from zict import LRU | ||
| self.seen = LRU(self.maxsize, self.seen) | ||
| else: | ||
| self.seen = [] | ||
|
|
||
| Stream.__init__(self, upstream, **kwargs) | ||
|
|
||
| def update(self, x, who=None): | ||
| y = self.key(x) | ||
| if y not in self.seen: | ||
| self.seen[y] = 1 | ||
| return self._emit(x) | ||
| emit = True | ||
| if isinstance(self.seen, list): | ||
| if y in self.seen: | ||
| self.seen.remove(y) | ||
| emit = False | ||
| self.seen.insert(0, y) | ||
| if self.maxsize: | ||
| del self.seen[self.maxsize:] | ||
| if emit: | ||
| return self._emit(x) | ||
| else: | ||
| if self.seen.get(y, '~~not_seen~~') == '~~not_seen~~': | ||
| self.seen[y] = 1 | ||
| return self._emit(x) | ||
|
|
||
|
|
||
| @Stream.register_api() | ||
|
|
||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maxsize, I thinkThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parameter name changed, but not this doc line (sorry :))