This issue describes how to implement the iteratorsconcept exercise for the Python track.
The related concept documents issue can be found here.
✅ Getting started
If you have not yet created or contributed to a concept exercise, this issue will require some upfront reading to give you the needed background knowledge. Some good example exercises to look at in the repo:
We also recommend completing one or more of the concept exercises (they're called "learning exercises") on the website.
Please please read the docs before starting. Posting PRs without reading these docs will be a lot more frustrating for you during the review cycle, and exhaust Exercism's maintainers' time. So, before diving into the implementation, please go through the following documents:
These concept docs are meant to teach an understanding, creation, and use of itertators in Python.
💡Learning objectives
Learn more about how looping / iteration and the iterator protocol work in Python, and the options available for creating & customizing iterators.
How loops in Python implement the iterator protocol
iteration "under the covers"
How iterables are defined
Understand and use the built-in iter() to return an iterator
Create/use one or more custom iterators via defining a class
Understand how class -created iterators relate to generators, and the pros and cons of each approach
🤔 Concepts
Concepts and Related Concepts this Concept Covers
- `loops` in Python
- `iteration` in Python
- `iterators` & `iterator types`
- The `iterator protocol`
- `iterables` & `enumeration`
🚫 Topics that are Out of scope
Concepts and Subjects that are Out of Scope
- `classes` & `class customization` beyond the use of the `iterator` dunder methods and `iterator protocol`
- `class-inheritance` beyond what is needed to customize `iteration` for a `container type` or other `class`
- `comprehensions`
- `comprehensions` in `lambdas`
- `coroutines`
- `decorators`
- `functools` and related `map()`, `filter()` and `functools.reduce()`
- `generators` in-depth as a specific form of iterators (_these have their own exercise_)
- `higher-order functions`
- `lambdas`
- using an `assignment expression` or "walrus" operator (`:=`)
- class decorators
- `enums`
↩️ Prerequisites
These are the concepts/concept exercises the student should be familiar with before taking on/learning this concept.
Resources
- [Python Docs: iterator definition](https://docs.python.org/3/glossary.html#term-iterator) | [Python Docs: iterator types](https://docs.python.org/3/library/stdtypes.html#iterator-types)
- [Python Docs: iterable](https://docs.python.org/3/glossary.html#term-iterable)
- [Python Tutorial: iterators](https://docs.python.org/3/tutorial/classes.html#iterators) | [Python Functional Programming HOWTO: Iterators](https://docs.python.org/3/howto/functional.html#iterators)
- [Python Docs: sequence](https://docs.python.org/3/glossary.html#term-sequence)
- [Python Docs: the `for` statement](https://docs.python.org/3/tutorial/controlflow.html#for-statements)
- [Trey Hunner: The Iterator Protocol: How `for` Loops Work in Python](https://treyhunner.com/2016/12/python-iterator-protocol-how-for-loops-work/)
- [Dan Bader: Python Iterators - A Step-by-Step Introduction](https://dbader.org/blog/python-iterators)
- [`iter()`](https://docs.python.org/3/library/functions.html#iter)
- [Python Docs: emulating container types](https://docs.python.org/3/reference/datamodel.html#emulating-container-types)
- [Trey Hunner: How to make an `iterator` in Python](https://treyhunner.com/2018/06/how-to-make-an-iterator-in-python/)
- [Python Patterns Guide: Gang of Four `iterator` Pattern](https://python-patterns.guide/gang-of-four/iterator/)
- [Python Docs C-API: Iterator Protocol](https://docs.python.org/3/c-api/iter.html)
- [Python Docs C-API: Iterator Objects](https://docs.python.org/3/c-api/iterator.html#iterator-objects)
Exercise Ideas & Stories
Should you need inspiration for an exercise story, you can find a collection here. You can also port an exercise from another track, but please make sure to only to include tasks that actually make sense in Python and that add value for a student. Remove/replace/add tasks as needed to make the concept clear/workable.
📁 Exercise Files to Be Created
File Detail for this Exercise
* ### Exercise `introduction.md`
For more information, see [**Exercise** `introduction.md`](https://github.com/exercism/docs/blob/main/building/tracks/concept-exercises.md#file-docsintroductionmd)
- This can summarize/paraphrase the linked concept documents if they have already been created (either the `about` or the `introduction`). The summary does need to have enough information and examples for the student to complete all the tasks outlined for this concept exercise.
* ### Exercise `instructions.md`
For more information, see [`instructions.md`](https://github.com/exercism/docs/blob/main/building/tracks/concept-exercises.md#file-docsinstructionsmd)
Instructions for an exercise usually center on a story that sets up the code challenge to be solved. You can create your own story, or fork one from the ones listed [here](https://exercism.org/docs/building/tracks/stories). Please make sure to give credit to the original authors if you use a story or fork an exercise.
* ### Exercise `Exemplar.py` Solution
For more information, see [exemplar implementation](https://github.com/exercism/docs/blob/main/building/tracks/concept-exercises.md#file-exemplar-implementation).
This file should not use syntax or datas structures not introduced in this exercise or in this exercise's prerequisites. It will be used as an "ideal" solution for the challenge, so make sure it conforms to PEP8 and other formatting conventions, and **does not use single letter variable names**. It should also include proper module and function-level docstrings. However, it should **NOT** include typehinting or type aliases.
* ### `.py` (Stub) for Implementation
For more information, see [stub implementation](https://github.com/exercism/docs/blob/main/building/tracks/concept-exercises.md#file-stub-implementation).
This file should provide the expected function names imported for testing, and optionally TODO comments and or docstrings to aid the student in their implementation. TODOs and docstrings are not required.
* ### `_Test.py` Files
For more information, see [Tests](https://github.com/exercism/docs/blob/main/building/tracks/concept-exercises.md#file-tests).
Additionally, please note that Python associates exercise tasks to tests via a [Pytest Marker](https://docs.pytest.org/en/7.1.x/example/markers.html), and uses [`unittest subtests`](https://docs.python.org/3/library/unittest.html#subtests) as a form of test paramaterization. See the test file for [`Little Sisters Vocab`](https://github.com/exercism/python/blob/main/exercises/concept/little-sisters-vocab/strings_test.py) for examples of how these techniques work.
* ### Exercise `Hints.md`
For more information on writing hints see [`hints.md`](https://github.com/exercism/docs/blob/main/building/tracks/concept-exercises.md#file-docshintsmd)
- Hints should provide enough information to get most students "un-stuck" and moving toward a solution. They should **not** provide a student with a direct solution.
- You can refer to one or more of the resources linked in this issue above, or analogous resources from a trusted source. We prefer using links within the [Python Docs](https://docs.python.org/3/) as the primary go-to, but other resources listed above are also good. Please try to avoid paid or subscription-based links if possible.
* ### Exercise Metadata Files Under `.meta/config.json`
For more information on exercise `.meta/` files and formatting, see [concept exercise metadata files](https://github.com/exercism/docs/blob/main/building/tracks/concept-exercises.md#metadata-files)
* `.meta/config.json` - see [this link](https://github.com/exercism/docs/blob/main/building/tracks/concept-exercises.md#file-metaconfigjson) for the fields and formatting of this file.
* `.meta/design.md` - see [this link](https://github.com/exercism/docs/blob/main/building/tracks/concept-exercises.md#file-metadesignmd) for the formatting of this file. Please use the **Goal**, **Learning Objectives**,**Concepts**, **Prerequisites** and , **Out of Scope** sections from this issue.
♾️ Exercise Metadata - Track
For more information on concept exercises and formatting for the Python track config.json , please see config.json. The track config.json file can be found in the root of the Python repo.
You can use the below for the exercise UUID. You can also generate a new one via exercism configlet, uuidgenerator.net, or any other favorite method. The UUID must be a valid V4 UUID.
concepts should be filled in from the Concepts section in this issue
prerequisites should be filled in from the Prerequisites section in this issue
🎶 Implementation Notes
As a reminder, code in the .meta/examplar.py file should only use syntax & concepts introduced in this exercise or one of its prerequisite exercises. We run all our examplar.py files through PyLint, but do not strictly require module docstrings. We do require function docstrings similar to PEP257. See this concept exercise exemplar.py for an example.
Please do not use comprehensions, generator expressions, or other syntax not previously covered either in the introduction to this exercise, or to one of its prerequisites. Please also follow PEP8 guidelines.
In General, tests should be written using unittest.TestCase and the test file should be named <EXERCISE-NAME>_test.py.
All asserts should contain a "user friendly" failure message (these will display on the webiste to students, so be as clear as you can).
We use a PyTest custom mark to link test cases to exercise task numbers.
We also use unittest.subtest to parameterize test input where/when needed.
Here is an example testfile that shows all three of these in action.
While we do use PyTest as our test runner and for some implementation tests, please check with a maintainer before using a PyTest-specific test method, fixture, or feature.
Our markdown and JSON files are checked against prettier . We recommend setting prettier up locally and running it prior to submitting your PR to avoid any CI errors.
🆘 Next Steps & Getting Help
If you'd like to work on this issue, comment saying "I'd like to work on this" (there is no real need to wait for a response, just go ahead, we'll assign you and put a [claimed] label on the issue).
If you have any questions while implementing, please post the questions as comments in here, or contact one of the maintainers on our Slack channel.
This issue describes how to implement the
iterators
concept exercise for the Python track. The related concept documents issue can be found here.✅ Getting started
If you have not yet created or contributed to a concept exercise, this issue will require some upfront reading to give you the needed background knowledge. Some good example exercises to look at in the repo:
💡Example Exercises💡
1. [Little Sister's Vocabulary](https://github.com/exercism/python/tree/main/exercises/concept/little-sisters-vocab) 2. [Meltdown Mitigation](https://github.com/exercism/python/tree/main/exercises/concept/meltdown-mitigation) 3. [Making the Grade](https://github.com/exercism/python/tree/main/exercises/concept/making-the-grade) 4. [Ellen's Alien Game](https://github.com/exercism/python/tree/main/exercises/concept/ellens-alien-game)
We also recommend completing one or more of the concept exercises (they're called "learning exercises") on the website.
Please please read the docs before starting.
Posting PRs without reading these docs will be a lot more frustrating for you during the review cycle, and exhaust Exercism's maintainers' time. So, before diving into the implementation, please go through the following documents:General Contributing Docs:
Documents on Language Tracks and Concept Exercises
🎯 Goal
These concept docs are meant to teach an understanding, creation, and use of
itertators
in Python.💡Learning objectives
Learn more about how
looping
/iteration
and theiterator protocol
work in Python, and the options available for creating & customizingiterators
.loops
in Python implement theiterator protocol
iteration
"under the covers"iterables
are definediter()
to return aniterator
iterators
via defining aclass
class
-created iterators relate togenerators
, and the pros and cons of each approach🤔 Concepts
Concepts and Related Concepts this Concept Covers
- `loops` in Python - `iteration` in Python - `iterators` & `iterator types` - The `iterator protocol` - `iterables` & `enumeration`
🚫 Topics that are Out of scope
Concepts and Subjects that are Out of Scope
- `classes` & `class customization` beyond the use of the `iterator` dunder methods and `iterator protocol` - `class-inheritance` beyond what is needed to customize `iteration` for a `container type` or other `class` - `comprehensions` - `comprehensions` in `lambdas` - `coroutines` - `decorators` - `functools` and related `map()`, `filter()` and `functools.reduce()` - `generators` in-depth as a specific form of iterators (_these have their own exercise_) - `higher-order functions` - `lambdas` - using an `assignment expression` or "walrus" operator (`:=`) - class decorators - `enums`
↩️ Prerequisites
These are the concepts/concept exercises the student should be familiar with before taking on/learning this concept.
Prereqs (click to expand)
- `basics` - `booleans` - `classes` - `comparisons` - `rich-comparisons` - `decorators` - `descriptors` - `dicts` - `dict-methods` - `functions` - `higher-order-functions` - `lists` - `list-methods` - `numbers` - `sequences` - `sets` - `strings` - `string-methods` - `tuples`📚 Resources for Writing and Reference
Resources
- [Python Docs: iterator definition](https://docs.python.org/3/glossary.html#term-iterator) | [Python Docs: iterator types](https://docs.python.org/3/library/stdtypes.html#iterator-types) - [Python Docs: iterable](https://docs.python.org/3/glossary.html#term-iterable) - [Python Tutorial: iterators](https://docs.python.org/3/tutorial/classes.html#iterators) | [Python Functional Programming HOWTO: Iterators](https://docs.python.org/3/howto/functional.html#iterators) - [Python Docs: sequence](https://docs.python.org/3/glossary.html#term-sequence) - [Python Docs: the `for` statement](https://docs.python.org/3/tutorial/controlflow.html#for-statements) - [Trey Hunner: The Iterator Protocol: How `for` Loops Work in Python](https://treyhunner.com/2016/12/python-iterator-protocol-how-for-loops-work/) - [Dan Bader: Python Iterators - A Step-by-Step Introduction](https://dbader.org/blog/python-iterators) - [`iter()`](https://docs.python.org/3/library/functions.html#iter) - [Python Docs: emulating container types](https://docs.python.org/3/reference/datamodel.html#emulating-container-types) - [Trey Hunner: How to make an `iterator` in Python](https://treyhunner.com/2018/06/how-to-make-an-iterator-in-python/) - [Python Patterns Guide: Gang of Four `iterator` Pattern](https://python-patterns.guide/gang-of-four/iterator/) - [Python Docs C-API: Iterator Protocol](https://docs.python.org/3/c-api/iter.html) - [Python Docs C-API: Iterator Objects](https://docs.python.org/3/c-api/iterator.html#iterator-objects)
Exercise Ideas & Stories
Should you need inspiration for an exercise story, you can find a collection here. You can also port an exercise from another track, but please make sure to only to include tasks that actually make sense in Python and that add value for a student. Remove/replace/add tasks as needed to make the concept clear/workable.
📁 Exercise Files to Be Created
File Detail for this Exercise
* ### Exercise `introduction.md` For more information, see [**Exercise** `introduction.md`](https://github.com/exercism/docs/blob/main/building/tracks/concept-exercises.md#file-docsintroductionmd) - This can summarize/paraphrase the linked concept documents if they have already been created (either the `about` or the `introduction`). The summary does need to have enough information and examples for the student to complete all the tasks outlined for this concept exercise. * ### Exercise `instructions.md` For more information, see [`instructions.md`](https://github.com/exercism/docs/blob/main/building/tracks/concept-exercises.md#file-docsinstructionsmd) Instructions for an exercise usually center on a story that sets up the code challenge to be solved. You can create your own story, or fork one from the ones listed [here](https://exercism.org/docs/building/tracks/stories). Please make sure to give credit to the original authors if you use a story or fork an exercise. * ### Exercise `Exemplar.py` Solution For more information, see [exemplar implementation](https://github.com/exercism/docs/blob/main/building/tracks/concept-exercises.md#file-exemplar-implementation). This file should not use syntax or datas structures not introduced in this exercise or in this exercise's prerequisites. It will be used as an "ideal" solution for the challenge, so make sure it conforms to PEP8 and other formatting conventions, and **does not use single letter variable names**. It should also include proper module and function-level docstrings. However, it should **NOT** include typehinting or type aliases. * ### `
♾️ Exercise Metadata - Track
For more information on concept exercises and formatting for the Python track
config.json
, please seeconfig.json
. The trackconfig.json
file can be found in the root of the Python repo.You can use the below for the exercise UUID. You can also generate a new one via exercism configlet, uuidgenerator.net, or any other favorite method. The UUID must be a valid V4 UUID.
3510bc43-3dfd-463e-8276-a5f5635ea438
🎶 Implementation Notes
As a reminder, code in the
.meta/examplar.py
file should only use syntax & concepts introduced in this exercise or one of its prerequisite exercises. We run all ourexamplar.py
files through PyLint, but do not strictly require module docstrings. We do require function docstrings similar to PEP257. See this concept exerciseexemplar.py
for an example.Please do not use comprehensions, generator expressions, or other syntax not previously covered either in the introduction to this exercise, or to one of its prerequisites. Please also follow PEP8 guidelines.
In General, tests should be written using
unittest.TestCase
and the test file should be named<EXERCISE-NAME>_test.py
.PyTest custom mark
to link test cases to exercise task numbers.unittest.subtest
to parameterize test input where/when needed. Here is an example testfile that shows all three of these in action.While we do use PyTest as our test runner and for some implementation tests, please check with a maintainer before using a PyTest-specific test method, fixture, or feature.
Our markdown and JSON files are checked against prettier . We recommend setting prettier up locally and running it prior to submitting your PR to avoid any CI errors.
🆘 Next Steps & Getting Help
If you'd like to work on this issue, comment saying "I'd like to work on this"
(there is no real need to wait for a response, just go ahead, we'll assign you and put a[claimed]
label on the issue).