Skip to content

Commit 768b23d

Browse files
authored
Merge branch 'master' into python-raise-exception
2 parents ecbfa35 + 4191b8f commit 768b23d

File tree

13 files changed

+445
-0
lines changed

13 files changed

+445
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# How to Make Engaging Programming Videos
2+
3+
This repository provides the VS Code settings file that's mentioned in the Real Python [How to Make Engaging Programming Videos](https://realpython.com/how-to-make-programming-videos/) tutorial.
4+
5+
## Using the Settings
6+
7+
The provided settings file helps you to have a clutter-free and readable VS Code editor for the project that you want to record in a video.
8+
9+
Here's an example folder structure to show where you can find your existing workspace settings file:
10+
11+
```
12+
project/
13+
14+
├── .vscode/
15+
│ └── settings.json
16+
17+
└── code.py
18+
```
19+
20+
If the `.vscode/` folder and the `settings.json` don't exist, then you can create the file with the contents of [settings.json](settings.json). If you already have an existing settings file, then you may only copy the configuration objects that you want to use.
21+
22+
To learn more about adjusting your VS Code settings, check out the VS Code documentation on [user and workspace settings](https://code.visualstudio.com/docs/getstarted/settings#_settingsjson).
23+
24+
## Before
25+
26+
[![Before](before.png)](before.png)
27+
28+
## After
29+
30+
[![After](after.png)](after.png)
31+
32+
## Author
33+
34+
- **Philipp Acsany**, E-mail: [[email protected]]([email protected])
35+
36+
## License
37+
38+
Distributed under the MIT license. See [`LICENSE`](../LICENSE) for more information.
404 KB
Loading
393 KB
Loading
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"window.zoomLevel": 2,
3+
"workbench.startupEditor": "none",
4+
"workbench.editorAssociations": {
5+
"*.ipynb": "jupyter.notebook.ipynb"
6+
},
7+
"breadcrumbs.enabled": false,
8+
"editor.minimap.enabled": false,
9+
"workbench.statusBar.visible": false,
10+
"workbench.activityBar.visible": false,
11+
"editor.scrollbar.horizontal": "hidden",
12+
"editor.scrollbar.vertical": "hidden",
13+
"editor.scrollbar.verticalScrollbarSize": 0,
14+
"editor.cursorBlinking": "solid",
15+
"editor.fontSize": 24,
16+
"terminal.integrated.fontSize": 24,
17+
"problems.decorations.enabled": false,
18+
"problems.autoReveal": false,
19+
"outline.problems.enabled": false,
20+
"outline.problems.colors": false,
21+
"outline.problems.badges": false,
22+
"editor.folding": false,
23+
"editor.glyphMargin": false,
24+
"editor.lineDecorationsWidth": 5,
25+
"editor.quickSuggestions": {
26+
"other": false,
27+
"comments": false,
28+
"strings": false
29+
},
30+
"editor.inlineSuggest.enabled": false,
31+
"window.commandCenter": false,
32+
"workbench.layoutControl.enabled": false
33+
}

