Skip to content

Commit 149e255

Browse files
committed
Reformatted some line lengths
1 parent 424c83a commit 149e255

File tree

23 files changed

+213
-141
lines changed

23 files changed

+213
-141
lines changed

Brainfuck/__main__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
if __name__ == "__main__":
2020
# Parse the file argument
2121
file_parser = ArgumentParser("NanoBASIC")
22-
file_parser.add_argument("brainfuck_file", help="A text file containing Brainfuck source code.")
22+
file_parser.add_argument("brainfuck_file",
23+
help="A file containing Brainfuck source code.")
2324
arguments = file_parser.parse_args()
2425
Brainfuck(arguments.brainfuck_file).execute()

Brainfuck/brainfuck.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def execute(self):
4848
instruction_index = self.find_bracket_match(instruction_index, False)
4949
instruction_index += 1
5050

51-
# Find the location of the corresponding matching bracket to the one at *start*
51+
# Find the location of the corresponding bracket to the one at *start*
5252
# If *forward* is true go to the right looking for a matching "]"
5353
# Otherwise do the reverse
5454
def find_bracket_match(self, start: int, forward: bool) -> int:
@@ -66,7 +66,7 @@ def find_bracket_match(self, start: int, forward: bool) -> int:
6666
in_between_brackets += 1
6767
location += direction
6868
# Didn't find a match
69-
print(f"Error: could not find matching bracket for {start_bracket} at {start}.")
69+
print(f"Error: could not find match for {start_bracket} at {start}.")
7070
return start
7171

7272

Chip8/__main__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
def run(program_data: bytes, name: str):
2626
# Startup Pygame, create the window, and load the sound
2727
pygame.init()
28-
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.SCALED)
28+
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT),
29+
pygame.SCALED)
2930
pygame.display.set_caption(f"Chip8 - {os.path.basename(name)}")
30-
bee_sound = pygame.mixer.Sound(os.path.dirname(os.path.realpath(__file__)) + "/bee.wav")
31+
bee_sound = pygame.mixer.Sound(os.path.dirname(os.path.realpath(__file__))
32+
+ "/bee.wav")
3133
currently_playing_sound = False
3234
vm = VM(program_data) # Load the virtual machine with the program data
3335
timer_accumulator = 0.0 # Used to limit the timer to 60 Hz
@@ -63,7 +65,7 @@ def run(program_data: bytes, name: str):
6365

6466
# Handle timing
6567
frame_end = timer()
66-
frame_time = frame_end - frame_start # time it took for this iteration in seconds
68+
frame_time = frame_end - frame_start # time it took in seconds
6769
timer_accumulator += frame_time
6870
# Every 1/60 of a second decrement the timers
6971
if timer_accumulator > TIMER_DELAY:
@@ -79,7 +81,8 @@ def run(program_data: bytes, name: str):
7981
if __name__ == "__main__":
8082
# Parse the file argument
8183
file_parser = ArgumentParser("Chip8")
82-
file_parser.add_argument("rom_file", help="A file containing a Chip-8 game.")
84+
file_parser.add_argument("rom_file",
85+
help="A file containing a Chip-8 game.")
8386
arguments = file_parser.parse_args()
8487
with open(arguments.rom_file, "rb") as fp:
8588
file_data = fp.read()

Chip8/vm.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ def __init__(self, program_data: bytes):
8181
# ours can just be unlimited and expand/contract as needed
8282
self.stack = []
8383
# Graphics buffer for the screen - 64 x 32 pixels
84-
self.display_buffer = np.zeros((SCREEN_WIDTH, SCREEN_HEIGHT), dtype=np.uint32)
84+
self.display_buffer = np.zeros((SCREEN_WIDTH, SCREEN_HEIGHT),
85+
dtype=np.uint32)
8586
self.needs_redraw = False
8687
# Timers - really simple registers that count down to 0 at 60 hertz
8788
self.delay_timer = 0
@@ -100,9 +101,9 @@ def decrement_timers(self):
100101
def play_sound(self) -> bool:
101102
return self.sound_timer > 0
102103

