Skip to content

Commit 53291b1

Browse files
authored
Merge branch 'master' into 3-12-error-messages
2 parents 03d7ead + bfe786c commit 53291b1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+4073
-0
lines changed

python-del-statement/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python's del: Remove References From Scopes and Containers
2+
3+
This folder provides the code examples for the Real Python tutorial [Python's del: Remove References From Scopes and Containers](https://realpython.com/python-del-statement/).
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Dict(dict):
2+
def __delitem__(self, key) -> None:
3+
print(f"Running .__delitem__() to delete {(key, self[key])}")
4+
super().__delitem__(key)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class List(list):
2+
def __delitem__(self, index):
3+
print(f"Running .__delitem__() to delete {self[index]}")
4+
super().__delitem__(index)

python-del-statement/factorial.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Factorial:
2+
def __init__(self, number):
3+
self._number = number
4+
self._cache = {0: 1, 1: 1}
5+
self._factorial = self._calculate_factorial(number)
6+
del self._cache
7+
8+
def _calculate_factorial(self, number):
9+
if number in self._cache:
10+
return self._cache[number]
11+
current_factorial = number * self._calculate_factorial(number - 1)
12+
self._cache[number] = current_factorial
13+
return current_factorial
14+
15+
@property
16+
def number(self):
17+
return self._number
18+
19+
@property
20+
def factorial(self):
21+
return self._factorial
22+
23+
def __str__(self) -> str:
24+
return f"{self._number}! = {self._factorial}"
25+
26+
def __repr__(self):
27+
return f"{type(self).__name__}({self._number})"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class NonDeletable:
2+
def __init__(self, value):
3+
self.value = value
4+
5+
def __delattr__(self, name):
6+
raise AttributeError(
7+
f"{type(self).__name__} object doesn't support attribute deletion"
8+
)

python-del-statement/person.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Person:
2+
def __init__(self, name):
3+
self.name = name
4+
5+
@property
6+
def name(self):
7+
return self._name
8+
9+
@name.setter
10+
def name(self, value):
11+
self._name = str(value).upper()
12+
13+
@name.deleter
14+
def name(self):
15+
raise AttributeError("can't delete attribute 'name'")

python-del-statement/sample.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class SampleClass:
2+
class_attribute = 0
3+
4+
def __init__(self, arg):
5+
self.instance_attribute = arg
6+
7+
def method(self):
8+
print(self.instance_attribute)

python-maze-solver/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Build a Maze Solver in Python Using Graphs
2+
3+
This is the code for the Real Python tutorial [Build a Maze Solver in Python Using Graphs](https://realpython.com/python-maze-solver/).
4+
5+
## Installation
6+
7+
The code requires Python 3.10 or later and depends on the NetworkX library. You can install the project into an active virtual environment by issuing the following command from the project root folder:
8+
9+
```sh
10+
(venv) $ python -m pip install .
11+
```
12+
13+
For development, consider installing the code in editable mode:
14+
15+
```sh
16+
(venv) $ python -m pip install --editable .
17+
```
18+
19+
## Running
20+
21+
To run the maze solver, use the `solve` command and provide the path to a maze file:
22+
23+
```sh
24+
$ solve /path/to/labyrinth.maze
25+
```
26+
27+
Alternatively, you can use the more explicit yet equivalent command:
28+
29+
```sh
30+
$ python -m maze_solver /path/to/labyrinth.maze
31+
```
32+
33+
If the command succeeds, then you should see either a rendered solution in your default web browser or a relevant error message in the console.
28 Bytes
Binary file not shown.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# fmt: off
2+
# flake8: noqa
3+
from pathlib import Path
4+
5+
from maze_solver.models.border import Border
6+
from maze_solver.models.maze import Maze
7+
from maze_solver.models.role import Role
8+
from maze_solver.models.square import Square
9+
10+
11+
def main() -> None:
12+
build_maze().dump(Path(__file__).with_suffix(".maze"))
13+
14+
15+
def build_maze() -> Maze:
16+
return Maze(
17+
squares=(
18+
Square(0, 0, 0, Border.EMPTY, Role.EXTERIOR),
19+
Square(1, 0, 1, Border.TOP | Border.LEFT),
20+
Square(2, 0, 2, Border.TOP | Border.BOTTOM),
21+
Square(3, 0, 3, Border.TOP | Border.RIGHT),
22+
Square(4, 0, 4, Border.BOTTOM | Border.LEFT, Role.EXTERIOR),
23+
Square(5, 1, 0, Border.TOP | Border.BOTTOM | Border.RIGHT, Role.EXIT),
24+
Square(6, 1, 1, Border.LEFT | Border.RIGHT),
25+
Square(7, 1, 2, Border.TOP | Border.BOTTOM | Border.LEFT | Border.RIGHT, Role.WALL),
26+
Square(8, 1, 3, Border.LEFT),
27+
Square(9, 1, 4, Border.TOP | Border.BOTTOM, Role.ENTRANCE),
28+
Square(10, 2, 0, Border.TOP | Border.RIGHT, Role.EXTERIOR),
29+
Square(11, 2, 1, Border.BOTTOM | Border.LEFT),
30+
Square(12, 2, 2, Border.TOP | Border.BOTTOM),
31+
Square(13, 2, 3, Border.BOTTOM | Border.RIGHT),
32+
Square(14, 2, 4, Border.TOP | Border.LEFT, Role.EXTERIOR),
33+
)
34+
)
35+
36+
37+
if __name__ == '__main__':
38+
main()

0 commit comments

Comments
 (0)