Advertisement
Guest User

Untitled

a guest
Sep 19th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. 'layer1': {
  2. 'layer2': {
  3. 'layer3_1': [ long_list_of_stuff ],
  4. 'layer3_2': 'string'
  5. }
  6. }
  7.  
  8. json.dumps(data_structure, indent=2)
  9.  
  10. {
  11. "layer1": {
  12. "layer2": {
  13. "layer3_1": [
  14. {
  15. "x": 1,
  16. "y": 7
  17. },
  18. {
  19. "x": 0,
  20. "y": 4
  21. },
  22. {
  23. "x": 5,
  24. "y": 3
  25. },
  26. {
  27. "x": 6,
  28. "y": 9
  29. }
  30. ],
  31. "layer3_2": "string"
  32. }
  33. }
  34. }
  35.  
  36. {
  37. "layer1": {
  38. "layer2": {
  39. "layer3_1": [{"x":1,"y":7},{"x":0,"y":4},{"x":5,"y":3},{"x":6,"y":9}],
  40. "layer3_2": "string"
  41. }
  42. }
  43. }
  44.  
  45. import json
  46.  
  47. class NoIndent(object):
  48. def __init__(self, value):
  49. self.value = value
  50. def __repr__(self):
  51. if not isinstance(self.value, (list, tuple)):
  52. return repr(self.value)
  53. else: # assume it's a list or tuple of coordinates stored as dicts
  54. delimiters = '[]' if isinstance(self.value, list) else '()'
  55. pairs = ('{!r}:{}'.format(*component)
  56. for coordinate in self.value
  57. for component in sorted(coordinate.items()))
  58. pairs = ('{{{}, {}}}'.format(*pair)
  59. for pair in zip(*[iter(pairs)]*2))
  60. return delimiters[0] + ', '.join(pairs) + delimiters[1]
  61.  
  62. class MyEncoder(json.JSONEncoder):
  63. def default(self, obj):
  64. return(repr(obj) if isinstance(obj, NoIndent) else
  65. json.JSONEncoder.default(self, obj))
  66.  
  67. data_structure = {
  68. 'layer1': {
  69. 'layer2': {
  70. 'layer3_1': NoIndent([{"x":1,"y":7}, {"x":0,"y":4},
  71. {"x":5,"y":3}, {"x":6,"y":9}]),
  72. 'layer3_2': 'string'
  73. }
  74. }
  75. }
  76.  
  77. print json.dumps(data_structure, cls=MyEncoder, indent=2)
  78.  
  79. s = json.dumps(data_structure, indent=2)
  80. s = re.sub('s*{s*"(.)": (d+),s*"(.)": (d+)s*}(,?)s*', r'{"1":2,"3":4}5', s)
  81.  
  82. class NoIndentList(list):
  83. pass
  84.  
  85. import json
  86. import sys
  87.  
  88. class NoIndent(object):
  89. def __init__(self, value):
  90. self.value = value
  91.  
  92. def default(o, encoder=json.JSONEncoder()):
  93. if isinstance(o, NoIndent):
  94. return json.dumps(o.value)
  95. return encoder.default(o)
  96.  
  97. L = [dict(x=x, y=y) for x in range(1) for y in range(2)]
  98. obj = [NoIndent(L), L]
  99. json.dump(obj, sys.stdout, default=default, indent=4)
  100.  
  101. [
  102. "[{"y": 0, "x": 0}, {"y": 1, "x": 0}]",
  103. [
  104. {
  105. "y": 0,
  106. "x": 0
  107. },
  108. {
  109. "y": 1,
  110. "x": 0
  111. }
  112. ]
  113. ]
  114.  
  115. import sys
  116. import yaml
  117.  
  118. class NoIndentList(list):
  119. pass
  120.  
  121. def noindent_list_presenter(dumper, data):
  122. return dumper.represent_sequence(u'tag:yaml.org,2002:seq', data,
  123. flow_style=True)
  124. yaml.add_representer(NoIndentList, noindent_list_presenter)
  125.  
  126.  
  127. obj = [
  128. [dict(x=x, y=y) for x in range(2) for y in range(1)],
  129. [dict(x=x, y=y) for x in range(1) for y in range(2)],
  130. ]
  131. obj[0] = NoIndentList(obj[0])
  132. yaml.dump(obj, stream=sys.stdout, indent=4)
  133.  
  134. - [{x: 0, y: 0}, {x: 1, y: 0}]
  135. - - {x: 0, y: 0}
  136. - {x: 0, y: 1}
  137.  
  138. def custom_print(data_structure, indent):
  139. for key, value in data_structure.items():
  140. print "n%s%s:" % (' '*indent,str(key)),
  141. if isinstance(value, dict):
  142. custom_print(value, indent+1)
  143. else:
  144. print "%s" % (str(value)),
  145.  
  146. >>> custom_print(data_structure,1)
  147.  
  148. layer1:
  149. layer2:
  150. layer3_2: string
  151. layer3_1: [{'y': 7, 'x': 1}, {'y': 4, 'x': 0}, {'y': 3, 'x': 5}, {'y': 9, 'x': 6}]
  152.  
  153. def default(self, o):
  154. if isinstance(o, NoIndent):
  155. key = uuid.uuid4().hex
  156. self._replacement_map[key] = json.dumps(o.value)
  157. return "@@%s@@" % (key,)
  158. else:
  159. return super(NoIndentEncoder, self).default(o)
  160.  
  161. def encode(self, o):
  162. result = super(NoIndentEncoder, self).encode(o)
  163. for k, v in self._replacement_map.iteritems():
  164. result = result.replace('"@@%s@@"' % (k,), v)
  165. return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement