Skip to content

Commit 9928699

Browse files
authored
Merge branch 'master' into python-flatten-list
2 parents 188b61d + fe09cae commit 9928699

File tree

18 files changed

+487
-0
lines changed

18 files changed

+487
-0
lines changed

python-raise-exception/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python's raise: Effectively Raising Exceptions in Your Code
2+
3+
This folder provides the code examples for the Real Python tutorial [Python's raise: Effectively Raising Exceptions in Your Code](https://realpython.com/python-raise-exception/).

python-raise-exception/api.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import requests
2+
3+
4+
class APIError(Exception):
5+
pass
6+
7+
8+
def call_external_api(url):
9+
try:
10+
response = requests.get(url)
11+
response.raise_for_status()
12+
data = response.json()
13+
except requests.RequestException as error:
14+
raise APIError(f"{error}") from None
15+
return data
16+
17+
18+
print(call_external_api("https://api.github.com/events"))

python-raise-exception/bare.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import logging
2+
3+
try:
4+
result = 42 / 0
5+
except Exception as error:
6+
logging.error(error)
7+
raise error

python-raise-exception/divide.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def divide(x, y):
2+
for arg in (x, y):
3+
if not isinstance(arg, int | float):
4+
raise TypeError(f"number expected, got {type(arg).__name__}")
5+
if y == 0:
6+
raise ValueError("denominator can't be zero")
7+
return x / y
8+
9+
10+
try:
11+
divide(42, 0)
12+
except Exception as error:
13+
raise ValueError("invalid argument") from error

python-raise-exception/divide2.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class MathLibraryError(Exception):
2+
pass
3+
4+
5+
def divide(a, b):
6+
try:
7+
return a / b
8+
except ZeroDivisionError as ex:
9+
raise MathLibraryError(ex)

python-raise-exception/grade.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class GradeValueError(Exception):
2+
pass
3+
4+
5+
def calculate_average_grade(grades):
6+
total = 0
7+
count = 0
8+
for grade in grades:
9+
if grade < 0 or grade > 100:
10+
raise GradeValueError(
11+
"grade values must be between 0 and 100 inclusive"
12+
)
13+
total += grade
14+
count += 1
15+
return round(total / count, 2)

python-raise-exception/group.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
try:
2+
raise ExceptionGroup(
3+
"several errors",
4+
[
5+
ValueError("invalid value"),
6+
TypeError("invalid type"),
7+
KeyError("missing key"),
8+
],
9+
)
10+
except* ValueError:
11+
print("Handling ValueError")
12+
except* TypeError:
13+
print("Handling TypeError")
14+
except* KeyError:
15+
print("Handling KeyError")
16+
17+
18+
try:
19+
raise ExceptionGroup(
20+
"several errors",
21+
[
22+
ValueError("invalid value"),
23+
TypeError("invalid type"),
24+
KeyError("missing key"),
25+
],
26+
)
27+
except ExceptionGroup:
28+
print("Got an exception group!")

python-raise-exception/prime.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from math import sqrt
2+
3+
4+
def is_prime(number):
5+
if not isinstance(number, int):
6+
raise TypeError(
7+
f"integer number expected, got {type(number).__name__}"
8+
)
9+
if number < 2:
10+
raise ValueError(f"integer above 1 expected, got {number}")
11+
for candidate in range(2, int(sqrt(number)) + 1):
12+
if number % candidate == 0:
13+
return False
14+
return True

python-raise-exception/square.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def squared(numbers):
2+
if not isinstance(numbers, list | tuple):
3+
raise TypeError(
4+
f"list or tuple expected, got '{type(numbers).__name__}'"
5+
)
6+
return [number**2 for number in numbers]

python-self-type/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python's Self Type: How to Annotate Methods That Return self
2+
3+
This folder provides the code examples for the Real Python tutorial [Python's Self Type: How to Annotate Methods That Return Self](https://realpython.com/python-type-self/).

0 commit comments

Comments
 (0)