Skip to content

Commit 0595734

Browse files
committed
Python-ify lab4/assembler.
Practicing using Python best practices. Changes made: * Update assembler's .gitignore to latest GitHub version. * Add docstrings to all functions. * Rewrite the main assembler.py file to use a main() function. * Add type annotations to have the script pass mypy. * Modify MachineCode class member names to properly reflect none of the members are public. * Make linter modifications to have the script pass flake8.
1 parent 6255296 commit 0595734

File tree

6 files changed

+232
-130
lines changed

6 files changed

+232
-130
lines changed

lab4/assembler/.gitignore

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ parts/
2020
sdist/
2121
var/
2222
wheels/
23+
share/python-wheels/
2324
*.egg-info/
2425
.installed.cfg
2526
*.egg
@@ -38,14 +39,17 @@ pip-delete-this-directory.txt
3839
# Unit test / coverage reports
3940
htmlcov/
4041
.tox/
42+
.nox/
4143
.coverage
4244
.coverage.*
4345
.cache
4446
nosetests.xml
4547
coverage.xml
4648
*.cover
49+
*.py,cover
4750
.hypothesis/
4851
.pytest_cache/
52+
cover/
4953

5054
# Translations
5155
*.mo
@@ -55,6 +59,7 @@ coverage.xml
5559
*.log
5660
local_settings.py
5761
db.sqlite3
62+
db.sqlite3-journal
5863

5964
# Flask stuff:
6065
instance/
@@ -67,16 +72,34 @@ instance/
6772
docs/_build/
6873

6974
# PyBuilder
75+
.pybuilder/
7076
target/
7177

7278
# Jupyter Notebook
7379
.ipynb_checkpoints
7480

81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
7585
# pyenv
76-
.python-version
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
98+
__pypackages__/
7799

78-
# celery beat schedule file
100+
# Celery stuff
79101
celerybeat-schedule
102+
celerybeat.pid
80103

81104
# SageMath parsed files
82105
*.sage.py
@@ -102,3 +125,14 @@ venv.bak/
102125

103126
# mypy
104127
.mypy_cache/
128+
.dmypy.json
129+
dmypy.json
130+
131+
# Pyre type checker
132+
.pyre/
133+
134+
# pytype static type analyzer
135+
.pytype/
136+
137+
# Cython debug symbols
138+
cython_debug/

lab4/assembler/assembler.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,28 @@
33
import utils.output
44

55
from utils.machinecode import MachineCode
6+
from typing import List
67

7-
# command line argument processing
8-
args = utils.commandline.argument_parsing()
98

10-
# input file processing
11-
all_machine_code = []
12-
utils.input.parse_input(args, all_machine_code)
9+
def main() -> None:
10+
"""Take in input CSV with assembler instructions.
11+
Write assembly hex to stdout or output file.
12+
"""
13+
# command line argument processing
14+
args = utils.commandline.argument_parsing()
1315

14-
# data processing
15-
for current_machine_code in all_machine_code:
16-
current_machine_code.convert_to_bin()
17-
current_machine_code.convert_to_hex()
16+
# input file processing
17+
all_machine_code: List['MachineCode'] = []
18+
utils.input.parse_input(args, all_machine_code)
1819

19-
# output file processing
20-
utils.output.write_output(args, all_machine_code)
20+
# data processing
21+
for current_machine_code in all_machine_code:
22+
current_machine_code.convert_to_bin()
23+
current_machine_code.convert_to_hex()
24+
25+
# output file processing
26+
utils.output.write_output(args, all_machine_code)
27+
28+
29+
if __name__ == "__main__":
30+
main()
Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,45 @@
11
import argparse
22

3-
# command line argument parsing
4-
def argument_parsing():
3+
4+
def argument_parsing() -> argparse.Namespace:
5+
"""Parse command line arguments with argparse."""
56
parser = argparse.ArgumentParser(
6-
description = 'Take in a CSV file with Lab 4 assembly code. '
7-
'Output a text file to load into a RAM in Logisim Evolution. '
8-
'All values, including immediate values, must be in decimal. '
9-
'Immediate values can be negative.'
7+
description='Take in a CSV file with Lab 4 assembly code. Output '
8+
'a text file to load into a RAM in Logisim Evolution. All '
9+
'values, including immediate values, must be in decimal. '
10+
'Immediate values can be negative.'
1011
)
1112

1213
# mandatory input file
13-
parser.add_argument('infile',
14-
metavar = 'input.csv',
15-
help = 'Input CSV file with valid assembly code.'
14+
parser.add_argument(
15+
'infile',
16+
metavar='input.csv',
17+
help='Input CSV file with valid assembly code.'
1618
)
1719

1820
# optional output file
19-
parser.add_argument('outfile',
20-
nargs = '?',
21-
help = 'If specified, write to an output file instead of the console. '
22-
'Output file will be overwritten.'
21+
parser.add_argument(
22+
'outfile',
23+
nargs='?',
24+
help='If specified, write to an output file instead of the console. '
25+
'Output file will be overwritten.'
2326
)
2427

2528
# no header flag
26-
parser.add_argument('-n',
29+
parser.add_argument(
30+
'-n',
2731
'--noheader',
28-
action = 'store_true',
29-
help = 'Treat the first row of the file as a line of code. '
30-
'Normal behavior treats the first line as a header and skips it.'
32+
action='store_true',
33+
help='Treat the first row of the file as a line of code. '
34+
'Normal behavior treats the first line as a header and skips it.'
3135
)
3236

3337
# binary flag
34-
parser.add_argument('-b',
38+
parser.add_argument(
39+
'-b',
3540
'--binary',
36-
action = 'store_true',
37-
help = 'Export binary instead of hexadecimal.'
41+
action='store_true',
42+
help='Export binary instead of hexadecimal.'
3843
)
3944

4045
return parser.parse_args()

lab4/assembler/utils/input.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import csv
2-
import sys
32

43
from utils.machinecode import MachineCode
54

6-
# input file parsing
7-
def parse_input(args, all_machine_code):
5+
6+
def parse_input(args, all_machine_code) -> None:
7+
"""Parse input CSV into a list of MachineCode objects."""
88
# open file and begin reading data
99
with open(args.infile) as infile:
1010
csvreader = csv.reader(infile)
1111

1212
# skip header line depending on command line arguments
13-
if args.noheader == False:
13+
if args.noheader is False:
1414
next(csvreader)
1515

1616
# for every line in the csv file
@@ -43,5 +43,6 @@ def parse_input(args, all_machine_code):
4343
raise Exception(invalid_assembly_exception) from None
4444

4545
# create new machine code
46-
current_machine_code = MachineCode(op, rd, rs1, rs2, imm, line_number)
46+
current_machine_code = MachineCode(op, rd, rs1, rs2,
47+
imm, line_number)
4748
all_machine_code.append(current_machine_code)

0 commit comments

Comments
 (0)