-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsoncheck.py
executable file
·103 lines (83 loc) · 3.28 KB
/
jsoncheck.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3.7
#
# Copyright (c) 2019, James C. McPherson. All Rights Reserved.
#
# Available under the terms of the MIT license:
#
# Permission is hereby granted, free of charge, to any
# person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the
# Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the
# Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice
# shall be included in all copies or substantial portions of
# the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import json
import sys
__doc__ = """
A simple (quick-n-dirty) script to compare the JSON contents of
two files.
Since this script is a checker of the output from SA1-to-mbpt.py,
we know that there are only four parts to check:
-- keyname
-- "jurisdiction"
-- "locality"
-- "coords"
"""
if __name__ == "__main__":
if len(sys.argv) < 3:
print("Two arguments are required")
sys.exit(1)
leftf = open(sys.argv[1], "r")
rightf = open(sys.argv[2], "r")
ljson = json.load(leftf)
rjson = json.load(rightf)
lkset = set(ljson.keys())
rkset = set(rjson.keys())
diffset = lkset - rkset
print("checking keys: left {left} vs right {right}".format(
left=len(lkset), right=len(rkset)))
print("difference: {diffset}".format(diffset=diffset))
print("checking each electorate's details")
for electorate in ljson.keys():
if electorate not in rjson:
print("Electorate {electorate} is not in {right}, "
"skipping".format(electorate=electorate,
right=sys.argv[2]))
continue
# set up some ease-of-access variables
lefte = ljson[electorate]
righte = rjson[electorate]
#
# If the jurisdictions don't match then our inputs are
# just plain wrong
ljur = lefte["jurisdiction"]
rjur = righte["jurisdiction"]
if ljur != rjur:
print("Jurisdictions do NOT match ({left} vs {right}) for "
"electorate {electorate}".format(
left=ljur, right=rjur, electorate=electorate))
lloc = lefte["locality"]
rloc = righte["locality"]
if lloc != rloc:
print("Localities do NOT match ({left} vs {right})".format(
left=lloc, right=rloc))
# Have we missed any coordinate points?
lcset = set(lefte["coords"][0])
rcset = set(righte["coords"][0])
diffset = lcset - rcset
print("Electorate of {electorate} has coordinate differences: "
"{diffset} ".format(diffset=diffset, electorate=electorate))