Skip to content

Commit f48801b

Browse files
Merge branch 'main' into feature/#233-add-nox-task-to-verify-dependency-declarations
2 parents 1757ed0 + 50bf443 commit f48801b

35 files changed

+1627
-155
lines changed

doc/_static/idioms/adapter.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# With Adapter
2+
for first, item in loop_first([]):
3+
if first:
4+
edge_case(item)
5+
continue
6+
default(item)
7+
8+
# Naive Approach
9+
is_first = True
10+
for items in []:
11+
if is_first:
12+
edge_case(item)
13+
is_first = False
14+
continue
15+
default(item)

doc/_static/idioms/concat.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Good
2+
names = ["Peter", "Albert", "Cleo", "Ember"]
3+
output = ", ".join(names)
4+
5+
# Bad
6+
names = ["Peter", "Albert", "Cleo", "Ember"]
7+
output = ""
8+
for name in names:
9+
output += name
10+
if not is_last_element(name):
11+
output += ", "
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# With Decorator
2+
import logging
3+
from functools import wraps
4+
5+
6+
def log_execution(f):
7+
@wraps(f)
8+
def wrapper(*args, **kwargs):
9+
logging.debug(f"Entering: {f.__name__}")
10+
result = f(*args, **kwargs)
11+
logging.debug(f"Leaving: {f.__name__}")
12+
return result
13+
14+
return wrapper
15+
16+
17+
@log_execution
18+
def some_function():
19+
return "Some Result"
20+
21+
22+
@log_execution
23+
def other_function():
24+
return "Other Result"
25+
26+
27+
# Naive Approach
28+
import logging
29+
30+
31+
def some_function():
32+
logging.debug("Entering: some_function")
33+
result = "Some Result"
34+
logging.debug("Leaving: some_function")
35+
return result
36+
37+
38+
def other_function():
39+
logging.debug("Entering: other_function")
40+
result = "Some Result"
41+
logging.debug("Leaving: other_function")
42+
return result

doc/_static/idioms/context_filter.py

Whitespace-only changes.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# With Context Manager
2+
import os
3+
from contextlib import contextmanager
4+
5+
6+
@contextmanager
7+
def chdir(path):
8+
old_dir = os.getcwd()
9+
os.chdir(path)
10+
yield path
11+
os.chdir(old_dir)
12+
13+
14+
def initialize(directory):
15+
with chdir(directory) as _working_dir:
16+
with open('some-file.txt', 'w') as f:
17+
f.write("Some content")
18+
19+
20+
# With Python 3.11
21+
from contextlib import chdir
22+
23+
24+
def initialize(directory):
25+
with chdir(directory) as _working_dir:
26+
with open('some-file.txt', 'w') as f:
27+
f.write("Some content")
28+
29+
30+
# Naive Approach
31+
import os
32+
33+
34+
def initialize(directory):
35+
old_dir = os.getcwd()
36+
os.chdir(directory)
37+
os.chdir(old_dir)
38+
with open('some-file.txt', 'w') as f:
39+
f.write("Some content")
40+
os.chdir(old_dir)

doc/_static/idioms/count.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Good
2+
from collections import Counter
3+
4+
colors = ["green", "blue", "red", "green", "red", "red"]
5+
6+
counts = Counter(colors)
7+
8+
# Bad
9+
from collections import defaultdict
10+
11+
colors = ["green", "blue", "red", "green", "red", "red"]
12+
13+
d = defaultdict(int)
14+
for color in colors:
15+
d[color] += 1

doc/_static/idioms/enumerate.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Good
2+
customers = ["Marry", "Thor", "Peter", "Batman"]
3+
for index, customer in enumerate(customers):
4+
print(f"Customer: {customer}, Queue position: {index}")
5+
6+
# Bad
7+
index = 0
8+
customers = ["Marry", "Thor", "Peter", "Batman"]
9+
for customer in customers:
10+
print(f"Customer: {customer}, Queue position: {index}")
11+
index += 1

doc/_static/idioms/filters.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# With Filter
2+
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
3+
odd = filter(lambda n: n % 2, numbers)
4+
even = (number for number in numbers if not number % 2)
5+
6+
print(f"Sum of odd values: {sum(odd)}")
7+
print(f"Sum of even values: {sum(even)}")
8+
9+
# Naive Approach
10+
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
11+
odd = 0
12+
even = 0
13+
14+
for number in numbers:
15+
if number % 2:
16+
odd += number
17+
else:
18+
even += number
19+
20+
print(f"Sum of odd values: {odd}")
21+
print(f"Sum of even values: {even}")

doc/_static/idioms/format.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Good
2+
template = """
3+
{heading} - from: {date}
4+
---------------------------------
5+
{users}
6+
"""
7+
output = template.format(
8+
heading="User Overview",
9+
date=datetime.now().strftime("%Y-%m-%d"),
10+
users="\n".join(
11+
(
12+
f"Firstname: {user.first_name}, Lastname: {user.last_name}, Age: {user.age}"
13+
for user in users
14+
)
15+
),
16+
)
17+
18+
# Bad
19+
heading = "User Overview"
20+
date = datetime.now().strftime("%Y-%m-%d")
21+
output = f"{heading} - from: {date}" + "\n"
22+
output += "---------------------------------" + "\n"
23+
for user in users:
24+
output += (
25+
f"Firstname: {user.first_name}, Lastname: {user.last_name}, Age: {user.age}\n"
26+
)

doc/_static/idioms/fstring.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Good
2+
output = f"Firstname: {user.first_name}, Lastname: {user.last_name}, Age: {user.age}"
3+
4+
# Bad
5+
output = "Firstname: {}, Lastname: {}, Age: {}".format(
6+
user.first_name, user.last_name, user.age
7+
)

0 commit comments

Comments
 (0)