Skip to content

Commit 777b26f

Browse files
authored
Merge pull request #371 from realpython/3-12-error-messages
Add materials for 3.12 error messages tutorial
2 parents bfe786c + 53291b1 commit 777b26f

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

python-312/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Python 3.12 Demos
2+
3+
This repository holds example code that demos some of the new features in Python 3.12.
4+
5+
## Introduction
6+
7+
You need Python 3.12 installed to run these examples. See the following tutorial instructions:
8+
9+
- [How Can You Install a Pre-Release Version of Python](https://realpython.com/python-pre-release/)
10+
11+
You can learn more about Python 3.12's new features in the following Real Python tutorials:
12+
13+
- [Python 3.12 Preview: Ever Better Error Messages](https://realpython.com/python312-error-messages/)
14+
15+
You'll find examples from all these tutorials in this repository.
16+
17+
## Examples
18+
19+
This section only contains brief instructions on how you can run the examples. See the tutorials for technical details.
20+
21+
### Improved Error Messages
22+
23+
Run [`encoder.py](encoder.py) to create an encoded message like the one shown in the tutorial. You can decode the message using [`decoder.py](decoder.py).
24+
25+
You can swap the import statement to `import d from this` in either of the files to encounter the improved error message:
26+
27+
```python
28+
>>> import d from this
29+
File "<stdin>", line 1
30+
import d from this
31+
^^^^^^^^^^^^^^^^^^
32+
SyntaxError: Did you mean to use 'from ... import ...' instead?
33+
```
34+
35+
In [`local_self.py`](local_self.py), you can see a naive reproduction of another improved error message. Pick apart the example code to learn more about how this was implemented in Python 3.12.
36+
37+
See [Ever Better Error Messages in Python 3.12](https://realpython.com/python312-error-messages/) for more information.
38+
39+
## Authors
40+
41+
- **Martin Breuss**, E-mail: [[email protected]]([email protected])
42+
- **Bartosz Zaczyński**, E-mail: [[email protected]]([email protected])
43+
- **Geir Arne Hjelle**, E-mail: [[email protected]]([email protected])
44+
45+
## License
46+
47+
Distributed under the MIT license. See [`LICENSE`](../LICENSE) for more information.

python-312/decoder.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from this import d
2+
3+
message = "Gunaxf sbe nyy gur nqqrq pynevgl va reebe zrffntrf, Cnoyb!"
4+
5+
decoded_characters = []
6+
for character in message:
7+
if character.isalpha():
8+
decoded_characters.append(d.get(character, ""))
9+
else:
10+
decoded_characters.append(character)
11+
decoded_message = "".join(decoded_characters)
12+
13+
print(decoded_message)

python-312/encoder.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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_characters = []
8+
for character in message:
9+
if character.isalpha():
10+
encoded_characters.append(reverse_cypher_dict.get(character, ""))
11+
else:
12+
encoded_characters.append(character)
13+
14+
encoded_message = "".join(encoded_characters)
15+
16+
print(encoded_message)

python-312/local_self.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
(
17+
f"name '{wrong_name}' is not defined. "
18+
f"Did you mean: 'self.{wrong_name}'?"
19+
)
20+
)
21+
22+
23+
Greeter().greet()

0 commit comments

Comments
 (0)