Skip to content

Commit 5481418

Browse files
committed
Fix normalize_json.py to format number arrays with multiple numbers per line
- Updated format_json_value to format number arrays with multiple numbers per line - Numbers wrap at printWidth (150) to match Prettier style - Matches the formatting style in book-sets.json
1 parent 7ed1046 commit 5481418

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

data/book-sets.json

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
338,
2525
345,
2626
374,
27-
2827
392,
2928
605,
3029
643,
@@ -629,6 +628,7 @@
629628
1768,
630629
1798,
631630
1920,
631+
1925,
632632
1926,
633633
1929,
634634
1957,
@@ -657,11 +657,23 @@
657657
2888,
658658
2894,
659659
2942,
660+
3100,
660661
3110,
662+
3133,
663+
3164,
661664
3190,
665+
3197,
666+
3228,
667+
3291,
668+
3320,
669+
3351,
670+
3380,
662671
3381,
672+
3413,
663673
3424,
664674
3432,
675+
3444,
676+
3471,
665677
3512,
666678
3522,
667679
3602,
@@ -2130,7 +2142,6 @@
21302142
1921,
21312143
1922,
21322144
1923,
2133-
1925,
21342145
1927,
21352146
1928,
21362147
1930,
@@ -3018,7 +3029,6 @@
30183029
3097,
30193030
3098,
30203031
3099,
3021-
3100,
30223032
3101,
30233033
3102,
30243034
3105,
@@ -3042,7 +3052,6 @@
30423052
3130,
30433053
3131,
30443054
3132,
3045-
3133,
30463055
3134,
30473056
3136,
30483057
3137,
@@ -3066,7 +3075,6 @@
30663075
3161,
30673076
3162,
30683077
3163,
3069-
3164,
30703078
3165,
30713079
3168,
30723080
3169,
@@ -3090,7 +3098,6 @@
30903098
3194,
30913099
3195,
30923100
3196,
3093-
3197,
30943101
3200,
30953102
3201,
30963103
3202,
@@ -3114,7 +3121,6 @@
31143121
3225,
31153122
3226,
31163123
3227,
3117-
3228,
31183124
3229,
31193125
3232,
31203126
3233,
@@ -3162,7 +3168,6 @@
31623168
3288,
31633169
3289,
31643170
3290,
3165-
3291,
31663171
3292,
31673172
3295,
31683173
3296,
@@ -3186,7 +3191,6 @@
31863191
3317,
31873192
3318,
31883193
3319,
3189-
3320,
31903194
3321,
31913195
3324,
31923196
3325,
@@ -3210,7 +3214,6 @@
32103214
3348,
32113215
3349,
32123216
3350,
3213-
3351,
32143217
3352,
32153218
3354,
32163219
3355,
@@ -3234,7 +3237,6 @@
32343237
3377,
32353238
3378,
32363239
3379,
3237-
3380,
32383240
3382,
32393241
3386,
32403242
3387,
@@ -3258,7 +3260,6 @@
32583260
3410,
32593261
3411,
32603262
3412,
3261-
3413,
32623263
3414,
32633264
3417,
32643265
3418,
@@ -3282,7 +3283,6 @@
32823283
3441,
32833284
3442,
32843285
3443,
3285-
3444,
32863286
3445,
32873287
3446,
32883288
3447,
@@ -3306,7 +3306,6 @@
33063306
3468,
33073307
3469,
33083308
3470,
3309-
3471,
33103309
3472,
33113310
3473,
33123311
3474,

scripts/normalize_json.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from collections import OrderedDict
1515

1616

17-
def format_json_value(value, indent_level=0):
17+
def format_json_value(value, indent_level=0, print_width=150):
1818
"""Format a JSON value with custom formatting following Prettier style."""
1919
indent = " " * indent_level
2020
next_indent = " " * (indent_level + 1)
@@ -25,19 +25,43 @@ def format_json_value(value, indent_level=0):
2525
items = []
2626
for k, v in sorted(value.items()):
2727
formatted_key = json.dumps(k, ensure_ascii=False)
28-
formatted_value = format_json_value(v, indent_level + 1)
28+
formatted_value = format_json_value(v, indent_level + 1, print_width)
2929
items.append(f"{next_indent}{formatted_key}: {formatted_value}")
3030
return "{\n" + ",\n".join(items) + "\n" + indent + "}"
3131
elif isinstance(value, list):
3232
if not value:
3333
return "[]"
34-
# Check if it's a list of numbers (for problems arrays) - format one per line
34+
# Check if it's a list of numbers (for problems arrays) - format multiple per line
3535
if value and isinstance(value[0], (int, float)):
36-
items = [f"{next_indent}{json.dumps(item, ensure_ascii=False)}" for item in value]
37-
return "[\n" + ",\n".join(items) + "\n" + indent + "]"
36+
# Calculate available width (print_width minus indentation and brackets)
37+
available_width = print_width - len(next_indent) - 2
38+
lines = []
39+
current_line = []
40+
current_length = 0
41+
42+
for i, item in enumerate(value):
43+
item_str = str(item)
44+
# Add comma and space length (2) if not first item on line
45+
item_length = len(item_str) + (2 if current_line else 0)
46+
47+
if current_length + item_length > available_width and current_line:
48+
# Start a new line
49+
lines.append(", ".join(str(x) for x in current_line))
50+
current_line = [item]
51+
current_length = len(item_str)
52+
else:
53+
current_line.append(item)
54+
current_length += item_length
55+
56+
if current_line:
57+
lines.append(", ".join(str(x) for x in current_line))
58+
59+
# Format with proper indentation
60+
formatted_lines = [f"{next_indent}{line}" for line in lines]
61+
return "[\n" + ",\n".join(formatted_lines) + "\n" + indent + "]"
3862
else:
3963
# Format other arrays - check if they fit on one line
40-
formatted_items = [format_json_value(item, indent_level + 1) for item in value]
64+
formatted_items = [format_json_value(item, indent_level + 1, print_width) for item in value]
4165
total_length = sum(len(str(item)) for item in formatted_items)
4266
if total_length < 100 and len(value) <= 5:
4367
# Short arrays on one line
@@ -74,7 +98,7 @@ def sort_json_by_numeric_keys(input_file, output_file):
7498
items = []
7599
for key, value in sorted_data.items():
76100
formatted_key = json.dumps(key, ensure_ascii=False)
77-
formatted_value = format_json_value(value, 1)
101+
formatted_value = format_json_value(value, 1, print_width=150)
78102
items.append(f' {formatted_key}: {formatted_value}')
79103
lines.append(",\n".join(items))
80104
lines.append("}")

0 commit comments

Comments
 (0)