mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-05 20:40:25 -08:00
Clean Code: Python
Fix issues found by flake8-bugbear: * Unused loop variables * assert statements in non-test code * mixing test code with production code * mark one excessive test which takes ~10min to run as 'slow'
This commit is contained in:
2
.github/workflows/check-python.yml
vendored
2
.github/workflows/check-python.yml
vendored
@@ -28,4 +28,4 @@ jobs:
|
||||
mypy . --exclude 79_Slalom --exclude 27_Civil_War --exclude 38_Fur_Trader --exclude 81_Splat --exclude 09_Battle --exclude 40_Gomoko --exclude 36_Flip_Flop --exclude 43_Hammurabi --exclude 04_Awari --exclude 78_Sine_Wave --exclude 77_Salvo --exclude 34_Digits --exclude 17_Bullfight --exclude 16_Bug
|
||||
- name: Test with flake8
|
||||
run: |
|
||||
flake8 . --ignore E501,W503,E203,E731
|
||||
flake8 . --ignore E501,W503,E203,E731,B011
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
pytest
|
||||
flake8
|
||||
flake8-bugbear
|
||||
mypy
|
||||
|
||||
@@ -5,8 +5,14 @@
|
||||
# pip-compile ci-requirements.in
|
||||
#
|
||||
attrs==21.4.0
|
||||
# via pytest
|
||||
# via
|
||||
# flake8-bugbear
|
||||
# pytest
|
||||
flake8==4.0.1
|
||||
# via
|
||||
# -r ci-requirements.in
|
||||
# flake8-bugbear
|
||||
flake8-bugbear==22.1.11
|
||||
# via -r ci-requirements.in
|
||||
iniconfig==1.1.1
|
||||
# via pytest
|
||||
|
||||
@@ -292,7 +292,7 @@ def do_move(m, home, board):
|
||||
move_stones = board[m]
|
||||
board[m] = 0
|
||||
|
||||
for stones in range(move_stones, 0, -1):
|
||||
for _stones in range(move_stones, 0, -1):
|
||||
m = m + 1
|
||||
if m > 13:
|
||||
m = m - 14
|
||||
|
||||
@@ -97,11 +97,11 @@ def print_banner():
|
||||
if s[u] == 1:
|
||||
f[u] = 8 - k
|
||||
break
|
||||
for t1 in range(1, x + 1):
|
||||
for _t1 in range(1, x + 1):
|
||||
line_str = " " * int((63 - 4.5 * y) * g1 / len(xStr) + 1)
|
||||
for b in range(0, f[u] + 1):
|
||||
if j[b] == 0:
|
||||
for i in range(1, y + 1):
|
||||
for _ in range(1, y + 1):
|
||||
line_str = line_str + " " * len(xStr)
|
||||
else:
|
||||
line_str = line_str + xStr * y
|
||||
|
||||
@@ -66,7 +66,7 @@ class Player:
|
||||
print(f"Extra rolls for {self.name}")
|
||||
pins = [0] * 10 # reset the pins
|
||||
score = 0
|
||||
for ball in range(extra):
|
||||
for _ball in range(extra):
|
||||
if score == 10:
|
||||
pins = [0] * 10
|
||||
simulateRoll(pins)
|
||||
|
||||
@@ -48,7 +48,7 @@ def play():
|
||||
if opponent_score >= 2 or player_score >= 2:
|
||||
break
|
||||
|
||||
for action in range(7):
|
||||
for _action in range(7):
|
||||
if random.randint(1, 10) > 5:
|
||||
# opponent swings
|
||||
punch = random.randint(1, 4)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import random
|
||||
import time
|
||||
|
||||
|
||||
def print_n_whitespaces(n: int):
|
||||
@@ -179,8 +180,7 @@ while not (Y > 0):
|
||||
# 970
|
||||
X = random.randint(1, 6)
|
||||
print()
|
||||
for delay in range(2000):
|
||||
pass
|
||||
time.sleep(2)
|
||||
|
||||
print("I ROLLED A", X)
|
||||
if X == 1:
|
||||
|
||||
@@ -109,9 +109,9 @@ def calendar(weekday, leap_year):
|
||||
print(days)
|
||||
print(sep)
|
||||
|
||||
for w in range(1, 7):
|
||||
for _ in range(1, 7):
|
||||
print("\n")
|
||||
for g in range(1, 8):
|
||||
for g in range(1, 8): # noqa
|
||||
d += 1
|
||||
d2 = d - days_count
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ def main():
|
||||
|
||||
count = int(PAGE_WIDTH / width)
|
||||
|
||||
for down in range(count):
|
||||
for _down in range(count):
|
||||
print_diamond(1, width, 2, width, count)
|
||||
print_diamond(width - 2, 1, -2, width, count)
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ while still_playing:
|
||||
n = int(input("How many rolls? "))
|
||||
|
||||
# Roll the dice n times
|
||||
for i in range(n):
|
||||
for _ in range(n):
|
||||
die1 = random.randint(1, 6)
|
||||
die2 = random.randint(1, 6)
|
||||
roll_total = die1 + die2
|
||||
|
||||
@@ -93,7 +93,7 @@ def main():
|
||||
z2 = 2
|
||||
running_correct = 0
|
||||
|
||||
for round in range(1, 4):
|
||||
for _round in range(1, 4):
|
||||
valid_numbers = False
|
||||
numbers = []
|
||||
while not valid_numbers:
|
||||
|
||||
@@ -225,7 +225,7 @@ def main():
|
||||
else:
|
||||
print("A FANTASTIC PERFORMANCE!!! CHARLEMANGE, DISRAELI, AND")
|
||||
print("JEFFERSON COMBINED COULD NOT HAVE DONE BETTER!\n")
|
||||
for N in range(1, 10):
|
||||
for _ in range(1, 10):
|
||||
print("\a")
|
||||
|
||||
print("\nSO LONG FOR NOW.\n")
|
||||
|
||||
@@ -418,9 +418,9 @@ def find_board_index_that_matches_board(board):
|
||||
if matches:
|
||||
return board_index, is_reversed
|
||||
|
||||
# THE TERMINATION OF THIS LOOP IS IMPOSSIBLE
|
||||
print("ILLEGAL BOARD PATTERN.")
|
||||
assert False
|
||||
# This point should never be reached
|
||||
# In future, mypy might be able to check exhaustiveness via assert_never
|
||||
raise RuntimeError("ILLEGAL BOARD PATTERN.")
|
||||
|
||||
|
||||
def pick_computer_move(board):
|
||||
|
||||
@@ -21,7 +21,7 @@ def play():
|
||||
secret = random.randint(1, 100)
|
||||
guessed_correctly = False
|
||||
|
||||
for attempt in range(MAX_ATTEMPTS):
|
||||
for _attempt in range(MAX_ATTEMPTS):
|
||||
print("YOUR GUESS", end=QUESTION_PROMPT)
|
||||
guess = int(input())
|
||||
|
||||
|
||||
@@ -55,7 +55,7 @@ def main():
|
||||
|
||||
pattern_height = len(pattern)
|
||||
pattern_width = 0
|
||||
for line_num, line in pattern.items():
|
||||
for _line_num, line in pattern.items():
|
||||
pattern_width = max(pattern_width, len(line))
|
||||
|
||||
min_x = 11 - pattern_height // 2
|
||||
@@ -94,7 +94,7 @@ def main():
|
||||
|
||||
p = 0
|
||||
g += 1
|
||||
for x in range(0, min_x):
|
||||
for _ in range(min_x):
|
||||
print()
|
||||
|
||||
for x in range(min_x, max_x + 1):
|
||||
@@ -120,7 +120,7 @@ def main():
|
||||
print("".join(line))
|
||||
|
||||
# line 295
|
||||
for x in range(max_x + 1, MAX_HEIGHT):
|
||||
for _ in range(max_x + 1, MAX_HEIGHT):
|
||||
print()
|
||||
|
||||
print()
|
||||
|
||||
@@ -38,7 +38,7 @@ def main():
|
||||
print("Guess my combination ...")
|
||||
answer = int(possibilities * random.random())
|
||||
numeric_answer = [-1] * num_positions
|
||||
for i in range(0, answer):
|
||||
for _ in range(0, answer):
|
||||
numeric_answer = get_possibility(numeric_answer)
|
||||
# human_readable_answer = make_human_readable(numeric_answer)
|
||||
while num_moves < 10 and not turn_over:
|
||||
@@ -122,7 +122,7 @@ def main():
|
||||
inconsistent_information = True
|
||||
else:
|
||||
numeric_guess = [-1] * num_positions
|
||||
for i in range(0, guess):
|
||||
for _ in range(0, guess):
|
||||
numeric_guess = get_possibility(numeric_guess)
|
||||
human_readable_guess = make_human_readable(numeric_guess)
|
||||
print(f"My guess is: {human_readable_guess}")
|
||||
@@ -142,7 +142,7 @@ def main():
|
||||
if all_possibilities[i] == 0: # already ruled out
|
||||
continue
|
||||
numeric_possibility = [-1] * num_positions
|
||||
for j in range(0, i):
|
||||
for _ in range(0, i):
|
||||
numeric_possibility = get_possibility(
|
||||
numeric_possibility
|
||||
)
|
||||
|
||||
@@ -68,7 +68,7 @@ class NIM:
|
||||
|
||||
def check_for_win(self):
|
||||
sum = 0
|
||||
for k, v in self.piles.items():
|
||||
for v in self.piles.values():
|
||||
sum += v
|
||||
|
||||
if sum == 0:
|
||||
|
||||
@@ -28,7 +28,7 @@ def print_header(title):
|
||||
|
||||
|
||||
def print_ticks():
|
||||
for t in range(4):
|
||||
for _ in range(4):
|
||||
print("-")
|
||||
|
||||
|
||||
@@ -134,7 +134,7 @@ def deliver_to(customer_index, customer_name, player_name):
|
||||
|
||||
|
||||
def play_game(num_turns, player_name):
|
||||
for turn in range(num_turns):
|
||||
for _turn in range(num_turns):
|
||||
x = random.randint(1, 4)
|
||||
y = random.randint(1, 4)
|
||||
customer_index = calculate_customer_index(x, y)
|
||||
|
||||
@@ -209,7 +209,7 @@ def generate_ship_coordinates(ship):
|
||||
coords = [(start_x, start_y)]
|
||||
x_coord = start_x
|
||||
y_coord = start_y
|
||||
for i in range(ship_len):
|
||||
for _ in range(ship_len):
|
||||
x_coord = x_coord + d_x
|
||||
y_coord = y_coord + d_y
|
||||
coords.append((x_coord, y_coord))
|
||||
@@ -356,7 +356,7 @@ def initialize_game():
|
||||
for ship in SHIPS:
|
||||
print(ship[0])
|
||||
list = []
|
||||
for i in range(ship[1]):
|
||||
for _ in range(ship[1]):
|
||||
x, y = input_coord()
|
||||
list.append((x, y))
|
||||
ship_coords.append(list)
|
||||
@@ -443,7 +443,7 @@ def execute_turn(turn):
|
||||
num_shots = num_player_shots
|
||||
|
||||
shots = []
|
||||
for shot in range(num_shots):
|
||||
for _shot in range(num_shots):
|
||||
valid_shot = False
|
||||
x = -1
|
||||
y = -1
|
||||
|
||||
@@ -46,7 +46,7 @@ radians = 0
|
||||
width = CENTER - 1
|
||||
|
||||
# "Start long loop"
|
||||
for line_num in range(MAX_LINES):
|
||||
for _line_num in range(MAX_LINES):
|
||||
|
||||
# Get string to display on this line
|
||||
curr_string = STRINGS[string_index]
|
||||
|
||||
@@ -882,7 +882,7 @@ def new_quadrant():
|
||||
if b3 > 0:
|
||||
b4, b5 = find_empty_place() # position of starbase (sector)
|
||||
insert_marker(b4, b5, ">!<")
|
||||
for i in range(s3):
|
||||
for _ in range(s3):
|
||||
r1, r2 = find_empty_place()
|
||||
insert_marker(r1, r2, " * ")
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ def get_yes_no(prompt):
|
||||
|
||||
|
||||
def print_header():
|
||||
for i in range(12):
|
||||
for _ in range(12):
|
||||
print()
|
||||
t10 = " " * 10
|
||||
print(t10 + "*************************************")
|
||||
@@ -24,7 +24,7 @@ def print_header():
|
||||
print(t10 + "* *")
|
||||
print(t10 + "* *")
|
||||
print(t10 + "*************************************")
|
||||
for i in range(8):
|
||||
for _ in range(8):
|
||||
print()
|
||||
|
||||
|
||||
|
||||
@@ -55,7 +55,7 @@ def prompt():
|
||||
|
||||
|
||||
def next_target():
|
||||
for i in range(5):
|
||||
for _ in range(5):
|
||||
print()
|
||||
print("NEXT TARGET...")
|
||||
print()
|
||||
|
||||
49
95_Weekday/python/test_weekday.py
Normal file
49
95_Weekday/python/test_weekday.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import datetime
|
||||
|
||||
import pytest
|
||||
from weekday import calculate_day_of_week
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("year", "month", "day"),
|
||||
[
|
||||
(yr, m, d)
|
||||
for yr in range(1600, 2021)
|
||||
for m in range(1, 12)
|
||||
for d in range(1, 28)
|
||||
],
|
||||
)
|
||||
@pytest.mark.slow # Those are 125,037 tests!
|
||||
def test_weekday_calc(year, month, day):
|
||||
dt = datetime.date(year, month, day)
|
||||
python_weekday = dt.weekday() # Monday = 0, Sunday = 6
|
||||
|
||||
basic_weekday = calculate_day_of_week(year, month, day) # Sunday = 1, Saturday = 7
|
||||
|
||||
if ((python_weekday + 2) % 7) != (basic_weekday % 7):
|
||||
print(f"testing yr {year} month {month} day {day}")
|
||||
print(f"python says {python_weekday}")
|
||||
print(f"BASIC says {basic_weekday}")
|
||||
assert False
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("year", "month", "day"),
|
||||
[
|
||||
(yr, m, d)
|
||||
for yr in range(2016, 2021)
|
||||
for m in range(1, 12)
|
||||
for d in range(1, 28)
|
||||
],
|
||||
)
|
||||
def test_weekday_calc_4_years(year, month, day):
|
||||
dt = datetime.date(year, month, day)
|
||||
python_weekday = dt.weekday() # Monday = 0, Sunday = 6
|
||||
|
||||
basic_weekday = calculate_day_of_week(year, month, day) # Sunday = 1, Saturday = 7
|
||||
|
||||
if ((python_weekday + 2) % 7) != (basic_weekday % 7):
|
||||
print(f"testing yr {year} month {month} day {day}")
|
||||
print(f"python says {python_weekday}")
|
||||
print(f"BASIC says {basic_weekday}")
|
||||
assert False
|
||||
@@ -142,7 +142,7 @@ def calculate_day_of_week(year, month, day):
|
||||
|
||||
|
||||
def end():
|
||||
for i in range(5):
|
||||
for _ in range(5):
|
||||
print()
|
||||
|
||||
|
||||
@@ -259,26 +259,6 @@ def main():
|
||||
end()
|
||||
|
||||
|
||||
def test_weekday_calc(year, month, day):
|
||||
dt = datetime.date(year, month, day)
|
||||
python_weekday = dt.weekday() # Monday = 0, Sunday = 6
|
||||
|
||||
basic_weekday = calculate_day_of_week(year, month, day) # Sunday = 1, Saturday = 7
|
||||
|
||||
if ((python_weekday + 2) % 7) != (basic_weekday % 7):
|
||||
print(f"testing yr {year} month {month} day {day}")
|
||||
print(f"python says {python_weekday}")
|
||||
print(f"BASIC says {basic_weekday}")
|
||||
assert False
|
||||
|
||||
|
||||
def test_harness():
|
||||
for yr in range(1600, 2021):
|
||||
for m in range(1, 12):
|
||||
for d in range(1, 28):
|
||||
test_weekday_calc(yr, m, d)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
|
||||
4
pyproject.toml
Normal file
4
pyproject.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
[tool.pytest.ini_options]
|
||||
markers = [
|
||||
"slow: marks tests as slow (deselect with '-m \"not slow\"')",
|
||||
]
|
||||
Reference in New Issue
Block a user