Skip to content

Commit 2a211e8

Browse files
authored
Merge branch 'master' into python-logging-update
2 parents 2feb080 + 2758339 commit 2a211e8

35 files changed

+703
-1
lines changed

.github/workflows/dircheck.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
f"{f}: ensure folder name only uses "
4747
f"lowercase letters, numbers, and hyphens"
4848
)
49-
has_error = True
49+
has_errors = True
5050

5151
files = sorted(_.name for _ in f.glob("*"))
5252
if "README.md" not in files:
File renamed without changes.

python-function-names/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# How Do You Choose Python Function Names?
2+
3+
This folder contains source code for the Real Python tutorial [How Do You Choose Python Function Names?](https://realpython.com/python-function-names/).
4+
5+
For additional explanation, check out the associated tutorial.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class BankAccount:
2+
def __init__(self, account_number, balance):
3+
self.account_number = account_number
4+
self.balance = balance
5+
6+
def __repr__(self):
7+
return f"{type(self).__name__}({self.account_number}, {self.balance})"
8+
9+
def _verify_funds(self, amount):
10+
return self.balance >= amount
11+
12+
def _deduct_funds(self, amount):
13+
self.balance -= amount
14+
15+
def _add_funds(self, amount):
16+
self.balance += amount
17+
18+
def get_balance(self):
19+
return self.balance
20+
21+
def withdraw(self, amount):
22+
if self._verify_funds(amount):
23+
self._deduct_funds(amount)
24+
return True
25+
return False
26+
27+
def deposit(self, amount):
28+
self._add_funds(amount)
29+
return True
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Poorly-named function, parameter, and variable
2+
def init(n):
3+
return " ".join(f"{i[0]}." for i in n.split())
4+
5+
6+
# Descriptive names improve readability
7+
def get_initials(full_name):
8+
return " ".join(f"{name[0]}." for name in full_name.split())
9+
10+
11+
print(get_initials("James Clerk Maxwell"))
12+
# OUTPUT: J. C. M.
13+
print(get_initials("Richard Feynman"))
14+
# OUTPUT: R. F.

python-protocol/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python Protocols: Leveraging Structural Subtyping
2+
3+
This folder provides the code examples for the Real Python tutorial [Python Protocols: Leveraging Structural Subtyping](https://realpython.com/python-protocol/).

python-protocol/adder_v1.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from typing import Protocol
2+
3+
4+
class Adder(Protocol):
5+
def add(self, x, y): ...
6+
7+
8+
class IntAdder:
9+
def add(self, x, y):
10+
return x + y
11+
12+
13+
class FloatAdder:
14+
def add(self, x, y):
15+
return x + y
16+
17+
18+
def add(adder: Adder) -> None:
19+
print(adder.add(2, 3))
20+
21+
22+
add(IntAdder())
23+
add(FloatAdder())
24+
25+
26+
for adder in [IntAdder(), FloatAdder()]:
27+
add(adder)

0 commit comments

Comments
 (0)