Skip to content

Commit a496499

Browse files
committed
Sample code for the iterators and iterables article
1 parent 9fb0ba0 commit a496499

File tree

12 files changed

+183
-0
lines changed

12 files changed

+183
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Iterators and Iterables in Python: Run Efficient Iterations
2+
3+
This folder provides the code examples for the article [Iterators and Iterables in Python: Run Efficient Iterations](https://realpython.com/python-iterators-iterables/).
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import asyncio
2+
from random import randint
3+
4+
5+
class AsyncIterable:
6+
def __init__(self, stop):
7+
self.stop = stop
8+
self.index = 0
9+
10+
def __aiter__(self):
11+
return self
12+
13+
async def __anext__(self):
14+
if self.index >= self.stop:
15+
raise StopAsyncIteration
16+
await asyncio.sleep(value := randint(1, 3))
17+
self.index += 1
18+
return value
19+
20+
21+
async def main():
22+
async for num in AsyncIterable(4):
23+
print(num)
24+
25+
26+
asyncio.run(main())
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# def fibonacci_generator(stop):
2+
# previous, next = 0, 1
3+
# for _ in range(0, stop):
4+
# fib_number = previous
5+
# previous, next = next, previous + next
6+
# yield fib_number
7+
8+
9+
def fibonacci_generator(stop=100):
10+
previous, next = 0, 1
11+
index = 0
12+
while True:
13+
if index == stop:
14+
return
15+
index += 1
16+
fib_number = previous
17+
previous, next = next, previous + next
18+
yield fib_number
19+
20+
21+
print("Fibonacci Generator")
22+
for fib in fibonacci_generator(10):
23+
print(fib)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Fibonaccier:
2+
def __init__(self, stop=10):
3+
self._stop = stop
4+
self._index = 0
5+
self._previous = 0
6+
self._next = 1
7+
8+
def __iter__(self):
9+
return self
10+
11+
def __next__(self):
12+
if self._index >= self._stop:
13+
raise StopIteration
14+
self._index += 1
15+
fib_number = self._previous
16+
self._previous, self._next = self._next, self._previous + self._next
17+
return fib_number
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class InfIntegers:
2+
def __init__(self):
3+
self._number = 0
4+
5+
def __iter__(self):
6+
return self
7+
8+
def __next__(self):
9+
number = self._number
10+
self._number += 1
11+
return number
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class ResettableRange:
2+
def __init__(self, start=0, end=None, step=1, *, resettable=False):
3+
if end is None:
4+
end, start = start, 0
5+
self._start = start
6+
self._end = end
7+
self._step = step
8+
self._resettable = resettable
9+
10+
def __iter__(self):
11+
return self
12+
13+
def __next__(self):
14+
if self._start >= self._end:
15+
if self._resettable:
16+
self._start = 0
17+
raise StopIteration
18+
value = self._start
19+
self._start += self._step
20+
return value
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class SequenceIterator:
2+
def __init__(self, sequence):
3+
self._sequence = sequence
4+
self._index = 0
5+
6+
def __iter__(self):
7+
return self
8+
9+
def __next__(self):
10+
if self._index < len(self._sequence):
11+
item = self._sequence[self._index]
12+
self._index += 1
13+
return item
14+
else:
15+
raise StopIteration
16+
17+
18+
class Iterable:
19+
def __init__(self, sequence):
20+
self.sequence = sequence
21+
22+
def __iter__(self):
23+
return SequenceIterator(self.sequence)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def square_generator(sequence):
2+
for item in sequence:
3+
yield item**2
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class SquareIterator:
2+
def __init__(self, sequence):
3+
self._sequence = sequence
4+
self._index = 0
5+
6+
def __iter__(self):
7+
return self
8+
9+
def __next__(self):
10+
if self._index < len(self._sequence):
11+
square = self._sequence[self._index] ** 2
12+
self._index += 1
13+
return square
14+
else:
15+
raise StopIteration
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def square_list(sequence):
2+
squares = []
3+
for item in sequence:
4+
squares.append(item**2)
5+
return squares
6+
7+
8+
# def square_list(sequence):
9+
# return [item**2 for item in sequence]

0 commit comments

Comments
 (0)