This repository has been archived by the owner on Feb 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Add safe generator #386
Open
NoahCarnahan
wants to merge
6
commits into
master
Choose a base branch
from
add-safe-generator
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add safe generator #386
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7cc21a0
Add safe generator class and decorator
NoahCarnahan fb5c084
Add test
NoahCarnahan 35f2aba
Rename classes
NoahCarnahan 0fad0db
Move to a new file
NoahCarnahan 69b2c81
rename test file
NoahCarnahan 4666dbc
Update test and add decorator test
NoahCarnahan 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 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,56 @@ | ||
from functools import wraps | ||
|
||
|
||
class IteratorAlreadyConsumedException(Exception): | ||
pass | ||
|
||
|
||
class SafeIterator(object): | ||
""" | ||
This iterator-like object wraps an iterator. | ||
The SafeIterator will raise a IteratorAlreadyConsumedException if a user attempts to iterate through it a | ||
second time. | ||
This is useful if the wrapped iterator is a one time user generator, which would simply raise StopIteration | ||
exception again (thereby, appearing "empty" to a user who didn't realize that the generator had already been | ||
consumed). | ||
|
||
Note that this class cannot be used interchangeably with an iterator, because iterators must raise | ||
StopIteration exceptions on subsequent iterations. However, in practice, I don't expect this to be an issue. | ||
https://docs.python.org/2/library/stdtypes.html#iterator-types | ||
""" | ||
def __init__(self, generator): | ||
""" | ||
Create a new SafeIterator by wrapping a normal generator (or iterator) | ||
:param generator: the generator to be wrapped | ||
""" | ||
self._generator = generator | ||
self._has_been_consumed = False | ||
|
||
def __iter__(self): | ||
return self | ||
|
||
def next(self): | ||
if self._has_been_consumed: | ||
raise IteratorAlreadyConsumedException | ||
try: | ||
return self._generator.next() | ||
except StopIteration as e: | ||
self._has_been_consumed = True | ||
raise | ||
|
||
def __next__(self): | ||
# Python 3 compatibility | ||
return self.next() | ||
|
||
|
||
def safe_generator(fn): | ||
""" | ||
This decorator function wraps the return value of the given function in a SafeIterator. | ||
You should only use this decorator on functions that return generators. | ||
The SafeIterator will render the generator "safe" by raising a IteratorAlreadyConsumedException if it is | ||
iterated a second time. | ||
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. I think it would be more correct to |
||
""" | ||
@wraps(fn) | ||
def wrapper(*args, **kwargs): | ||
return SafeIterator(fn(*args, **kwargs)) | ||
return wrapper |
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,24 @@ | ||
from django.test import SimpleTestCase | ||
|
||
from dimagi.utils.safe_iterator import SafeIterator, IteratorAlreadyConsumedException, safe_generator | ||
|
||
|
||
class SafeIteratorTest(SimpleTestCase): | ||
|
||
def test_exception(self): | ||
generator = (x for x in range(3)) | ||
safe_generator = SafeIterator(generator) | ||
self.assertEqual(list(safe_generator), [0, 1, 2]) | ||
with self.assertRaises(IteratorAlreadyConsumedException): | ||
for i in safe_generator: | ||
pass | ||
|
||
def test_decorator(self): | ||
@safe_generator | ||
def func(): | ||
return (x for x in range(3)) | ||
|
||
generator = func() | ||
self.assertEqual(list(generator), [0, 1, 2]) | ||
with self.assertRaises(IteratorAlreadyConsumedException): | ||
list(generator) |
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.
should say "one time use" instead of "one time user"?