From 15220fffa6d0de20898f6ef9dbf17a016c2793c4 Mon Sep 17 00:00:00 2001 From: Max Regan Date: Sun, 22 Dec 2019 06:50:14 -0800 Subject: [PATCH] Use logging conditional formatting --- src/python/src/gbasm/gbasm.py | 26 +++++++++++--------------- src/python/test/test_assemble.py | 10 ++++++---- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/python/src/gbasm/gbasm.py b/src/python/src/gbasm/gbasm.py index bc7aaf1..da7235e 100755 --- a/src/python/src/gbasm/gbasm.py +++ b/src/python/src/gbasm/gbasm.py @@ -74,7 +74,7 @@ def assemble_file(infile) -> bytes: def assemble(lines: str) -> bytes: instruction_map = build_instruction_map() - logger.debug("Instruction map: {}".format(instruction_map)) + logger.debug("Instruction map: %s", instruction_map) byte_offset = 0 instruction_count = 0 @@ -86,7 +86,7 @@ def assemble(lines: str) -> bytes: return labels[label] for step in ["SIZE", "CONTENT"]: - logger.debug("Starting step: {}".format(step)) + logger.debug("Starting step: %s", step) for line_num, line in enumerate(lines): # Remove comments @@ -94,8 +94,6 @@ def assemble(lines: str) -> bytes: # Tokenize tokens = line.split() - logging.info("Line: {}".format(line)) - logging.info("Tokens: {}".format(tokens)) if len(tokens) == 0: continue @@ -108,15 +106,14 @@ def assemble(lines: str) -> bytes: if instruction_name[-1] == LABEL_SUFFIX: if step == 'SIZE': label = instruction_name[:-1] - logger.debug("Found label '{}' at {}" - .format(label, byte_offset)) + logger.debug("Found label '%s' at %s", label, byte_offset) if label in labels.keys(): - raise KeyError("Label '{}' defined at {} and {}" - .format(label, labels[label], line_num)) + raise KeyError("Label '%s' defined at %s and %s", + label, labels[label], line_num) labels[label] = byte_offset continue - raise KeyError("Unknown instruction \"{}\" on line {}" - .format(instruction_name, line_num)) + raise KeyError("Unknown instruction \"%s\" on line %s", + instruction_name, line_num) if step == 'SIZE': byte_offset += parse_line_size(instruction, args) @@ -125,12 +122,11 @@ def assemble(lines: str) -> bytes: try: program += parse_line_bytes(instruction, args, label_resolver) except ValueError: - raise ValueError("Failed to parse line {},\n{}" - .format(line_num, line)) + raise ValueError("Failed to parse line %s,\n%s", line_num, line) if step == 'SIZE': - logger.info("Program size: {} bytes, {} instructions" - .format(byte_offset, instruction_count)) - logger.debug("Found labels: {}".format(labels)) + logger.info("Program size: %s bytes, %s instructions", + byte_offset, instruction_count) + logger.debug("Found labels: %s", labels) return program diff --git a/src/python/test/test_assemble.py b/src/python/test/test_assemble.py index faf1551..25522f0 100644 --- a/src/python/test/test_assemble.py +++ b/src/python/test/test_assemble.py @@ -6,11 +6,13 @@ import yaml import pytest import logging -logging.basicConfig(format="") -logging.getLogger().setLevel(logging.INFO) - logger = logging.getLogger(__name__) +if __name__ == "__main__": + logging.basicConfig(format="") + logging.getLogger().setLevel(logging.INFO) + + class AssembleCase(object): def __init__(self, name: str, program: str, expected: bytes): @@ -35,7 +37,7 @@ def get_test_cases(subdir: str): try: case = AssembleCase(desc['name'], desc['program'], bytes(desc['expected'])) except TypeError: - logger.exception("Failed to parse yaml: {}".format(desc)) + logger.exception("Failed to parse yaml: %s", desc) cases.append(case) return cases