103-
# Draw a sprite at *x*, *y* using data at *i* and with a height of *height*
104+
# Draw a sprite at *x*, *y* using data at *i* with a height of *height*
104105
def draw_sprite(self, x: int, y: int, height: int):
105-
flipped_black = False # did drawing this flip any pixels from white to black?
106+
flipped_black = False # did drawing this flip any pixels?
106107
for row in range(0, height):
107108
row_bits = self.ram[self.i + row]
108109
for col in range(0, SPRITE_WIDTH):
@@ -112,11 +113,13 @@ def draw_sprite(self, x: int, y: int, height: int):
112113
continue # Ignore off-screen pixels
113114
new_bit = (row_bits >> (7 - col)) & 1
114115
old_bit = self.display_buffer[px, py] & 1
115-
if new_bit & old_bit: # If both are set, they will get flipped white -> black
116+
if new_bit & old_bit: # If both set, flip white -> black
116117
flipped_black = True
117-
new_pixel = new_bit ^ old_bit # Chip 8 draws by XORing, which flips everything
118+
# Chip 8 draws by XORing, which flips everything
119+
new_pixel = new_bit ^ old_bit
118120
self.display_buffer[px, py] = WHITE if new_pixel else BLACK
119-
self.v[0xF] = 1 if flipped_black else 0 # set flipped flag for collision detection
121+
# set flipped flag for collision detection
122+
self.v[0xF] = 1 if flipped_black else 0
120123

121124
def step(self):
122125
# we look at the opcode in terms of its nibbles (4 bit pieces)
@@ -139,7 +142,7 @@ def step(self):
139142
self.pc = self.stack.pop()
140143
jumped = True
141144
case (0x0, n1, n2, n3): # call program
142-
self.pc = concat_nibbles(n1, n2, n3) # go to program start
145+
self.pc = concat_nibbles(n1, n2, n3) # go to start
143146
# clear registers
144147
self.delay_timer = 0
145148
self.sound_timer = 0
@@ -153,7 +156,7 @@ def step(self):
153156
self.pc = concat_nibbles(n1, n2, n3)
154157
jumped = True
155158
case (0x2, n1, n2, n3): # call subroutine
156-
self.stack.append(self.pc + 2) # return location onto the stack
159+
self.stack.append(self.pc + 2) # put return place on stack
157160
self.pc = concat_nibbles(n1, n2, n3) # goto subroutine
158161
jumped = True
159162
case (0x3, x, _, _): # conditional skip v[x] equal last2
@@ -261,7 +264,8 @@ def step(self):
261264
for r in range(0, x + 1):
262265
self.v[r] = self.ram[self.i + r]
263266
case _:
264-
print(f"Unknown opcode {(hex(first), hex(second), hex(third), hex(fourth))}!")
267+
print(f"Unknown opcode {(hex(first), hex(second),
268+
hex(third), hex(fourth))}!")
265269

266270
if not jumped:
267271
self.pc += 2 # increment program counter

