[scripts/pyprint] fix various issues
- reintroduce 'lmin' argument - don't use one-line dict representation - replace list.extend with actual for loop
This commit is contained in:
@@ -10,7 +10,7 @@
|
|||||||
import re
|
import re
|
||||||
|
|
||||||
|
|
||||||
def pyprint(obj, indent=0, sort=None, lmax=16):
|
def pyprint(obj, indent=0, sort=None, lmin=9, lmax=16):
|
||||||
|
|
||||||
if isinstance(obj, str):
|
if isinstance(obj, str):
|
||||||
if obj.startswith("lit:"):
|
if obj.startswith("lit:"):
|
||||||
@@ -46,9 +46,6 @@ def pyprint(obj, indent=0, sort=None, lmax=16):
|
|||||||
if isinstance(obj, dict):
|
if isinstance(obj, dict):
|
||||||
if not obj:
|
if not obj:
|
||||||
return "{}"
|
return "{}"
|
||||||
if len(obj) == 1:
|
|
||||||
key, value = next(iter(obj.items()))
|
|
||||||
return f'''{{"{key}": {pyprint(value, indent, sort)}}}'''
|
|
||||||
|
|
||||||
if sort:
|
if sort:
|
||||||
if callable(sort):
|
if callable(sort):
|
||||||
@@ -62,7 +59,9 @@ def pyprint(obj, indent=0, sort=None, lmax=16):
|
|||||||
obj = {key: obj[key] for key in keys}
|
obj = {key: obj[key] for key in keys}
|
||||||
|
|
||||||
ws = " " * indent
|
ws = " " * indent
|
||||||
key_maxlen = max(kl for kl in map(len, obj) if kl <= lmax)
|
keylen = max(kl for kl in map(len, obj) if kl <= lmax)
|
||||||
|
if keylen < lmin:
|
||||||
|
keylen = lmin
|
||||||
|
|
||||||
lines = ["{"]
|
lines = ["{"]
|
||||||
for key, value in obj.items():
|
for key, value in obj.items():
|
||||||
@@ -71,7 +70,7 @@ def pyprint(obj, indent=0, sort=None, lmax=16):
|
|||||||
else:
|
else:
|
||||||
lines.append(
|
lines.append(
|
||||||
f'''{ws} "{key}"'''
|
f'''{ws} "{key}"'''
|
||||||
f'''{' '*(key_maxlen - len(key))}: '''
|
f'''{' '*(keylen - len(key))}: '''
|
||||||
f'''{pyprint(value, indent+4, sort)},'''
|
f'''{pyprint(value, indent+4, sort)},'''
|
||||||
)
|
)
|
||||||
lines.append(f'''{ws}}}''')
|
lines.append(f'''{ws}}}''')
|
||||||
@@ -84,12 +83,9 @@ def pyprint(obj, indent=0, sort=None, lmax=16):
|
|||||||
return f'''[{pyprint(obj[0], indent, sort)}]'''
|
return f'''[{pyprint(obj[0], indent, sort)}]'''
|
||||||
|
|
||||||
ws = " " * indent
|
ws = " " * indent
|
||||||
|
|
||||||
lines = ["["]
|
lines = ["["]
|
||||||
lines.extend(
|
for value in obj:
|
||||||
f'''{ws} {pyprint(value, indent+4, sort)},'''
|
lines.append(f'''{ws} {pyprint(value, indent+4, sort)},''')
|
||||||
for value in obj
|
|
||||||
)
|
|
||||||
lines.append(f'''{ws}]''')
|
lines.append(f'''{ws}]''')
|
||||||
return "\n".join(lines)
|
return "\n".join(lines)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user