Skip to content

Commit f5cabd1

Browse files
committed
TR suggestions
1 parent 4209adc commit f5cabd1

File tree

7 files changed

+287
-280
lines changed

7 files changed

+287
-280
lines changed

python-first/chart.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import matplotlib.pyplot as plt
99

10-
1110
TIMEIT_TIMES = 1000 # Increase number for smoother lines
1211
LIST_SIZE = 500
1312
POSITION_INCREMENT = 10
@@ -18,13 +17,13 @@ def build_list(size, fill, value, at_position):
1817

1918

2019
def find_match_loop(iterable):
21-
for val in iterable:
22-
if val["population"] > 50:
23-
return val
20+
for value in iterable:
21+
if value["population"] > 50:
22+
return value
2423

2524

2625
def find_match_gen(iterable):
27-
return next(val for val in iterable if val["population"] > 50)
26+
return next(value for value in iterable if value["population"] > 50)
2827

2928

3029
looping_times = []
@@ -33,7 +32,7 @@ def find_match_gen(iterable):
3332

3433
for position in range(0, LIST_SIZE, POSITION_INCREMENT):
3534
print(
36-
f"Progress {position/LIST_SIZE:.0%}",
35+
f"Progress {position / LIST_SIZE:.0%}",
3736
end=f"{3 * ' '}\r", # Clear previous characters and reset cursor
3837
)
3938

python-first/chart_gen_loop_in.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ def build_list(size, fill, value, at_position):
2222

2323

2424
def find_match_loop(list_to_search, item_to_find):
25-
for val in list_to_search:
26-
if val == item_to_find:
27-
return val
25+
for value in list_to_search:
26+
if value == item_to_find:
27+
return value
2828
return None
2929

3030

3131
def find_match_gen(list_to_search, item_to_find):
32-
return next((val for val in list_to_search if val == item_to_find), None)
32+
return next(
33+
(value for value in list_to_search if value == item_to_find), None
34+
)
3335

3436

3537
def find_match_in(list_to_search, item_to_find):
@@ -39,7 +41,7 @@ def find_match_in(list_to_search, item_to_find):
3941

4042
for position in range(0, LIST_SIZE, POSITION_INCREMENT):
4143
print(
42-
f"Progress {position/LIST_SIZE:.0%}",
44+
f"Progress {position / LIST_SIZE:.0%}",
4345
end=f"{3 * ' '}\r", # Clear previous characters and resets cursor
4446
)
4547

python-first/from_file.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
value = "Florina"
44

5-
with open(Path("names.txt"), mode="r", encoding="utf-8") as f:
6-
while value not in (line := f.readline()) and line != "":
5+
with Path("names.txt").open(mode="r", encoding="utf-8") as f:
6+
while (line := f.readline()) and value not in line:
77
pass
8-
if line != "":
8+
if line:
99
print(f"Found: {line.strip()}")
1010
else:
1111
print("Not found")

python-first/get_first.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@
44
def get_first(iterable, value=None, key=None, default=None):
55
match value is None, callable(key):
66
case (True, True):
7-
gen = (val for val in iterable if key(val))
7+
gen = (elem for elem in iterable if key(elem))
88
case (False, True):
9-
gen = (val for val in iterable if key(val) == value)
9+
gen = (elem for elem in iterable if key(elem) == value)
1010
case (True, False):
11-
gen = (val for val in iterable if val)
11+
gen = (elem for elem in iterable if elem)
1212
case (False, False):
13-
gen = (val for val in iterable if val == value)
13+
gen = (elem for elem in iterable if elem == value)
1414

1515
return next(gen, default)
1616

1717

1818
if __name__ == "__main__":
19-
target_match = {"country": "Norway", "population": 5311916}
20-
print(get_first(countries, target_match))
19+
print(get_first(countries))
20+
21+
target_match = {"country": "Norway", "population": 5_311_916}
22+
print(get_first(countries, value=target_match))
2123

2224
def match_scotland(data):
2325
return data["country"] == "Scotland"

python-first/get_first_alt.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
from test_fixtures import countries
22

33

4-
def find_match_gen(iterable, key=None, default=None):
4+
def get_first(iterable, default=None, key=None):
55
if callable(key):
6-
gen = (val for val in iterable if key(val))
6+
gen = (elem for elem in iterable if key(elem))
77
else:
8-
gen = (val for val in iterable if val)
8+
gen = (elem for elem in iterable if elem)
99

1010
return next(gen, default)
1111

1212

1313
if __name__ == "__main__":
14-
target_match = {"country": "Norway", "population": 5311916}
15-
print(find_match_gen(countries, target_match))
14+
print(get_first(countries))
15+
16+
target_match = {"country": "Norway", "population": 5_311_916}
17+
print(get_first(countries, key=lambda elem: elem == target_match))
1618

1719
def match_scotland(data):
1820
return data["country"] == "Scotland"
1921

20-
print(find_match_gen(countries, key=match_scotland))
22+
print(get_first(countries, key=match_scotland))

python-first/get_first_loop.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,26 @@
22

33

44
def get_first_loop_no_key(iterable, value=None, default=None):
5-
for val in iterable:
6-
if (value is None and val) or (value is not None and val == value):
7-
return val
5+
for elem in iterable:
6+
if (value is None and elem) or (value is not None and elem == value):
7+
return elem
88
return default
99

1010

1111
def get_first_loop(iterable, value=None, key=None, default=None):
12-
for val in iterable:
13-
_val = key(val) if callable(key) else val
12+
for elem in iterable:
13+
_elem = key(elem) if callable(key) else elem
1414

15-
if (value is None and _val) or (value is not None and _val == value):
16-
return val
15+
if (value is None and _elem) or (value is not None and _elem == value):
16+
return elem
1717
return default
1818

1919

2020
if __name__ == "__main__":
21-
target_match = {"country": "Puerto Rico", "population": 3195153}
22-
print(get_first_loop(countries, target_match))
21+
print(get_first_loop(countries))
22+
23+
target_match = {"country": "Dominican Republic", "population": 10_627_165}
24+
print(get_first_loop(countries, value=target_match))
2325

2426
def match_scotland(data):
2527
return data["country"] == "Scotland"

0 commit comments

Comments
 (0)