compact_dict_string.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. def compact_object_string(o, max_line_length=120, indent=0, max_depth=2 ** 32):
  2. if max_depth == 0:
  3. return ' ' * indent + '...'
  4. if isinstance(o, list):
  5. return compact_list_string(o, max_line_length, indent=indent, max_depth=max_depth)
  6. elif isinstance(o, tuple):
  7. return compact_tuple_string(o, max_line_length, indent=indent, max_depth=max_depth)
  8. elif isinstance(o, dict):
  9. return compact_dict_string(o, max_line_length, indent=indent, max_depth=max_depth)
  10. elif isinstance(o, str):
  11. return '"' + o + '"'
  12. else:
  13. return ' ' * indent + str(o)
  14. def compact_list_string(xs: list, max_line_length=120, indent=0, closing=']', opening='[', max_depth=2 ** 32):
  15. # try to fit everything in one line with the default str method
  16. single_line_result = ' ' * indent + str(xs)
  17. if len(single_line_result) <= max_line_length:
  18. return single_line_result
  19. # not extra lines for [ and ]
  20. multi_line_result_right = ' ' * indent + opening
  21. for x in xs:
  22. prefix = ''
  23. multi_line_result_right += prefix + f'{compact_object_string(x, max_line_length=max_line_length, indent=indent + 1 + len(prefix), max_depth=max_depth - 1)}'.strip()
  24. multi_line_result_right += ',\n' + ' ' * indent
  25. multi_line_result_right = multi_line_result_right[:-len(',\n' + ' ' * indent)] + closing
  26. # extra lines for [ and ]
  27. multi_line_result_below = ' ' * indent + opening
  28. for x in xs:
  29. prefix = ''
  30. multi_line_result_below += '\n' + ' ' * (indent + 2)
  31. multi_line_result_below += prefix + f'{compact_object_string(x, max_line_length=max_line_length, indent=indent + 2 + len(prefix), max_depth=max_depth - 1)}'.strip()
  32. multi_line_result_below += ',\n' + ' ' * indent
  33. multi_line_result_below = multi_line_result_below + closing
  34. if len(multi_line_result_right.splitlines()) < len(multi_line_result_below.splitlines()):
  35. return multi_line_result_right
  36. else:
  37. return multi_line_result_below
  38. def compact_tuple_string(xs: tuple, max_line_length=120, indent=0, max_depth=2 ** 32):
  39. return compact_list_string(list(xs), max_line_length, indent, closing=')', opening='(')
  40. def compact_dict_string(d: dict, max_line_length=120, indent=0, max_depth=2 ** 32):
  41. # try to fit everything in one line with the default str method
  42. single_line_result = ' ' * indent + str(d).replace("'", '"')
  43. if len(single_line_result) <= max_line_length:
  44. return single_line_result
  45. # try to put compact value string next to the key strings
  46. multi_line_result_right = ' ' * indent + '{'
  47. for k, v in d.items():
  48. if isinstance(k, str):
  49. prefix = '"' + str(k) + '"' + ': '
  50. else:
  51. prefix = str(k) + ': '
  52. multi_line_result_right += prefix + f'{compact_object_string(v, max_line_length=max_line_length, indent=indent + 1 + len(prefix), max_depth=max_depth - 1)}'.strip()
  53. multi_line_result_right += ',\n' + ' ' * indent
  54. multi_line_result_right = multi_line_result_right[:-len(',\n' + ' ' * indent)] + '}'
  55. # try to put compact value string below key strings
  56. multi_line_result_below = ' ' * indent + '{'
  57. multi_line_result_below += '\n' + ' ' * (indent + 2)
  58. for k, v in d.items():
  59. prefix = '"' + str(k) + '"' + ': '
  60. multi_line_result_below += prefix + f'{compact_object_string(v, max_line_length=max_line_length, indent=indent + 2 + len(prefix), max_depth=max_depth - 1)}'.strip()
  61. multi_line_result_below += ',\n' + ' ' * (indent + 2)
  62. multi_line_result_below = multi_line_result_below[:-2] + '}'
  63. if len(multi_line_result_right.splitlines()) < len(multi_line_result_below.splitlines()):
  64. return multi_line_result_right
  65. else:
  66. return multi_line_result_below
  67. if __name__ == '__main__':
  68. print(compact_dict_string({"body": {"game_state": {"crafting_machines": [{"name": "Basic Crafting Machine 16910", "duration": 10, "type": "CraftingMachine"}], "game_name": "test", "properties": [
  69. {"tier": 1, "name": "Luxuriant", "type": "Property"},
  70. {"tier": 1, "name": "Edible", "type": "Property"},
  71. {"tier": 1, "name": "Appealable", "type": "Property"}], "resources": [
  72. {"properties": [{"tier": 1, "name": "Luxuriant", "type": "Property"}], "name": "Wolverine", "type": "Resource"},
  73. {"properties": [{"tier": 1, "name": "Edible", "type": "Property"}], "name": "Lm", "type": "Resource"},
  74. {"properties": [{"tier": 1, "name": "Appealable", "type": "Property"}], "name": "Skittishness", "type": "Resource"}], "users": [
  75. {"resource_inventory": {}, "session_id": "12843d4e-8cfb-4f30-acaa-d4d9de6cf33f", "username": "testUser", "type": "User"}], "resource_discoveries":
  76. {"testUser": [{"properties": [{"tier": 1, "name": "Luxuriant", "type": "Property"}], "name": "Wolverine", "type": "Resource"},
  77. {"properties": [{"tier": 1, "name": "Edible", "type": "Property"}], "name": "Lm", "type": "Resource"},
  78. {"properties": [{"tier": 1, "name": "Appealable", "type": "Property"}], "name": "Skittishness", "type": "Resource"}]},
  79. "type": "GameState"}}, "http_status_code": 200, "request_token": "774ba52b-31a2-481a-9f12-537495dae993"}, max_line_length=200))