-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbite_212.py
More file actions
32 lines (24 loc) · 1007 Bytes
/
bite_212.py
File metadata and controls
32 lines (24 loc) · 1007 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python3.8
from contextlib import suppress
def sum_numbers(numbers):
"""This generator divides each nummber by its consecutive number.
So if it gets passed in [4, 2, 1] it yields 4/2 and 2/1.
It ignores ZeroDivisionError and TypeError exceptions (latter happens
when a string or other non-numeric data type is in numbers)
Task: use contextlib's suppress twice to make the code below more concise.
"""
for i, j in zip(numbers):
# replace the block below
with suppress(TypeError):
with suppress(ZeroDivisionError):
yield i/j
numbers = [1, 2, 0, 4, 5, 12, 'a', 3]
print(list(sum_numbers(numbers)))
"""
Bite 212. Suppressing exceptions ☆
Ever wanted to suppress an exception? Check out Python's contextlib
module.
In this Bite you refactor sum_numbers which has some nested try/except
statements. Use suppress to make this logic cleaner/ more readable.
Have fun and keep calm and code in Python!
"""