From 08be4731ad0e15bc42b2b2eb6354057590622fb0 Mon Sep 17 00:00:00 2001 From: gahjelle Date: Wed, 30 Apr 2025 16:13:47 +0200 Subject: [PATCH 1/3] How to Debug Common Python Errors --- debug-python-errors/README.md | 3 +++ debug-python-errors/cat.py | 3 +++ debug-python-errors/fruit.py | 12 ++++++++++ debug-python-errors/palindromes.py | 13 +++++++++++ debug-python-errors/test_fruit.py | 36 ++++++++++++++++++++++++++++++ 5 files changed, 67 insertions(+) create mode 100644 debug-python-errors/README.md create mode 100644 debug-python-errors/cat.py create mode 100644 debug-python-errors/fruit.py create mode 100644 debug-python-errors/palindromes.py create mode 100644 debug-python-errors/test_fruit.py diff --git a/debug-python-errors/README.md b/debug-python-errors/README.md new file mode 100644 index 0000000000..085ee71e00 --- /dev/null +++ b/debug-python-errors/README.md @@ -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/). diff --git a/debug-python-errors/cat.py b/debug-python-errors/cat.py new file mode 100644 index 0000000000..8e350e1ed8 --- /dev/null +++ b/debug-python-errors/cat.py @@ -0,0 +1,3 @@ +cat = "Siamese" + +print(cat) diff --git a/debug-python-errors/fruit.py b/debug-python-errors/fruit.py new file mode 100644 index 0000000000..f63f12b0a8 --- /dev/null +++ b/debug-python-errors/fruit.py @@ -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"])) diff --git a/debug-python-errors/palindromes.py b/debug-python-errors/palindromes.py new file mode 100644 index 0000000000..da68f69510 --- /dev/null +++ b/debug-python-errors/palindromes.py @@ -0,0 +1,13 @@ +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.")) diff --git a/debug-python-errors/test_fruit.py b/debug-python-errors/test_fruit.py new file mode 100644 index 0000000000..b824f5408a --- /dev/null +++ b/debug-python-errors/test_fruit.py @@ -0,0 +1,36 @@ +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() From 628ff575b58effeb50220d125d6183ff0603a9aa Mon Sep 17 00:00:00 2001 From: gahjelle Date: Wed, 30 Apr 2025 16:16:32 +0200 Subject: [PATCH 2/3] Fix imports --- debug-python-errors/test_fruit.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/debug-python-errors/test_fruit.py b/debug-python-errors/test_fruit.py index b824f5408a..688d0b7085 100644 --- a/debug-python-errors/test_fruit.py +++ b/debug-python-errors/test_fruit.py @@ -1,6 +1,8 @@ import unittest + from fruit import capitalize_fruit_names + class TestAllFruits(unittest.TestCase): def test_empty_list(self): """with empty list""" @@ -22,15 +24,12 @@ def test_uppercase_list(self): def test_mixed_case_list(self): """with mixed case strings""" - self.assertEqual( - capitalize_fruit_names(["mAnGo", "grApE"]), ["Mango", "Grape"] - ) + 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"] - ) + self.assertEqual(capitalize_fruit_names([123, "banana"]), ["", "Banana"]) + if __name__ == "__main__": unittest.main() From a7a7f7fdf66c44bcb7859f8e02695fb1223a20fc Mon Sep 17 00:00:00 2001 From: gahjelle Date: Wed, 30 Apr 2025 16:18:06 +0200 Subject: [PATCH 3/3] Fix formatting --- debug-python-errors/palindromes.py | 8 ++++++-- debug-python-errors/test_fruit.py | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/debug-python-errors/palindromes.py b/debug-python-errors/palindromes.py index da68f69510..c73d3afa39 100644 --- a/debug-python-errors/palindromes.py +++ b/debug-python-errors/palindromes.py @@ -3,11 +3,15 @@ def find_palindromes(text): words = text.split() # Remove punctuation and convert to lowercase - normalized_words = ["".join(filter(str.isalnum, word)).lower() for word in words] + 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.")) + print( + find_palindromes("Dad plays many solos at noon, and sees a racecar.") + ) diff --git a/debug-python-errors/test_fruit.py b/debug-python-errors/test_fruit.py index 688d0b7085..83f3520cc4 100644 --- a/debug-python-errors/test_fruit.py +++ b/debug-python-errors/test_fruit.py @@ -24,11 +24,15 @@ def test_uppercase_list(self): def test_mixed_case_list(self): """with mixed case strings""" - self.assertEqual(capitalize_fruit_names(["mAnGo", "grApE"]), ["Mango", "Grape"]) + 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"]) + self.assertEqual( + capitalize_fruit_names([123, "banana"]), ["", "Banana"] + ) if __name__ == "__main__":