Skip to content

Commit a423bc0

Browse files
committed
Add materials
1 parent 03cdde0 commit a423bc0

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

python312-error-messages/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Python 3.12 Preview: Ever Better Error Messages
2+
3+
This repository contains script files with code examples shown in the Real Python tutorial [Python 3.12 Preview: Ever Better Error Messages](https://realpython.com/python312-error-messages/).
4+
5+
The examples support the tutorial text.
6+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from this import d
2+
3+
message = "Gunaxf sbe nyy gur nqqrq pynevgl va reebe zrffntrf, Cnoyb!"
4+
5+
decoded_message = ""
6+
for character in message:
7+
if character.isalpha():
8+
decoded_message += d.get(character, "")
9+
else:
10+
decoded_message += character
11+
12+
print(decoded_message)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from this import d
2+
3+
message = "Thanks for all the added clarity in error messages, Pablo!"
4+
5+
reverse_cypher_dict = {value: key for key, value in d.items()}
6+
7+
encoded_message = ""
8+
for character in message:
9+
if character.isalpha():
10+
encoded_message += reverse_cypher_dict.get(character, "")
11+
else:
12+
encoded_message += character
13+
14+
print(encoded_message)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import inspect
2+
3+
4+
class Greeter:
5+
def __init__(self):
6+
self.message = "Hello"
7+
8+
def greet(self, whom="World"):
9+
frame = inspect.currentframe()
10+
wrong_name = "message"
11+
12+
if "self" in frame.f_locals:
13+
self = frame.f_locals["self"]
14+
if hasattr(self, wrong_name):
15+
raise NameError(
16+
f"name '{wrong_name}' is not defined. Did you mean: 'self.{wrong_name}'?"
17+
)
18+
19+
20+
Greeter().greet()

0 commit comments

Comments
 (0)