Skip to content

Commit

Permalink
Merge pull request #172 from OkieOth/sort_dict
Browse files Browse the repository at this point in the history
Sort dict
  • Loading branch information
OkieOth authored Nov 18, 2024
2 parents 8f2761f + 0e2924d commit 1fbcfd6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ADD randomDataServer.py randomDataServer.py
ADD customRandomConstraints.py customRandomConstraints.py
ADD validate.py validate.py
ADD validateSchemas.py validateSchemas.py
ADD sort_dict.py sort_dict.py
ADD version.txt version.txt
ADD requirements.txt requirements.txt
#ADD start_debug.sh start_debug.sh
Expand Down
66 changes: 66 additions & 0 deletions sort_dict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import json
import argparse
import sys

description = """Takes two JSON files as input
and aligns the order of the input file with the provided order by master
"""


parser = argparse.ArgumentParser(prog='sort_dict', description=description)
parser.add_argument('--master', help='path to the JSON file that provides the desired order')
parser.add_argument('--input', help='path to JSON file that needs the aligned order')
parser.add_argument('--output', help='path to the desired output file')


def load_json(file_path):
with open(file_path, 'r') as file:
return json.load(file)

def sort_dict(inputDict, masterDict):
outputDict = {}
if masterDict is not None:
for k, v in masterDict.items():
value = inputDict.get(k, None)
if value is not None:
outputDict[k] = value
if isinstance(value, dict):
outputDict[k] = sort_dict(value, v)
else:
outputDict[k] = value
inputDict.pop(k)
for k, v in inputDict.items():
outputDict[k] = v
return outputDict

if __name__ == '__main__':
args = parser.parse_args()
if args.master is None:
print("\nmissing input parameter '--master' for the file that provides the structure\n")
sys.exit(1)
if args.input is None:
print("\nmissing parameter '--input' for the file to read\n")
sys.exit(1)
if args.output is None:
print("\nmissing parameter '--output' for the output file to write\n")
sys.exit(1)

masterDict = load_json(args.master)
inputDict = load_json(args.input)

outputDict = {}

for k, v in masterDict.items():
value = inputDict.get(k, None)
if value is not None:
if isinstance(value, dict):
outputDict[k] = sort_dict(value, v)
else:
outputDict[k] = value
inputDict.pop(k)
for k, v in inputDict.items():
outputDict[k] = v

with open(args.output, 'w') as outfile:
json.dump(outputDict, outfile, indent=2)

2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.9.0
6.10.0

0 comments on commit 1fbcfd6

Please sign in to comment.