Skip to content

Commit

Permalink
Add dummy class, docstrings, type hints; argparse
Browse files Browse the repository at this point in the history
This addresses iai-group#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?
  • Loading branch information
tlinjordet committed Jul 9, 2021
1 parent e5e1202 commit 1fdb3ad
Showing 1 changed file with 60 additions and 9 deletions.
69 changes: 60 additions & 9 deletions python/example_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@
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
"""

import argparse
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.
Expand All @@ -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.
Expand All @@ -71,35 +70,87 @@ 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",
help="short explanation, input",
default="input.txt",
required=True,
)
named.add_argument(
parser.add_argument(
"-o",
"--output_path",
dest="output_path",
help="short explanation, output",
default="output.txt",
required=True,
)
named.add_argument(
parser.add_argument(
"-m",
"--modification_path",
dest="mod_path",
help="short explanation, modifiying file",
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"))

0 comments on commit 1fdb3ad

Please sign in to comment.