Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions generators/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# How to Use Generators and yield in Python

This folder provides the code examples for the Real Python tutorial [How to Use Generators and yield in Python](https://realpython.com/introduction-to-python-generators/).

The scripts only use Python's standard library. They read `techcrunch.csv` from the current working directory, so run them from inside this folder:

```shell
$ python data_pipeline.py
Total series A fundraising: $4380015000
```

Each script holds the code from one section of the tutorial:

| File | Tutorial section |
| --- | --- |
| `csv_reader.py` | Example 1: Reading Large Files |
| `infinite_sequence.py` | Example 2: Generating an Infinite Sequence |
| `palindromes.py` | Example 3: Detecting Palindromes |
| `advanced_gen.py` | Using Advanced Generator Methods |
| `data_pipeline.py` | Creating Data Pipelines With Generators |
| `techcrunch.csv` | The data set for Example 1 and the data pipeline |

`infinite_sequence.py` and `palindromes.py` run forever, so press <kbd>Ctrl</kbd>+<kbd>C</kbd> to stop them. `advanced_gen.py` takes the generator method to demonstrate as an argument:

```shell
$ python advanced_gen.py send # Runs until you press Ctrl+C
$ python advanced_gen.py throw # Stops with a ValueError
$ python advanced_gen.py close # Stops with StopIteration
```
65 changes: 65 additions & 0 deletions generators/advanced_gen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import sys


def is_palindrome(num):
# Skip single-digit inputs
if num // 10 == 0:
return False
temp = num
reversed_num = 0
while temp != 0:
reversed_num = (reversed_num * 10) + (temp % 10)
temp = temp // 10
if num == reversed_num:
return True
else:
return False


def infinite_palindromes():
num = 0
while True:
if is_palindrome(num):
i = yield num
if i is not None:
num = i
num += 1


def use_send():
# Press Ctrl+C to stop the loop
pal_gen = infinite_palindromes()
for i in pal_gen:
print(i)
digits = len(str(i))
pal_gen.send(10 ** (digits))


def use_throw():
pal_gen = infinite_palindromes()
for i in pal_gen:
print(i)
digits = len(str(i))
if digits == 5:
pal_gen.throw(ValueError("We don't like large palindromes"))
pal_gen.send(10 ** (digits))


def use_close():
pal_gen = infinite_palindromes()
for i in pal_gen:
print(i)
digits = len(str(i))
if digits == 5:
pal_gen.close()
pal_gen.send(10 ** (digits))


def main():
demos = {"send": use_send, "throw": use_throw, "close": use_close}
choice = sys.argv[1] if len(sys.argv) > 1 else "close"
demos[choice]()


if __name__ == "__main__":
main()
10 changes: 10 additions & 0 deletions generators/csv_reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def csv_reader(file_name):
for row in open(file_name, "r"):
yield row


csv_gen = csv_reader("techcrunch.csv")
row_count = 0
for row in csv_gen:
row_count += 1
print(f"Row count is {row_count}")
12 changes: 12 additions & 0 deletions generators/data_pipeline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
file_name = "techcrunch.csv"
lines = (line for line in open(file_name))
list_line = (s.rstrip().split(",") for s in lines)
cols = next(list_line)
company_dicts = (dict(zip(cols, data)) for data in list_line)
funding = (
int(company_dict["raisedAmt"])
for company_dict in company_dicts
if company_dict["round"] == "a"
)
total_series_a = sum(funding)
print(f"Total series A fundraising: ${total_series_a}")
10 changes: 10 additions & 0 deletions generators/infinite_sequence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def infinite_sequence():
num = 0
while True:
yield num
num += 1


# Press Ctrl+C to stop the loop
for i in infinite_sequence():
print(i, end=" ")
27 changes: 27 additions & 0 deletions generators/palindromes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
def infinite_sequence():
num = 0
while True:
yield num
num += 1


def is_palindrome(num):
# Skip single-digit inputs
if num // 10 == 0:
return False
temp = num
reversed_num = 0
while temp != 0:
reversed_num = (reversed_num * 10) + (temp % 10)
temp = temp // 10
if num == reversed_num:
return num
else:
return False


# Press Ctrl+C to stop the loop
for i in infinite_sequence():
pal = is_palindrome(i)
if pal:
print(i)
Loading