Use logging conditional formatting

This commit is contained in:
2019-12-22 06:50:14 -08:00
parent cb222cc6bb
commit 15220fffa6
2 changed files with 17 additions and 19 deletions

View File

@@ -74,7 +74,7 @@ def assemble_file(infile) -> bytes:
def assemble(lines: str) -> bytes: def assemble(lines: str) -> bytes:
instruction_map = build_instruction_map() instruction_map = build_instruction_map()
logger.debug("Instruction map: {}".format(instruction_map)) logger.debug("Instruction map: %s", instruction_map)
byte_offset = 0 byte_offset = 0
instruction_count = 0 instruction_count = 0
@@ -86,7 +86,7 @@ def assemble(lines: str) -> bytes:
return labels[label] return labels[label]
for step in ["SIZE", "CONTENT"]: for step in ["SIZE", "CONTENT"]:
logger.debug("Starting step: {}".format(step)) logger.debug("Starting step: %s", step)
for line_num, line in enumerate(lines): for line_num, line in enumerate(lines):
# Remove comments # Remove comments
@@ -94,8 +94,6 @@ def assemble(lines: str) -> bytes:
# Tokenize # Tokenize
tokens = line.split() tokens = line.split()
logging.info("Line: {}".format(line))
logging.info("Tokens: {}".format(tokens))
if len(tokens) == 0: if len(tokens) == 0:
continue continue
@@ -108,15 +106,14 @@ def assemble(lines: str) -> bytes:
if instruction_name[-1] == LABEL_SUFFIX: if instruction_name[-1] == LABEL_SUFFIX:
if step == 'SIZE': if step == 'SIZE':
label = instruction_name[:-1] label = instruction_name[:-1]
logger.debug("Found label '{}' at {}" logger.debug("Found label '%s' at %s", label, byte_offset)
.format(label, byte_offset))
if label in labels.keys(): if label in labels.keys():
raise KeyError("Label '{}' defined at {} and {}" raise KeyError("Label '%s' defined at %s and %s",
.format(label, labels[label], line_num)) label, labels[label], line_num)
labels[label] = byte_offset labels[label] = byte_offset
continue continue
raise KeyError("Unknown instruction \"{}\" on line {}" raise KeyError("Unknown instruction \"%s\" on line %s",
.format(instruction_name, line_num)) instruction_name, line_num)
if step == 'SIZE': if step == 'SIZE':
byte_offset += parse_line_size(instruction, args) byte_offset += parse_line_size(instruction, args)
@@ -125,12 +122,11 @@ def assemble(lines: str) -> bytes:
try: try:
program += parse_line_bytes(instruction, args, label_resolver) program += parse_line_bytes(instruction, args, label_resolver)
except ValueError: except ValueError:
raise ValueError("Failed to parse line {},\n{}" raise ValueError("Failed to parse line %s,\n%s", line_num, line)
.format(line_num, line))
if step == 'SIZE': if step == 'SIZE':
logger.info("Program size: {} bytes, {} instructions" logger.info("Program size: %s bytes, %s instructions",
.format(byte_offset, instruction_count)) byte_offset, instruction_count)
logger.debug("Found labels: {}".format(labels)) logger.debug("Found labels: %s", labels)
return program return program

View File

@@ -6,10 +6,12 @@ import yaml
import pytest import pytest
import logging import logging
logger = logging.getLogger(__name__)
if __name__ == "__main__":
logging.basicConfig(format="") logging.basicConfig(format="")
logging.getLogger().setLevel(logging.INFO) logging.getLogger().setLevel(logging.INFO)
logger = logging.getLogger(__name__)
class AssembleCase(object): class AssembleCase(object):
@@ -35,7 +37,7 @@ def get_test_cases(subdir: str):
try: try:
case = AssembleCase(desc['name'], desc['program'], bytes(desc['expected'])) case = AssembleCase(desc['name'], desc['program'], bytes(desc['expected']))
except TypeError: except TypeError:
logger.exception("Failed to parse yaml: {}".format(desc)) logger.exception("Failed to parse yaml: %s", desc)
cases.append(case) cases.append(case)
return cases return cases