python-self-type/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python's Self Type: How to Annotate Methods That Return self
2+
3+
This folder provides the code examples for the Real Python tutorial [Python's Self Type: How to Annotate Methods That Return Self](https://realpython.com/python-type-self/).

python-self-type/accounts.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from dataclasses import dataclass
2+
from typing import Self
3+
import random
4+
5+
6+
@dataclass
7+
class BankAccount:
8+
account_number: int
9+
balance: float
10+
11+
def display_balance(self) -> Self:
12+
print(f"Account Number: {self.account_number}")
13+
print(f"Balance: ${self.balance:,.2f}\n")
14+
return self
15+
16+
def deposit(self, amount: float) -> Self:
17+
self.balance += amount
18+
return self
19+
20+
def withdraw(self, amount: float) -> Self:
21+
if self.balance >= amount:
22+
self.balance -= amount
23+
else:
24+
print("Insufficient balance")
25+
return self
26+
27+
28+
@dataclass
29+
class SavingsAccount(BankAccount):
30+
interest_rate: float
31+
32+
@classmethod
33+
def from_application(
34+
cls, deposit: float = 0, interest_rate: float = 1
35+
) -> Self:
36+
# Generate a random seven-digit bank account number
37+
account_number = random.randint(1000000, 9999999)
38+
return cls(account_number, deposit, interest_rate)
39+
40+
def calculate_interest(self) -> float:
41+
return self.balance * self.interest_rate / 100
42+
43+
def add_interest(self) -> Self:
44+
self.deposit(self.calculate_interest())
45+
return self
46+
47+
48+
account = BankAccount(account_number=1534899324, balance=50)
49+
(
50+
account.display_balance()
51+
.deposit(50)
52+
.display_balance()
53+
.withdraw(30)
54+
.display_balance()
55+
)
56+
57+
savings = SavingsAccount.from_application(deposit=100, interest_rate=5)
58+
(
59+
savings.display_balance()
60+
.add_interest()
61+
.display_balance()
62+
.deposit(50)
63+
.display_balance()
64+
.withdraw(30)
65+
.add_interest()
66+
.display_balance()
67+
)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from __future__ import annotations
2+
from dataclasses import dataclass
3+
import random
4+
5+
6+
@dataclass
7+
class BankAccount:
8+
account_number: int
9+
balance: float
10+
11+
def display_balance(self) -> BankAccount:
12+
print(f"Account Number: {self.account_number}")
13+
print(f"Balance: ${self.balance:,.2f}\n")
14+
return self
15+
16+
def deposit(self, amount: float) -> BankAccount:
17+
self.balance += amount
18+
return self
19+
20+
def withdraw(self, amount: float) -> BankAccount:
21+
if self.balance >= amount:
22+
self.balance -= amount
23+
else:
24+
print("Insufficient balance")
25+
return self
26+
27+
28+
@dataclass
29+
class SavingsAccount(BankAccount):
30+
interest_rate: float
31+
32+
@classmethod
33+
def from_application(
34+
cls, deposit: float = 0, interest_rate: float = 1
35+
) -> SavingsAccount:
36+
# Generate a random seven-digit bank account number
37+
account_number = random.randint(1000000, 9999999)
38+
return cls(account_number, deposit, interest_rate)
39+
40+
def calculate_interest(self) -> float:
41+
return self.balance * self.interest_rate / 100
42+
43+
def add_interest(self) -> SavingsAccount:
44+
self.deposit(self.calculate_interest())
45+
return self
46+
47+
48+
account = BankAccount(account_number=1534899324, balance=50)
49+
(
50+
account.display_balance()
51+
.deposit(50)
52+
.display_balance()
53+
.withdraw(30)
54+
.display_balance()
55+
)
56+
57+
savings = SavingsAccount.from_application(deposit=100, interest_rate=5)
58+
(
59+
savings.display_balance()
60+
.add_interest()
61+
.display_balance()
62+
.deposit(50)
63+
.display_balance()
64+
.withdraw(30)
65+
.add_interest()
66+
.display_balance()
67+
)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import random
2+
from dataclasses import dataclass
3+
4+
5+
@dataclass
6+
class BankAccount:
7+
account_number: int
8+
balance: float
9+
10+
def display_balance(self) -> "BankAccount":
11+
print(f"Account Number: {self.account_number}")
12+
print(f"Balance: ${self.balance:,.2f}\n")
13+
return self
14+
15+
def deposit(self, amount: float) -> "BankAccount":
16+
self.balance += amount
17+
return self
18+
19+
def withdraw(self, amount: float) -> "BankAccount":
20+
if self.balance >= amount:
21+
self.balance -= amount
22+
else:
23+
print("Insufficient balance")
24+
return self
25+
26+
27+
@dataclass
28+
class SavingsAccount(BankAccount):
29+
interest_rate: float
30+
31+
@classmethod
32+
def from_application(
33+
cls, deposit: float = 0, interest_rate: float = 1
34+
) -> "SavingsAccount":
35+
# Generate a random seven-digit bank account number
36+
account_number = random.randint(1000000, 9999999)
37+
return cls(account_number, deposit, interest_rate)
38+
39+
def calculate_interest(self) -> float:
40+
return self.balance * self.interest_rate / 100
41+
42+
def add_interest(self) -> "SavingsAccount":
43+
self.deposit(self.calculate_interest())
44+
return self
45+
46+
47+
account = BankAccount(account_number=1534899324, balance=50)
48+
(
49+
account.display_balance()
50+
.deposit(50)
51+
.display_balance()
52+
.withdraw(30)
53+
.display_balance()
54+
)
55+
56+
savings = SavingsAccount.from_application(deposit=100, interest_rate=5)
57+
(
58+
savings.display_balance()
59+
.add_interest()
60+
.display_balance()
61+
.deposit(50)
62+
.display_balance()
63+
.withdraw(30)
64+
.add_interest()
65+
.display_balance()
66+
)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
from dataclasses import dataclass
2+
from typing import TypeVar
3+
import random
4+
5+
# Create TBankAccount type bound by the BankAccount class
6+
TBankAccount = TypeVar("TBankAccount", bound="BankAccount")
7+
8+
9+
@dataclass
10+
class BankAccount:
11+
account_number: int
12+
balance: float
13+
14+
def display_balance(self: TBankAccount) -> TBankAccount:
15+
print(f"Account Number: {self.account_number}")
16+
print(f"Balance: ${self.balance:,.2f}\n")
17+
return self
18+
19+
def deposit(self: TBankAccount, amount: float) -> TBankAccount:
20+
self.balance += amount
21+
return self
22+
23+
def withdraw(self: TBankAccount, amount: float) -> TBankAccount:
24+
if self.balance >= amount:
25+
self.balance -= amount
26+
else:
27+
print("Insufficient balance")
28+
return self
29+
30+
31+
@dataclass
32+
class SavingsAccount(BankAccount):
33+
interest_rate: float
34+
35+
@classmethod
36+
def from_application(
37+
cls: type[TBankAccount], deposit: float = 0, interest_rate: float = 1
38+
) -> TBankAccount:
39+
# Generate a random seven-digit bank account number
40+
account_number = random.randint(1000000, 9999999)
41+
return cls(account_number, deposit, interest_rate)
42+
43+
def calculate_interest(self) -> float:
44+
return self.balance * self.interest_rate / 100
45+
46+
def add_interest(self: TBankAccount) -> TBankAccount:
47+
self.deposit(self.calculate_interest())
48+
return self
49+
50+
51+
account = BankAccount(account_number=1534899324, balance=50)
52+
(
53+
account.display_balance()
54+
.deposit(50)
55+
.display_balance()
56+
.withdraw(30)
57+
.display_balance()
58+
)
59+
60+
savings = SavingsAccount.from_application(deposit=100, interest_rate=5)
61+
(
62+
savings.display_balance()
63+
.add_interest()
64+
.display_balance()
65+
.deposit(50)
66+
.display_balance()
67+
.withdraw(30)
68+
.add_interest()
69+
.display_balance()
70+
)

python-self-type/stack.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import Any, Self
2+
3+
# from typing_extensions import Self - for Python < 3.11
4+
5+
6+
class Stack:
7+
def __init__(self) -> None:
8+
self.items: list[Any] = []
9+
10+
def push(self, item: Any) -> Self:
11+
self.items.append(item)
12+
return self
13+
14+
def pop(self) -> Any:
15+
if self.__bool__():
16+
return self.items.pop()
17+
else:
18+
raise ValueError("Stack is empty")
19+
20+
def __bool__(self) -> bool:
21+
return len(self.items) > 0
22+
23+
24+
stack = Stack()
25+
stack.push(1).push(2).push(3).pop()
26+
print(stack.items)

0 commit comments

Comments
 (0)