Mercurial > repos > shellac > guppy_basecaller
comparison env/lib/python3.7/site-packages/future/builtins/newnext.py @ 0:26e78fe6e8c4 draft
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
| author | shellac |
|---|---|
| date | Sat, 02 May 2020 07:14:21 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:26e78fe6e8c4 |
|---|---|
| 1 ''' | |
| 2 This module provides a newnext() function in Python 2 that mimics the | |
| 3 behaviour of ``next()`` in Python 3, falling back to Python 2's behaviour for | |
| 4 compatibility if this fails. | |
| 5 | |
| 6 ``newnext(iterator)`` calls the iterator's ``__next__()`` method if it exists. If this | |
| 7 doesn't exist, it falls back to calling a ``next()`` method. | |
| 8 | |
| 9 For example: | |
| 10 | |
| 11 >>> class Odds(object): | |
| 12 ... def __init__(self, start=1): | |
| 13 ... self.value = start - 2 | |
| 14 ... def __next__(self): # note the Py3 interface | |
| 15 ... self.value += 2 | |
| 16 ... return self.value | |
| 17 ... def __iter__(self): | |
| 18 ... return self | |
| 19 ... | |
| 20 >>> iterator = Odds() | |
| 21 >>> next(iterator) | |
| 22 1 | |
| 23 >>> next(iterator) | |
| 24 3 | |
| 25 | |
| 26 If you are defining your own custom iterator class as above, it is preferable | |
| 27 to explicitly decorate the class with the @implements_iterator decorator from | |
| 28 ``future.utils`` as follows: | |
| 29 | |
| 30 >>> @implements_iterator | |
| 31 ... class Odds(object): | |
| 32 ... # etc | |
| 33 ... pass | |
| 34 | |
| 35 This next() function is primarily for consuming iterators defined in Python 3 | |
| 36 code elsewhere that we would like to run on Python 2 or 3. | |
| 37 ''' | |
| 38 | |
| 39 _builtin_next = next | |
| 40 | |
| 41 _SENTINEL = object() | |
| 42 | |
| 43 def newnext(iterator, default=_SENTINEL): | |
| 44 """ | |
| 45 next(iterator[, default]) | |
| 46 | |
| 47 Return the next item from the iterator. If default is given and the iterator | |
| 48 is exhausted, it is returned instead of raising StopIteration. | |
| 49 """ | |
| 50 | |
| 51 # args = [] | |
| 52 # if default is not _SENTINEL: | |
| 53 # args.append(default) | |
| 54 try: | |
| 55 try: | |
| 56 return iterator.__next__() | |
| 57 except AttributeError: | |
| 58 try: | |
| 59 return iterator.next() | |
| 60 except AttributeError: | |
| 61 raise TypeError("'{0}' object is not an iterator".format( | |
| 62 iterator.__class__.__name__)) | |
| 63 except StopIteration as e: | |
| 64 if default is _SENTINEL: | |
| 65 raise e | |
| 66 else: | |
| 67 return default | |
| 68 | |
| 69 | |
| 70 __all__ = ['newnext'] |
