From ab0934b77da63c131686882dbf616b27db69e5fe Mon Sep 17 00:00:00 2001 From: ferricles Date: Sat, 25 Jun 2022 16:35:09 -0700 Subject: [PATCH] refactor argument parsing from sys to argparse --- py_script/clean_json.py | 11 ++++++++--- py_script/compress_json.py | 11 ++++++++--- py_script/process_ings.py | 6 +++++- py_script/process_items.py | 7 ++++++- py_script/process_recipes.py | 8 ++++++-- 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/py_script/clean_json.py b/py_script/clean_json.py index 910a985..56894db 100644 --- a/py_script/clean_json.py +++ b/py_script/clean_json.py @@ -7,8 +7,13 @@ Usage: python clean_json.py [infile rel path] [outfile rel path] ''' if __name__ == "__main__": - import sys import json - infile = sys.argv[1] - outfile = sys.argv[2] + import argparse + + parser = argparse.ArgumentParser(description="Pull data from wynn API.") + parser.add_argument('infile', help='input file to read data from') + parser.add_argument('outfile', help='output file to dump clean data into') + args = parser.parse_args() + + infile, outfile = args.infile, args.outfile json.dump(json.load(open(infile)), open(outfile, "w"), indent = 2) diff --git a/py_script/compress_json.py b/py_script/compress_json.py index b021738..030f739 100644 --- a/py_script/compress_json.py +++ b/py_script/compress_json.py @@ -6,8 +6,13 @@ Usage: python compress_json.py [infile rel path] [outfile rel path] ''' if __name__ == "__main__": - import sys import json - infile = sys.argv[1] - outfile = sys.argv[2] + import argparse + + parser = argparse.ArgumentParser(description="Pull data from wynn API.") + parser.add_argument('infile', help='input file to read data from') + parser.add_argument('outfile', help='output file to dump clean data into') + args = parser.parse_args() + + infile, outfile = args.infile, args.outfile json.dump(json.load(open(infile)), open(outfile, "w")) diff --git a/py_script/process_ings.py b/py_script/process_ings.py index d0e9799..e1ebe8c 100644 --- a/py_script/process_ings.py +++ b/py_script/process_ings.py @@ -13,7 +13,11 @@ import os import base64 import argparse -infile, outfile = sys.argv[1], sys.argv[2] if len(sys.argv) > 2 else sys.argv[1] +parser = argparse.ArgumentParser(description="Process raw pulled ingredient data.") +parser.add_argument('infile', help='input file to read data from') +parser.add_argument('outfile', help='output file to dump clean data into') +args = parser.parse_args() +infile, outfile = args.infile, args.outfile with open(infile, "r") as in_file: ing_data = json.loads(in_file.read()) diff --git a/py_script/process_items.py b/py_script/process_items.py index 27e6ed6..2001271 100644 --- a/py_script/process_items.py +++ b/py_script/process_items.py @@ -14,8 +14,13 @@ import json import sys import os import base64 +import argparse -infile, outfile = sys.argv[1], sys.argv[2] if len(sys.argv) > 2 else sys.argv[1] +parser = argparse.ArgumentParser(description="Process raw pulled item data.") +parser.add_argument('infile', help='input file to read data from') +parser.add_argument('outfile', help='output file to dump clean data into') +args = parser.parse_args() +infile, outfile = args.infile, args.outfile with open(infile, "r") as in_file: data = json.loads(in_file.read()) diff --git a/py_script/process_recipes.py b/py_script/process_recipes.py index 7dad257..f2b13f5 100644 --- a/py_script/process_recipes.py +++ b/py_script/process_recipes.py @@ -11,9 +11,13 @@ import json import sys import os import base64 +import argparse -infile, outfile = sys.argv[1], sys.argv[2] if len(sys.argv) > 2 else sys.argv[1] - +parser = argparse.ArgumentParser(description="Process raw pulled recipe data.") +parser.add_argument('infile', help='input file to read data from') +parser.add_argument('outfile', help='output file to dump clean data into') +args = parser.parse_args() +infile, outfile = args.infile, args.outfile with open(infile, "r") as in_file: recipe_data = json.loads(in_file.read())