Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions python-code-quality/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Python Code Quality: Best Practices and Tools

This folder provides the code examples for the Real Python tutorial [Python Code Quality: Best Practices and Tools](https://realpython.com/python-code-quality/).
7 changes: 7 additions & 0 deletions python-code-quality/compilance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# def calcTotal(price,taxRate=0.05): return price*(1+taxRate)


def calculate_price_with_taxes(
base_price: float, tax_rate: float = 0.05
) -> float:
return base_price * (1 + tax_rate)
18 changes: 18 additions & 0 deletions python-code-quality/documentation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# def multiply(a, b):
# return a * b


def multiply(a: float, b: float) -> float:
"""Multiply two numbers

Args:
a (float): First number.
b (float): Second number.

Returns:
float: Product of a and b.
"""
return a * b


print(multiply(2, 3))
14 changes: 14 additions & 0 deletions python-code-quality/efficiency_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from time import perf_counter


def fibonacci_of(n):
if n in {0, 1}:
return n
return fibonacci_of(n - 1) + fibonacci_of(n - 2)


start = perf_counter()
[fibonacci_of(n) for n in range(35)]
end = perf_counter()

print(f"Execution time: {end - start:.2f} seconds")
17 changes: 17 additions & 0 deletions python-code-quality/efficiency_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from time import perf_counter

cache = {0: 0, 1: 1}


def fibonacci_of(n):
if n in cache:
return cache[n]
cache[n] = fibonacci_of(n - 1) + fibonacci_of(n - 2)
return cache[n]


start = perf_counter()
[fibonacci_of(n) for n in range(35)]
end = perf_counter()

print(f"Execution time: {end - start:.2f} seconds")
11 changes: 11 additions & 0 deletions python-code-quality/functionality.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# def add_numbers(a, b):
# return a + b


def add_numbers(a: int | float, b: int | float) -> int | float:
a, b = float(a), float(b)
return a + b


print(add_numbers(2, 3))
print(add_numbers(2, "3"))
3 changes: 3 additions & 0 deletions python-code-quality/input_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
user_input = input("Enter number: ")
result = int(user_input) * 2
print(f"Duplicate: {result}")
7 changes: 7 additions & 0 deletions python-code-quality/input_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
user_input = input("Enter number: ")
try:
number = int(user_input)
except ValueError:
print("Error: invalid input")
else:
print(f"Duplicate: {number * 2}")
18 changes: 18 additions & 0 deletions python-code-quality/maitainability.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def process(numbers):
cleaned = [number for number in numbers if number >= 0]
return sum(cleaned)


print(process([1, 2, 3, -1, -2, -3]))


def clean_data(numbers: list[int]) -> list[int]:
return [number for number in numbers if number >= 0]


def calculate_total(numbers: list[int]) -> int:
return sum(numbers)


cleaned = clean_data([1, 2, 3, -1, -2, -3])
print(calculate_total(cleaned))
10 changes: 10 additions & 0 deletions python-code-quality/readability.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def ca(w, h):
return w * h


def calculate_rectangle_area(width: float, height: float) -> float:
return width * height


print(ca(12, 20))
print(calculate_rectangle_area(12, 20))
6 changes: 6 additions & 0 deletions python-code-quality/reusability.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# def greet_alice():
# return "Hello, Alice!"


def greet(name: str) -> str:
return f"Hello, {name}!"
13 changes: 13 additions & 0 deletions python-code-quality/robustness.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# def divide_numbers(a, b):
# return a / b


def divide_numbers(a: float, b: float) -> float | None:
try:
return a / b
except ZeroDivisionError:
print("Error: can't divide by zero")


print(divide_numbers(10, 2))
print(divide_numbers(3, 0))
10 changes: 10 additions & 0 deletions python-code-quality/scalability.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# def sum_even_numbers(numbers):
# even_numbers = [number for number in numbers if number % 2 == 0]
# return sum(even_numbers)


def sum_even_numbers(numbers):
return sum(number for number in numbers if number % 2 == 0)


sum_even_numbers([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
19 changes: 19 additions & 0 deletions python-code-quality/testability.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# import pytest

# def greet(name):
# print(f"Hello, {name}")


# def test_greet(capsys):
# greet("Alice")
# captured = capsys.readouterr()
# assert captured.out.strip() == "Hello, Alice"


def greet(name: str) -> str:
return f"Hello, {name}"


# Easy to test
def test_greet():
assert greet("Alice") == "Hello, Alice"