Skip to content

Commit 53b0141

Browse files
authored
Merge branch 'master' into python-asyncio
2 parents c7fdb6a + 863607f commit 53b0141

24 files changed

+366
-0
lines changed

debug-python-errors/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# How to Debug Common Python Errors
2+
3+
This folder provides the code examples for the Real Python tutorial [How to Debug Common Python Errors](https://realpython.com/debug-python-errors/).

debug-python-errors/cat.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cat = "Siamese"
2+
3+
print(cat)

debug-python-errors/fruit.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def capitalize_fruit_names(fruits):
2+
capitalized_fruit_names = []
3+
cleaned = [fruit if isinstance(fruit, str) else "" for fruit in fruits]
4+
5+
for fruit in cleaned:
6+
capitalized_fruit_names.append(fruit.capitalize())
7+
8+
return capitalized_fruit_names
9+
10+
11+
if __name__ == "__main__":
12+
print(capitalize_fruit_names(["apple", "BANANA", "cherry", "maNgo"]))

debug-python-errors/palindromes.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def find_palindromes(text):
2+
# Split sentence into words
3+
words = text.split()
4+
5+
# Remove punctuation and convert to lowercase
6+
normalized_words = [
7+
"".join(filter(str.isalnum, word)).lower() for word in words
8+
]
9+
10+
# Check for palindromes
11+
return [word for word in normalized_words if word == word[::-1]]
12+
13+
14+
if __name__ == "__main__":
15+
print(
16+
find_palindromes("Dad plays many solos at noon, and sees a racecar.")
17+
)

debug-python-errors/test_fruit.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import unittest
2+
3+
from fruit import capitalize_fruit_names
4+
5+
6+
class TestFruit(unittest.TestCase):
7+
def test_empty_list(self):
8+
"""with empty list"""
9+
self.assertEqual(capitalize_fruit_names([]), [])
10+
11+
def test_lowercase(self):
12+
"""with lowercase strings"""
13+
self.assertEqual(
14+
capitalize_fruit_names(["apple", "banana", "cherry"]),
15+
["Apple", "Banana", "Cherry"],
16+
)
17+
18+
def test_uppercase(self):
19+
"""with uppercase strings"""
20+
self.assertEqual(
21+
capitalize_fruit_names(["APPLE", "BANANA", "CHERRY"]),
22+
["Apple", "Banana", "Cherry"],
23+
)
24+
25+
def test_mixed_case(self):
26+
"""with mixed case strings"""
27+
self.assertEqual(
28+
capitalize_fruit_names(["mAnGo", "grApE"]),
29+
["Mango", "Grape"],
30+
)
31+
32+
def test_non_string_element(self):
33+
"""with a mix of integer and string elements"""
34+
self.assertEqual(
35+
capitalize_fruit_names([123, "banana"]),
36+
["", "Banana"],
37+
)
38+
39+
40+
if __name__ == "__main__":
41+
unittest.main()

python-isinstance/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# What Does isinstance() Do in Python?
2+
3+
This folder provides the code examples for the Real Python tutorial [What Does isinstance() Do in Python?](https://realpython.com/what-does-isinstance-do-in-python/).
4+
5+
The `.py` files contain the code found in the tutorial.
6+
7+
When setting up your tutorial environment, make sure `balls.py`, `balls_v2.py`, and `player_iterables.py` are in your program folder. You'll need to import content from these to replicate the tutorial examples.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from balls_v2 import Ball
2+
3+
test_ball = Ball("white", "sphere")
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from balls import AmericanFootBall, Ball, PoolBall
2+
3+
eight_ball = PoolBall("black", 8)
4+
football = AmericanFootBall("brown")
5+
ball = Ball("green", "sphere")
6+
7+
isinstance(eight_ball, PoolBall)
8+
isinstance(eight_ball, Ball)
9+
isinstance(eight_ball, AmericanFootBall)
10+
11+
isinstance(eight_ball, object)
12+
isinstance(football, object)
13+
isinstance(ball, object)
14+
isinstance(object, object)

python-isinstance/balls.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Ball:
2+
def __init__(self, color, shape):
3+
self.color = color
4+
self.shape = shape
5+
6+
7+
class PoolBall(Ball):
8+
def __init__(self, color, number):
9+
super().__init__(color, shape="sphere")
10+
self.number = number
11+
12+
13+
class AmericanFootBall(Ball):
14+
def __init__(self, color):
15+
super().__init__(color, shape="prolate spheroid")

python-isinstance/balls_v2.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from abc import ABC, abstractmethod
2+
3+
4+
class Ball(ABC):
5+
def __init__(self, color, shape):
6+
self.color = color
7+
self.shape = shape
8+
9+
@abstractmethod
10+
def get_state(self):
11+
pass
12+
13+
14+
class PoolBall(Ball):
15+
def __init__(self, color, number):
16+
super().__init__(color, shape="sphere")
17+
self.number = number
18+
19+
def get_state(self):
20+
print(
21+
f"Color = {self.color}, Number = {self.number}, Shape = {self.shape}"
22+
)
23+
24+
25+
class AmericanFootBall(Ball):
26+
def __init__(self, color):
27+
super().__init__(color, shape="prolate spheroid")
28+
29+
def get_state(self):
30+
print(f"Color = {self.color}, Shape = {self.shape}")

0 commit comments

Comments
 (0)