forked from iai-group/guidelines
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d6804c9
commit 779aadf
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
"""An example Python script showing IAI Style Guide format and argparse usage. | ||
This Python script implements a dummy function that takes input and output file | ||
paths. The main purpose of this script is to show how a Python file should look | ||
in order to be presentable and conform to the IAI Python Style Guide. As a | ||
bonus, the usage of the argparse library is demonstrated for use with command- | ||
line interface (CLI). | ||
Typical usage example in shell CLI: | ||
python example_script.py -in ../foo/input.txt -out ../bar/output.txt | ||
:Author: Trond Linjordet | ||
""" | ||
|
||
import argparse | ||
|
||
def dummy_line_modification(line, i): | ||
"""Defines the modification to be perfomed for a single input line. | ||
Args: | ||
line: A string. | ||
i: An integer. | ||
Returns: | ||
Modified form of the input line. | ||
""" | ||
return line.rstrip('\n')+'Added dummy modification '+str(i)+'.\n' | ||
|
||
|
||
def dummy_function(input_path, output_path): | ||
"""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. | ||
""" | ||
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)) | ||
|
||
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) | ||
args = parser.parse_args() | ||
input_path = args.input_path | ||
output_path = args.output_path | ||
|
||
# Execute script given parsed arguments. | ||
dummy_function(input_path, output_path) |