From 1fdb3ade4a84e371dcd5f0c32922662b7830173d Mon Sep 17 00:00:00 2001 From: Trond Linjordet Date: Fri, 9 Jul 2021 12:09:04 +0200 Subject: [PATCH] Add dummy class, docstrings, type hints; argparse This addresses #8, but not sure it closes it, addresses all issues. Black doesn't break up long function definitions at column 80, can that be automated by config? --- python/example_script.py | 69 ++++++++++++++++++++++++++++++++++------ 1 file changed, 60 insertions(+), 9 deletions(-) diff --git a/python/example_script.py b/python/example_script.py index 01210f4..1421dc3 100644 --- a/python/example_script.py +++ b/python/example_script.py @@ -8,11 +8,12 @@ Typical usage example in shell CLI: - python example_script.py -i input.txt -o output.txt -m buzzfizz.json + python example_script.py -i input.txt -o output.txt -m buzzfizz.json \ + -x happy Or simply: - python example_script.py -i input.txt -o output.txt + python example_script.py -i input.txt -o output.txt -x happy """ @@ -20,8 +21,6 @@ import json import typing -def merge_conflict_function(): - pass def dummy_line_modification(line: str, i: int, mods: typing.Dict[str, dict]) -> str: """Defines the modification to be perfomed for a single input line. @@ -47,7 +46,7 @@ def dummy_line_modification(line: str, i: int, mods: typing.Dict[str, dict]) -> return output_line + "\n" -def dummy_function(input_path: str, output_path: str, mod_path: str = None): +def dummy_function(input_path: str, output_path: str, mod_path: str = None) -> None: """Takes input from a file, modifies each line, and writes output accordingly, line by line. @@ -71,11 +70,52 @@ def dummy_function(input_path: str, output_path: str, mod_path: str = None): f_out.write(dummy_line_modification(line, i, mods)) +class DummyThing: + def __init__(self, mood: str, weight: float, color: str = "green") -> None: + """The class docstring goes under the __init__ method. + + Args: + mode: What mode the class should instantiate: happy or sad. + weight (float): How heavy this thing is in Earth gravity. + color (str): The optical color label of the imaginary object. + """ + self._mood = mood + self._color = color + self._weight = weight + + def weight(self) -> float: + """Methods can be defined that reveal private variables. + + Returns: + float: The value of the weight of the object. + """ + return self._weight + + def act(self, command: str) -> str: + """The object returns a string based on the command and mode. + + Args: + command: Tell the object whether to sing or dance. + + Returns: + A description of the resulting behavior. + """ + if command == "sing": + if self._mood == "happy": + return "This DummyThing sings 'What a Wonderful World'" + if self._mood == "sad": + return "This DummyThing sings 'Everybody Hurts'" + if command == "dance": + if self._mood == "happy": + return "This DummyThing bobs and weaves to the beat" + if self._mood == "sad": + return "Oh no, this DummyThing fell down." + + if __name__ == "__main__": # Parse required (or expected) arguments passed from command-line call. parser = argparse.ArgumentParser() - named = parser.add_argument_group("Named arguments") - named.add_argument( + parser.add_argument( "-i", "--input_path", dest="input_path", @@ -83,7 +123,7 @@ def dummy_function(input_path: str, output_path: str, mod_path: str = None): default="input.txt", required=True, ) - named.add_argument( + parser.add_argument( "-o", "--output_path", dest="output_path", @@ -91,7 +131,7 @@ def dummy_function(input_path: str, output_path: str, mod_path: str = None): default="output.txt", required=True, ) - named.add_argument( + parser.add_argument( "-m", "--modification_path", dest="mod_path", @@ -99,7 +139,18 @@ def dummy_function(input_path: str, output_path: str, mod_path: str = None): default=None, required=False, ) + parser.add_argument( + "-x", + "--mood", + dest="mood", + help="The mode argument for the DummyClass object", + default=None, + required=True, + ) args = parser.parse_args() # Execute script given parsed arguments. dummy_function(args.input_path, args.output_path, args.mod_path) + my_dummything = DummyThing(args.mood, 456.9) + print(my_dummything.act("sing")) + print(my_dummything.act("dance"))