Ticket #7398 (closed enhancement: fixed)
Added is_iterator method and fixes sage to use it.
| Reported by: | nthiery | Owned by: | hivert |
|---|---|---|---|
| Priority: | major | Milestone: | sage-4.2.1 |
| Component: | misc | Keywords: | iterators, itertools |
| Cc: | sage-combinat, was | Work issues: | |
| Report Upstream: | Reviewers: | Nicolas M. Thiéry | |
| Authors: | Florent Hivert | Merged in: | sage-4.2.1.rc0 |
| Dependencies: | Stopgaps: |
Description (last modified by nthiery) (diff)
The following mantra occurs at three places in Sage's code to test whether v is an iterator:
if hasattr(v, 'next'):
This is not quite correct since some sage objects have a next method without being iterable, or with a different semantic.
Let me quote python's doc:
The iterator objects themselves are required to support the following two methods, which together form the iterator protocol:
- iterator.iter()
Return the iterator object itself. This is required to allow both containers and iterators to be used with the for and in statements. This method corresponds to the tp_iter slot of the type structure for Python objects in the Python/C API.
iterator.next()::
Return the next item from the container. If there are no further items, raise the StopIteration? exception. This method corresponds to the tp_iternext slot of the type structure for Python objects in the Python/C API.
Therefore here is the good way to test if an element is an iterator:
try:
return it is iter(it)
except:
return False
Note: it is not sufficient to check for the existence of the methods since some sage object implement __iter__ to raise a NotImplemented exception !
Florent
Attachments
Change History
comment:1 Changed 4 years ago by hivert
- Description modified (diff)
- Summary changed from Improved mantra to find whether an object is iterable (and get an iterator out it) to Added is_iterator method and fixes sage to use it.
Changed 4 years ago by nthiery
-
attachment
trac_7398_iter_detect-fh.2.patch
added
Updated patch after review by Nicolas (uses itertools to simplify further sage.server.interact.list_of_first_n)
comment:4 Changed 4 years ago by nthiery
- Cc was added
- Keywords iterators, itertools added; iterators removed
- Status changed from needs_review to positive_review
- Description modified (diff)
William: this makes a small edit to sage.server.notebook.interact.list_of_first_n
You may want to check/backport to the notebook code
comment:5 Changed 4 years ago by hivert
- Status changed from positive_review to needs_work
The given patch actually breaks somme code... I'm uploading a new one.
comment:6 Changed 4 years ago by hivert
- Status changed from needs_work to needs_review
Nicolas : can you re-review this patch... I commented out some line saying that it was never user and actually is was quite a lot... I don't understand why we didn't caught it by the tests. Anyway, Massena did.
Sorry for the mess,
Florent
comment:7 Changed 4 years ago by hivert
The patch trac_7398_iter_detect-fh.3.patch broke something in graph_generators. the patch trac_7398_combclass_set_compat-fh.patch should fix it.
Apply those two patches in that order.
Cheers,
Florent
