Skip to content

Commit 73024dd

Browse files
authored
Merge pull request #646 from realpython/python-code-quality
Sample code for the article on code quality
2 parents f698a49 + e14d42a commit 73024dd

File tree

14 files changed

+161
-0
lines changed

14 files changed

+161
-0
lines changed

python-code-quality/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python Code Quality: Best Practices and Tools
2+
3+
This folder provides the code examples for the Real Python tutorial [Python Code Quality: Best Practices and Tools](https://realpython.com/python-code-quality/).

python-code-quality/compilance.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# def calcTotal(price,taxRate=0.05): return price*(1+taxRate)
2+
3+
4+
def calculate_price_with_taxes(
5+
base_price: float, tax_rate: float = 0.05
6+
) -> float:
7+
return base_price * (1 + tax_rate)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# def multiply(a, b):
2+
# return a * b
3+
4+
5+
def multiply(a: float, b: float) -> float:
6+
"""Multiply two numbers
7+
8+
Args:
9+
a (float): First number.
10+
b (float): Second number.
11+
12+
Returns:
13+
float: Product of a and b.
14+
"""
15+
return a * b
16+
17+
18+
print(multiply(2, 3))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from time import perf_counter
2+
3+
4+
def fibonacci_of(n):
5+
if n in {0, 1}:
6+
return n
7+
return fibonacci_of(n - 1) + fibonacci_of(n - 2)
8+
9+
10+
start = perf_counter()
11+
[fibonacci_of(n) for n in range(35)]
12+
end = perf_counter()
13+
14+
print(f"Execution time: {end - start:.2f} seconds")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from time import perf_counter
2+
3+
cache = {0: 0, 1: 1}
4+
5+
6+
def fibonacci_of(n):
7+
if n in cache:
8+
return cache[n]
9+
cache[n] = fibonacci_of(n - 1) + fibonacci_of(n - 2)
10+
return cache[n]
11+
12+
13+
start = perf_counter()
14+
[fibonacci_of(n) for n in range(35)]
15+
end = perf_counter()
16+
17+
print(f"Execution time: {end - start:.2f} seconds")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# def add_numbers(a, b):
2+
# return a + b
3+
4+
5+
def add_numbers(a: int | float, b: int | float) -> int | float:
6+
a, b = float(a), float(b)
7+
return a + b
8+
9+
10+
print(add_numbers(2, 3))
11+
print(add_numbers(2, "3"))

python-code-quality/input_v1.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
user_input = "Amount to withdraw? "
2+
amount = int(input(user_input))
3+
available_balance = 1000
4+
print(f"Here are your {amount:.2f}USD")
5+
print(f"Your available balance is {available_balance - amount:.2f}USD")

python-code-quality/input_v2.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
user_input = "Amount to withdraw? "
2+
amount = int(input(user_input))
3+
available_balance = 1000
4+
if amount > available_balance:
5+
print("Insufficient funds")
6+
amount = 0
7+
else:
8+
print(f"Here are your {amount:.2f}USD")
9+
10+
print(f"Your available balance is {available_balance - amount:.2f}USD")
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def process(numbers):
2+
cleaned = [number for number in numbers if number >= 0]
3+
return sum(cleaned)
4+
5+
6+
print(process([1, 2, 3, -1, -2, -3]))
7+
8+
9+
def clean_data(numbers: list[int]) -> list[int]:
10+
return [number for number in numbers if number >= 0]
11+
12+
13+
def calculate_total(numbers: list[int]) -> int:
14+
return sum(numbers)
15+
16+
17+
cleaned = clean_data([1, 2, 3, -1, -2, -3])
18+
print(calculate_total(cleaned))

python-code-quality/readability.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def ca(w, h):
2+
return w * h
3+
4+
5+
def calculate_rectangle_area(width: float, height: float) -> float:
6+
return width * height
7+
8+
9+
print(ca(12, 20))
10+
print(calculate_rectangle_area(12, 20))

0 commit comments

Comments
 (0)