Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Figure out the right level of abstraction for iterators. #2548

Copy link
Copy link

Description

@theacodes
Issue body actions

(From #2545)

We need to determine how users will interact with our iterators and what level of abstraction is expected.

Right now @dhermes has implemented an iterator that iterates through items but exposes page information by setting state on the iterator itself.

iterator = MyIterator(...)

for item in iterator:
     print(item)
     print(iterator.page)
     print(iterator.next_page_token)

I'm not the biggest fan of this for two reasons: it combines the iterable and the iterator interfaces, it merges the item-level and page-level abstractions.

In my opinion, we should work at well-defined abstraction levels and keep the iterable and iterator separate.

The first level is the page level:

iterator = MyIterator(...)

# You could also specify arguments to pages, such as page_token
for page in iterator.pages():
    print(page.number)
    print(page.items)
    print(page.next_page_token)

The second level yields items but also page context:

for item, current_page in iterator.items_by_page():
     print(item)
     print(page.number)
     print(page.next_page_token)

The final level yields only items and completely abstracts away the concept of pages:

for item in iterator.items():
    print(item)
Reactions are currently unavailable

Metadata

Metadata

Assignees

Labels

type: questionRequest for information or clarification. Not an issue.Request for information or clarification. Not an issue.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.