Impressionist/__main__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919
if __name__ == "__main__":
2020
# Parse the file argument
2121
argument_parser = ArgumentParser("Impressionist")
22-
argument_parser.add_argument("image_file", help="The input image to be painted.")
23-
argument_parser.add_argument("output_file", help="The final resulting abstract art image.")
22+
argument_parser.add_argument("image_file", help="The input image")
23+
argument_parser.add_argument("output_file", help="The resulting abstract art")
2424
argument_parser.add_argument('-t', '--trials', type=int, default=10000,
2525
help='The number of trials to run (default 10000).')
26-
argument_parser.add_argument('-m', '--method', choices=['random', 'average', 'common'],
27-
default='average',
26+
argument_parser.add_argument('-m', '--method',
27+
choices=['random', 'average', 'common'], default='average',
2828
help='Shape color determination method (default average).')
2929
argument_parser.add_argument('-s', '--shape', choices=['ellipse', 'triangle',
30-
'quadrilateral', 'line'], default='ellipse',
31-
help='The shape type to use (default ellipse).')
30+
'quadrilateral', 'line'],
31+
default='ellipse', help='The shape type (default ellipse).')
3232
argument_parser.add_argument('-l', '--length', type=int, default=256,
3333
help='The length of the final image in pixels (default 256).')
3434
argument_parser.add_argument('-v', '--vector', default=False, action='store_true',

Impressionist/impressionist.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def experiment() -> bool:
125125
return False
126126

127127
if experiment():
128-
# Try expanding every direction, keep expanding in any directions that are better
128+
# Try expanding every direction, keep going in better directions
129129
for index in range(len(coordinates)):
130130
for amount in (-1, 1):
131131
while True:

Impressionist/svg.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ def __init__(self, width: int, height: int, background_color: tuple[int, int, in
2323

2424
def draw_ellipse(self, x1: int, y1: int, x2: int, y2: int, color: tuple[int, int, int]):
2525
self.content += f'<ellipse cx="{(x1 + x2) // 2}" cy="{(y1 + y2) // 2}" ' \
26-
f'rx="{abs(x1 - x2) // 2}" ry="{abs(y1 - y2) // 2}" fill="rgb{color}" />\n'
26+
(f'rx="{abs(x1 - x2) // 2}" ry="{abs(y1 - y2) // 2}'
27+
f'" fill="rgb{color}" />\n')
2728

2829
def draw_line(self, x1: int, y1: int, x2: int, y2: int, color: tuple[int, int, int]):
2930
self.content += f'<line x1="{x1}" y1="{y1}" x2="{x2}" y2="{y2}" stroke="rgb{color}" ' \

KNN/__main__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@
3030

3131
def run():
3232
# Create a 2D array of pixels to represent the digit
33-
digit_pixels = np.zeros((PIXEL_HEIGHT, PIXEL_WIDTH, 3), dtype=np.uint32)
33+
digit_pixels = np.zeros((PIXEL_HEIGHT, PIXEL_WIDTH, 3),
34+
dtype=np.uint32)
3435
# Load the training data
3536
os.chdir(os.path.dirname(os.path.abspath(__file__)))
36-
digits_knn = KNN(Digit, './datasets/digits/digits.csv', has_header=False)
37+
digits_knn = KNN(Digit, './datasets/digits/digits.csv',
38+
has_header=False)
3739
# Startup Pygame, create the window
3840
pygame.init()
39-
screen = pygame.display.set_mode(size=(PIXEL_WIDTH, PIXEL_HEIGHT), flags=pygame.SCALED | pygame.RESIZABLE)
41+
screen = pygame.display.set_mode(size=(PIXEL_WIDTH, PIXEL_HEIGHT),
42+
flags=pygame.SCALED | pygame.RESIZABLE)
4043
pygame.display.set_caption("Digit Recognizer")
4144
while True:
4245
pygame.surfarray.blit_array(screen, digit_pixels)

KNN/fish.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ def from_string_data(cls, data: list[str]) -> Self:
3535
height=float(data[5]), width=float(data[6]))
3636

3737
def distance(self, other: Self) -> float:
38-
return ((self.length1 - other.length1) ** 2 + (self.length2 - other.length2) ** 2 +
39-
(self.length3 - other.length3) ** 2 + (self.height - other.height) ** 2 +
38+
return ((self.length1 - other.length1) ** 2 +
39+
(self.length2 - other.length2) ** 2 +
40+
(self.length3 - other.length3) ** 2 +
41+
(self.height - other.height) ** 2 +
4042
(self.width - other.width) ** 2) ** 0.5

KNN/knn.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def distance(self, other: Self) -> float: ...
2929

3030

3131
class KNN[DP: DataPoint]:
32-
def __init__(self, data_point_type: type[DP], file_path: str, has_header: bool = True) -> None:
32+
def __init__(self, data_point_type: type[DP], file_path: str,
33+
has_header: bool = True) -> None:
3334
self.data_point_type = data_point_type
3435
self.data_points = []
3536
self._read_csv(file_path, has_header)
@@ -41,9 +42,10 @@ def _read_csv(self, file_path: str, has_header: bool) -> None:
4142
if has_header:
4243
_ = next(reader)
4344
for row in reader:
44-
self.data_points.append(self.data_point_type.from_string_data(row))
45+
self.data_points.append(
46+
self.data_point_type.from_string_data(row))
4547

46-
# Find the k nearest neighbors of a given data point based on the distance method
48+
# Find the k nearest neighbors of a given data point
4749
def nearest(self, k: int, data_point: DP) -> list[DP]:
4850
return sorted(self.data_points, key=data_point.distance)[:k]
4951

@@ -57,10 +59,12 @@ def classify(self, k: int, data_point: DP) -> str:
5759
# Find the average of that property from the neighbors and return it
5860
def predict(self, k: int, data_point: DP, property_name: str) -> float:
5961
neighbors = self.nearest(k, data_point)
60-
return sum([getattr(neighbor, property_name) for neighbor in neighbors]) / len(neighbors)
62+
return (sum([getattr(neighbor, property_name) for neighbor in neighbors])
63+
/ len(neighbors))
6164

6265
# Predict a NumPy array property of a data point based on the k nearest neighbors
6366
# Find the average of that property from the neighbors and return it
6467
def predict_array(self, k: int, data_point: DP, property_name: str) -> np.ndarray:
6568
neighbors = self.nearest(k, data_point)
66-
return np.sum([getattr(neighbor, property_name) for neighbor in neighbors], axis=0) / len(neighbors)
69+
return (np.sum([getattr(neighbor, property_name) for neighbor in neighbors], axis=0)
70+
/ len(neighbors))

0 commit comments

Comments
 (0)