[Pkg-isocodes-devel] [iso-codes] 03/06: Ensure a stable sorting order in JSON data files

Tobias Quathamer toddy at moszumanska.debian.org
Sat Mar 5 19:28:37 UTC 2016


This is an automated email from the git hooks/post-receive script.

toddy pushed a commit to branch master
in repository iso-codes.

commit cfd2b59204240c15e5c8ec8d7637a795d19b1714
Author: Dr. Tobias Quathamer <toddy at debian.org>
Date:   Sat Mar 5 16:46:29 2016 +0100

    Ensure a stable sorting order in JSON data files
---
 bin/validate_json_data.py | 46 ++++++++++++++++++++++++++++++++++------------
 1 file changed, 34 insertions(+), 12 deletions(-)

diff --git a/bin/validate_json_data.py b/bin/validate_json_data.py
index 882b588..1b7a072 100755
--- a/bin/validate_json_data.py
+++ b/bin/validate_json_data.py
@@ -22,18 +22,40 @@ import json
 from jsonschema import validate
 
 standards = [
-	"639-2",
-	"639-3",
-	"639-5",
-	"3166-1",
-	"3166-2",
-	"3166-3",
-	"4217",
-	"15924",
+    "639-2",
+    "639-3",
+    "639-5",
+    "3166-1",
+    "3166-2",
+    "3166-3",
+    "4217",
+    "15924",
 ]
 
+# Validate against schema
 for standard in standards:
-	with open("data/schema-" + standard + ".json") as schema_file:
-		schema = json.load(schema_file)
-		with open("data/iso_" + standard + ".json") as json_file:
-			validate(json.load(json_file), schema)
+    with open("data/schema-" + standard + ".json") as schema_file:
+        schema = json.load(schema_file)
+        with open("data/iso_" + standard + ".json") as json_file:
+            validate(json.load(json_file), schema)
+
+# Ensure correct sorting order
+for standard in standards:
+    # Read in the JSON file
+    with open("data/iso_" + standard + ".json") as json_file:
+        iso = json.load(json_file)
+    # Special case for ISO 3166-2
+    if standard == "3166-2":
+        for entry in iso[standard]:
+            for subset in entry["subsets"]:
+                subset["items"].sort(key=lambda item: item["code"])
+    else:
+        sort_key = "alpha_3"
+        if standard in ["3166-3", "15924"]:
+            sort_key = "alpha_4"
+        iso[standard].sort(key=lambda item: item[sort_key])
+    # Write the sorted JSON file
+    with open("data/iso_" + standard + ".json", "w") as json_file:
+        json.dump(iso, json_file, ensure_ascii=False, indent=2, sort_keys=True)
+        # Add a final newline
+        json_file.write("\n")

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-isocodes/iso-codes.git



More information about the Pkg-isocodes-devel mailing list