Skip to content

Commit a18caaa

Browse files
committed
Changes to use pathlib
1 parent 4c5730b commit a18caaa

File tree

8 files changed

+69
-69
lines changed

8 files changed

+69
-69
lines changed

Brainfuck/brainfuck.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
16+
from pathlib import Path
17+
18+
1619
class Brainfuck:
17-
def __init__(self, file_name: str):
20+
def __init__(self, file_name: str | Path):
1821
# Open text file and store in instance variable
1922
with open(file_name, "r") as text_file:
2023
self.source_code: str = text_file.read()

KNN/knn.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
16+
from pathlib import Path
1617
import csv
1718
from typing import Protocol, Self
1819
from collections import Counter
@@ -29,14 +30,14 @@ def distance(self, other: Self) -> float: ...
2930

3031

3132
class KNN[DP: DataPoint]:
32-
def __init__(self, data_point_type: type[DP], file_path: str,
33+
def __init__(self, data_point_type: type[DP], file_path: str | Path,
3334
has_header: bool = True) -> None:
3435
self.data_point_type = data_point_type
3536
self.data_points = []
3637
self._read_csv(file_path, has_header)
3738

3839
# Read a CSV file and return a list of data points
39-
def _read_csv(self, file_path: str, has_header: bool) -> None:
40+
def _read_csv(self, file_path: str | Path, has_header: bool) -> None:
4041
with open(file_path, 'r') as f:
4142
reader = csv.reader(f)
4243
if has_header:

NESEmulator/rom.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
16+
from pathlib import Path
1617
from struct import unpack
1718
from collections import namedtuple
1819
from array import array
@@ -27,8 +28,8 @@
2728

2829

2930
class ROM:
30-
def __init__(self, filename: str):
31-
with open(filename, "rb") as file:
31+
def __init__(self, file_name: str | Path):
32+
with open(file_name, "rb") as file:
3233
# Read Header and Check Signature "NES"
3334
self.header = Header._make(unpack("!LBBBBBBB5s",
3435
file.read(HEADER_SIZE)))

NanoBASIC/executioner.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
16+
from pathlib import Path
1617
from NanoBASIC.tokenizer import tokenize
1718
from NanoBASIC.parser import Parser
1819
from NanoBASIC.interpreter import Interpreter
1920

2021

21-
def execute(file_name: str):
22+
def execute(file_name: str | Path):
2223
# Load the text file from the argument
2324
# Tokenize, parse, and execute it
2425
with open(file_name, "r") as text_file:

tests/test_brainfuck.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@
1919
# output against the expected output.
2020
import unittest
2121
import sys
22-
import os
2322
from pathlib import Path
2423
from io import StringIO
2524
from Brainfuck.brainfuck import Brainfuck
2625

2726

2827
# Tokenizes, parses, and interprets a Brainfuck
2928
# program; stores the output in a string and returns it
30-
def run(file_name: str) -> str:
29+
def run(file_name: str | Path) -> str:
3130
output_holder = StringIO()
3231
sys.stdout = output_holder
3332
Brainfuck(file_name).execute()
@@ -36,28 +35,27 @@ def run(file_name: str) -> str:
3635

3736
class BrainfuckTestCase(unittest.TestCase):
3837
def setUp(self) -> None:
39-
# Change working directory to this file so we can easily access
40-
# the Examples directory where the test Brainfuck code resides
41-
os.chdir(Path(__file__).resolve().parent)
38+
self.example_folder = (Path(__file__).resolve().parent.parent
39+
/ 'Brainfuck' / 'Examples')
4240

4341
def test_hello_world(self):
44-
program_output = run("../Brainfuck/Examples/hello_world_verbose.bf")
42+
program_output = run(self.example_folder / "hello_world_verbose.bf")
4543
expected = "Hello World!\n"
4644
self.assertEqual(program_output, expected)
4745

4846
def test_fibonacci(self):
49-
program_output = run("../Brainfuck/Examples/fibonacci.bf")
47+
program_output = run(self.example_folder / "fibonacci.bf")
5048
expected = "1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89"
5149
self.assertEqual(program_output, expected)
5250

5351
def test_cell_size(self):
54-
program_output = run("../Brainfuck/Examples/cell_size.bf")
52+
program_output = run(self.example_folder / "cell_size.bf")
5553
expected = "8 bit cells\n"
5654
self.assertEqual(program_output, expected)
5755

5856
def test_beer(self):
59-
program_output = run("../Brainfuck/Examples/beer.bf")
60-
with open("../Brainfuck/Examples/beer.out", "r") as text_file:
57+
program_output = run(self.example_folder / "beer.bf")
58+
with open(self.example_folder / "beer.out", "r") as text_file:
6159
expected = text_file.read()
6260
self.assertEqual(program_output, expected)
6361

tests/test_knn.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,21 @@
1717
# DESCRIPTION
1818
# Tries running multiple different tests to verify the correctness of our KNN implementation.
1919
import unittest
20-
import os
21-
import csv
2220
from pathlib import Path
21+
import csv
2322
from KNN.knn import KNN
2423
from KNN.fish import Fish
2524
from KNN.digit import Digit
2625

2726

2827
class FishTestCase(unittest.TestCase):
2928
def setUp(self) -> None:
30-
# Change working directory to this file to get datasets
31-
os.chdir(Path(__file__).resolve().parent)
29+
self.data_file = (Path(__file__).resolve().parent.parent
30+
/ "KNN" / "datasets" / "fish" / "fish.csv")
3231

3332
def test_nearest(self):
3433
k: int = 3
35-
fish_knn = KNN(Fish, '../KNN/datasets/fish/fish.csv')
34+
fish_knn = KNN(Fish, str(self.data_file))
3635
test_fish: Fish = Fish("", 0.0, 30.0, 32.5, 38.0, 12.0, 5.0)
3736
nearest_fish: list[Fish] = fish_knn.nearest(k, test_fish)
3837
self.assertEqual(len(nearest_fish), k)
@@ -43,30 +42,31 @@ def test_nearest(self):
4342

4443
def test_classify(self):
4544
k: int = 5
46-
fish_knn = KNN(Fish, '../KNN/datasets/fish/fish.csv')
45+
fish_knn = KNN(Fish, str(self.data_file))
4746
test_fish: Fish = Fish("", 0.0, 20.0, 23.5, 24.0, 10.0, 4.0)
4847
classify_fish: str = fish_knn.classify(k, test_fish)
4948
self.assertEqual(classify_fish, "Parkki")
5049

5150
def test_predict(self):
5251
k: int = 5
53-
fish_knn = KNN(Fish, '../KNN/datasets/fish/fish.csv')
52+
fish_knn = KNN(Fish, self.data_file)
5453
test_fish: Fish = Fish("", 0.0, 20.0, 23.5, 24.0, 10.0, 4.0)
5554
predict_fish: float = fish_knn.predict(k, test_fish, "weight")
5655
self.assertEqual(predict_fish, 165.0)
5756

5857

5958
class DigitsTestCase(unittest.TestCase):
6059
def setUp(self) -> None:
61-
# Change working directory to this file to get datasets
62-
os.chdir(os.path.dirname(os.path.abspath(__file__)))
60+
self.data_file = (Path(__file__).resolve().parent.parent
61+
/ "KNN" / "datasets" / "digits" / "digits.csv")
62+
self.test_file = (Path(__file__).resolve().parent.parent
63+
/ "KNN" / "datasets" / "digits" / "digits_test.csv")
6364

6465
def test_digits_test_set(self):
6566
k: int = 1
66-
digits_knn = KNN(Digit, '../KNN/datasets/digits/digits.csv',
67-
has_header=False)
67+
digits_knn = KNN(Digit, self.data_file, has_header=False)
6868
test_data_points: list[Digit] = []
69-
with open('../KNN/datasets/digits/digits_test.csv', 'r') as f:
69+
with open(self.test_file, 'r') as f:
7070
reader = csv.reader(f)
7171
for row in reader:
7272
test_data_points.append(Digit.from_string_data(row))

tests/test_nanobasic.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@
1919
# output against the expected output.
2020
import unittest
2121
import sys
22-
import os
2322
from pathlib import Path
2423
from io import StringIO
2524
from NanoBASIC.executioner import execute
2625

2726

2827
# Tokenizes, parses, and interprets a NanoBASIC
2928
# program; stores the output in a string and returns it
30-
def run(file_name: str) -> str:
29+
def run(file_name: str | Path) -> str:
3130
output_holder = StringIO()
3231
sys.stdout = output_holder
3332
execute(file_name)
@@ -36,62 +35,61 @@ def run(file_name: str) -> str:
3635

3736
class NanoBASICTestCase(unittest.TestCase):
3837
def setUp(self) -> None:
39-
# Change working directory to this file so we can easily access
40-
# the Examples directory where the test NanoBASIC code resides
41-
os.chdir(Path(__file__).resolve().parent)
38+
self.example_folder = (Path(__file__).resolve().parent.parent
39+
/ 'NanoBASIC' / 'Examples')
4240

4341
def test_print1(self):
44-
program_output = run("../NanoBASIC/Examples/print1.bas")
42+
program_output = run(self.example_folder / "print1.bas")
4543
expected = "Hello World\n"
4644
self.assertEqual(program_output, expected)
4745

4846
def test_print2(self):
49-
program_output = run("../NanoBASIC/Examples/print2.bas")
47+
program_output = run(self.example_folder / "print2.bas")
5048
expected = "4\n12\n30\n7\n100\t9\n"
5149
self.assertEqual(program_output, expected)
5250

5351
def test_print3(self):
54-
program_output = run("../NanoBASIC/Examples/print3.bas")
52+
program_output = run(self.example_folder / "print3.bas")
5553
expected = "E is\t-31\n"
5654
self.assertEqual(program_output, expected)
5755

5856
def test_variables(self):
59-
program_output = run("../NanoBASIC/Examples/variables.bas")
57+
program_output = run(self.example_folder / "variables.bas")
6058
expected = "15\n"
6159
self.assertEqual(program_output, expected)
6260

6361
def test_goto(self):
64-
program_output = run("../NanoBASIC/Examples/goto.bas")
62+
program_output = run(self.example_folder / "goto.bas")
6563
expected = "Josh\nDave\nNanoBASIC ROCKS\n"
6664
self.assertEqual(program_output, expected)
6765

6866
def test_gosub(self):
69-
program_output = run("../NanoBASIC/Examples/gosub.bas")
67+
program_output = run(self.example_folder / "gosub.bas")
7068
expected = "10\n"
7169
self.assertEqual(program_output, expected)
7270

7371
def test_if1(self):
74-
program_output = run("../NanoBASIC/Examples/if1.bas")
72+
program_output = run(self.example_folder / "if1.bas")
7573
expected = "10\n40\n50\n60\n70\n100\n"
7674
self.assertEqual(program_output, expected)
7775

7876
def test_if2(self):
79-
program_output = run("../NanoBASIC/Examples/if2.bas")
77+
program_output = run(self.example_folder / "if2.bas")
8078
expected = "GOOD\n"
8179
self.assertEqual(program_output, expected)
8280

8381
def test_fib(self):
84-
program_output = run("../NanoBASIC/Examples/fib.bas")
82+
program_output = run(self.example_folder / "fib.bas")
8583
expected = "0\n1\n1\n2\n3\n5\n8\n13\n21\n34\n55\n89\n"
8684
self.assertEqual(program_output, expected)
8785

8886
def test_factorial(self):
89-
program_output = run("../NanoBASIC/Examples/factorial.bas")
87+
program_output = run(self.example_folder / "factorial.bas")
9088
expected = "120\n"
9189
self.assertEqual(program_output, expected)
9290

9391
def test_gcd(self):
94-
program_output = run("../NanoBASIC/Examples/gcd.bas")
92+
program_output = run(self.example_folder / "gcd.bas")
9593
expected = "7\n"
9694
self.assertEqual(program_output, expected)
9795

0 commit comments

Comments
 (0)