Skip to content

How to Debug Common Python Errors #665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions debug-python-errors/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# How to Debug Common Python Errors

This folder provides the code examples for the Real Python tutorial [How to Debug Common Python Errors](https://realpython.com/debug-python-errors/).
3 changes: 3 additions & 0 deletions debug-python-errors/cat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cat = "Siamese"

print(cat)
12 changes: 12 additions & 0 deletions debug-python-errors/fruit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def capitalize_fruit_names(fruits):
capitalized_fruit_names = []
cleaned = [fruit if isinstance(fruit, str) else "" for fruit in fruits]

for fruit in cleaned:
capitalized_fruit_names.append(fruit.capitalize())

return capitalized_fruit_names


if __name__ == "__main__":
print(capitalize_fruit_names(["apple", "BANANA", "cherry", "maNgo"]))
17 changes: 17 additions & 0 deletions debug-python-errors/palindromes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def find_palindromes(text):
# Split sentence into words
words = text.split()

# Remove punctuation and convert to lowercase
normalized_words = [
"".join(filter(str.isalnum, word)).lower() for word in words
]

# Check for palindromes
return [word for word in normalized_words if word == word[::-1]]


if __name__ == "__main__":
print(
find_palindromes("Dad plays many solos at noon, and sees a racecar.")
)
39 changes: 39 additions & 0 deletions debug-python-errors/test_fruit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import unittest

from fruit import capitalize_fruit_names


class TestAllFruits(unittest.TestCase):
def test_empty_list(self):
"""with empty list"""
self.assertEqual(capitalize_fruit_names([]), [])

def test_lowercase_list(self):
"""with lowercase strings"""
self.assertEqual(
capitalize_fruit_names(["apple", "banana", "cherry"]),
["Apple", "Banana", "Cherry"],
)

def test_uppercase_list(self):
"""with uppercase strings"""
self.assertEqual(
capitalize_fruit_names(["APPLE", "BANANA", "CHERRY"]),
["Apple", "Banana", "Cherry"],
)

def test_mixed_case_list(self):
"""with mixed case strings"""
self.assertEqual(
capitalize_fruit_names(["mAnGo", "grApE"]), ["Mango", "Grape"]
)

def test_non_string_element(self):
"""with a mix of integer and string elements"""
self.assertEqual(
capitalize_fruit_names([123, "banana"]), ["", "Banana"]
)


if __name__ == "__main__":
unittest.main()