diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3553b65 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +python/.idea/* +python/output.txt +.DS_Store diff --git a/python/buzzfizz.json b/python/buzzfizz.json new file mode 100644 index 0000000..68d06b6 --- /dev/null +++ b/python/buzzfizz.json @@ -0,0 +1,14 @@ +{ + "fizz": { + "0": "buzz", + "1": "bubble", + "2": "bzzzt", + "3": "brass", + "4": "borscht" + }, + "buzz": { + "0": "fizz", + "1": "frappuccino", + "2": "frazzle" + } +} diff --git a/python/example_script.py b/python/example_script.py index 245372b..777e31b 100644 --- a/python/example_script.py +++ b/python/example_script.py @@ -8,52 +8,100 @@ Typical usage example in shell CLI: - python example_script.py -in ../foo/input.txt -out ../bar/output.txt + python example_script.py -i input.txt -o output.txt -m buzzfizz.json + + Or simply: + + python example_script.py -i input.txt -o output.txt :Author: Trond Linjordet """ import argparse +import json + -def dummy_line_modification(line, i): +def dummy_line_modification(line: str, i: int, mods: dict) -> str: """Defines the modification to be perfomed for a single input line. Args: - line: A string. - i: An integer. + line: The line from the input file to be modified. + i: The line number where the line argument was read. + mods: Returns: Modified form of the input line. """ - return line.rstrip('\n')+'Added dummy modification '+str(i)+'.\n' + output_line = line.rstrip("\n") + " \t | " + if not mods == dict(): + if i % 3 == 0 and i % 5 == 0: + output_line += str(mods["fizz"][str(i % 5)]) \ + + str(mods["buzz"][str(i % 3)]) + elif i % 3 == 0: + output_line += str(mods["fizz"][str(i % 5)]) + elif i % 5 == 0: + output_line += str(mods["buzz"][str(i % 3)]) + else: + output_line += "Added standard modification " + str(i) + "." + return output_line + "\n" -def dummy_function(input_path, output_path): +def dummy_function(input_path: str, output_path: str, mod_path=None): """Takes input from a file, modifies each line, and writes output accordingly, line by line. Args: input_path: A file path to an existing file. output_path: A file path to a non-existent file. + mod_path: A file path to a JSON file with a dictionary to modify lines. """ - with open(input_path, 'r', encoding='utf8') as f_in, \ - open(output_path, 'w', encoding='utf8') as f_out: + if mod_path is not None: + assert isinstance(mod_path, str) + assert mod_path[-5:] == ".json" + with open(mod_path) as json_file: + mods = json.load(json_file) + else: + mods = dict() + + with open(input_path, "r", encoding="utf8") as f_in, open( + output_path, "w", encoding="utf8" + ) as f_out: for i, line in enumerate(f_in): - f_out.write(dummy_line_modification(line, i)) + f_out.write(dummy_line_modification(line, i, mods)) + -if __name__ == '__main__': +if __name__ == "__main__": # Parse required (or expected) arguments passed from command-line call. parser = argparse.ArgumentParser() - requiredNamed = parser.add_argument_group('Required named arguments') - requiredNamed.add_argument('-in', '--input_path', dest='input_path', - help='short explanation, input', - default='../foo/input.txt', - required=True) - requiredNamed.add_argument('-out', '--output_path', dest='output_path', - help='short explanation, output', - required=True) + required_named = parser.add_argument_group("Required named arguments") + required_named.add_argument( + "-i", + "--input_path", + dest="input_path", + help="short explanation, input", + default="input.txt", + required=True, + ) + required_named.add_argument( + "-o", + "--output_path", + dest="output_path", + help="short explanation, output", + default="output.txt", + required=True, + ) + optional_named = parser.add_argument_group("Optional named arguments") + optional_named.add_argument( + "-m", + "--modification_path", + dest="mod_path", + help="short explanation, modifiying file", + default=None, + required=False, + ) args = parser.parse_args() input_path = args.input_path output_path = args.output_path + mod_path = args.mod_path # Execute script given parsed arguments. - dummy_function(input_path, output_path) + dummy_function(input_path, output_path, mod_path) diff --git a/python/input.txt b/python/input.txt new file mode 100644 index 0000000..5320f16 --- /dev/null +++ b/python/input.txt @@ -0,0 +1,16 @@ +"Now like to bees in summer's heate from hives, +Out flie the citizens, some here, some there; +Some all alone, and others with their wives: +With wives and children some flie, all for feare ! + +Here stands a watch, with guard of partizans, +To stoppe their passages, or to or fro, +As if they were not men, nor Christians, +But fiends or monsters, murdering as they go. + +Each village, free, now stands upon her guard, +None must have harbour in them but their owne; +And as for life and death all watch and ward, +And flie for life (as death) the man unknowne !" + +---- Sir John Davies (1569 – 1626)