Mailing List Archive

tree representation of Python data
I have a question that is a bit of a shot in the dark. I have this nice
bash utility installed:

$ tree -d unit/
unit/
??? mocks
??? plugins
?   ??? ast
?   ??? editor
?   ??? editor-autosuggest
?   ??? editor-metadata
?   ??? json-schema-validator
?   ?   ??? test-documents
?   ??? validate-semantic
?   ??? 2and3
?   ??? bugs
?   ??? oas3
??? standalone
??? topbar-insert

I just thought that it would be great if there was a Python utility that
visualized a similar graph for nested data structures.
Of course I am aware of indent (json.dumps()) and pprint, and they are
OK options for my need. It's just that the compact, improved
visualization would be nice to have. Not so nice that I would go out of
my way to build, but nice enough to use an exising package.

Thanks

Dino
--
https://mail.python.org/mailman/listinfo/python-list
Re: tree representation of Python data [ In reply to ]
https://docs.python.org/3/library/pprint.html

From: Python-list <python-list-bounces+gweatherby=uchc.edu@python.org> on behalf of Dino <dino@no.spam.ar>
Date: Saturday, January 21, 2023 at 11:42 AM
To: python-list@python.org <python-list@python.org>
Subject: tree representation of Python data
*** Attention: This is an external email. Use caution responding, opening attachments or clicking on links. ***

I have a question that is a bit of a shot in the dark. I have this nice
bash utility installed:

$ tree -d unit/
unit/
??? mocks
??? plugins
? ??? ast
? ??? editor
? ??? editor-autosuggest
? ??? editor-metadata
? ??? json-schema-validator
? ? ??? test-documents
? ??? validate-semantic
? ??? 2and3
? ??? bugs
? ??? oas3
??? standalone
??? topbar-insert

I just thought that it would be great if there was a Python utility that
visualized a similar graph for nested data structures.
Of course I am aware of indent (json.dumps()) and pprint, and they are
OK options for my need. It's just that the compact, improved
visualization would be nice to have. Not so nice that I would go out of
my way to build, but nice enough to use an exising package.

Thanks

Dino
--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!iGkQhjN2DTLNPZj7JaAhVrakd6oiQrV3IUV08E2ayIK1hWH2AaJ4OQ_uEobpiLQuWde2974mF41mvsnO$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!iGkQhjN2DTLNPZj7JaAhVrakd6oiQrV3IUV08E2ayIK1hWH2AaJ4OQ_uEobpiLQuWde2974mF41mvsnO$>
--
https://mail.python.org/mailman/listinfo/python-list
Re: tree representation of Python data [ In reply to ]
you rock. Thank you, Stefan.

Dino

On 1/21/2023 2:41 PM, Stefan Ram wrote:
> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>> def display_( object, last ):
>> directory = object; result = ''; count = len( directory )
>> for entry in directory:
>> count -= 1; name = entry; indent = ''
>> for c in last[ 1: ]: indent += '?   ' if c else ' '
>> indent += '???' if count else '???' if last else ''
>> result += '\n' + indent +( ' ' if indent else '' )+ name
>> if directory[ entry ]:
>> result += display_( directory[ entry ], last +[ count ])
>> return result
>
> This ultimate version has some variable names made more speaking:
>
> def display_( directory, container_counts ):
> result = ''; count = len( directory )
> for name in directory:
> count -= 1; indent = ''
> for container_count in container_counts[ 1: ]:
> indent += '?   ' if container_count else ' '
> indent += '???' if count else '???' if container_counts else ''
> result += '\n' + indent +( ' ' if indent else '' )+ name
> if directory[ name ]:
> result += display_\
> ( directory[ name ], container_counts +[ count ])
> return result
>
>

--
https://mail.python.org/mailman/listinfo/python-list
Re: tree representation of Python data [ In reply to ]
On 1/21/2023 10:03 AM, Dino wrote:
>
> I have a question that is a bit of a shot in the dark. I have this nice
> bash utility installed:
>
> $ tree -d unit/
> unit/
> ??? mocks
> ??? plugins
> ?   ??? ast
> ?   ??? editor
> ?   ??? editor-autosuggest
> ?   ??? editor-metadata
> ?   ??? json-schema-validator
> ?   ?   ??? test-documents
> ?   ??? validate-semantic
> ?       ??? 2and3
> ?       ??? bugs
> ?       ??? oas3
> ??? standalone
>     ??? topbar-insert
>
> I just thought that it would be great if there was a Python utility that
> visualized a similar graph for nested data structures.
> Of course I am aware of indent (json.dumps()) and pprint, and they are
> OK options for my need. It's just that the compact, improved
> visualization would be nice to have. Not so nice that I would go out of
> my way to build, but nice enough to use an exising package.

It's not clear to me whether you want to display JSON structures or
Python objects. However, Python dictionaries are so similar to JSON
structures that a dictionary example should be pretty close.

This can actually be a tricky problem because for complicated nesting,
it's not that easy to work out how to display the elements. The
examples posted in this thread so far have been somewhat obscured by the
use of leader lines, etc, so that it's a little hard to discern the
simple core of the algorithm.

Below I include code that displays each leaf element on its own indented
line, can only display keys and simple leaf values, and does not
construct leader lines so as to keep the code easy to read. In this
form it can only display Python dictionaries but could be modified for
JSON without too much work.

"""Display nested objects as an indented list."""
INDENT = ' ' * 3
DICT = {'a': {'aa':'AA', 'ab':'AB'},
'b': {'ba': {'baa': 'BAA', 'bab': 'BAB'},
'bb':'BB'},
'c': ['CA', 'CB'],
'd': 'D' }
SIMPLE_TYPES = (int, float, str, bytes)

def unroll(obj, indent = ''):
if isinstance(obj, dict):
unroll_dict(obj, indent)
else:
"""unroll other kinds of objects (not implemented)."""
print(indent, 'Can only unroll nested dictionaries')

def unroll_dict(dct, indent = ''):
"""Unroll a dictionary whose values can be either simple or
nested.
"""
for k, v in dct.items():
g.es(indent, k)
new_indent = indent + INDENT
if type(v) in SIMPLE_TYPES:
print(new_indent, v)
else:
unroll(v, new_indent)
# print(f'{new_indent}{v}')

unroll(DICT)

Output:
a
aa
AA
ab
AB
b
ba
baa
BAA
bab
BAB
bb
BB
c
Can only unroll nested dictionaries
d
D

If you change the last else block, you can make use of the object's
internal representation (not for JSON, of course). Change the block to
read:

# unroll(v, new_indent)
print(f'{new_indent}{v}')

This change gives the following output:

a
{'aa': 'AA', 'ab': 'AB'}
b
{'ba': {'baa': 'BAA', 'bab': 'BAB'}, 'bb': 'BB'}
c
['CA', 'CB']
d
D


In practice, I would collect the text fragments into a list instead of
printing them, then print the joined list at the end.
--
https://mail.python.org/mailman/listinfo/python-list
Re: tree representation of Python data [ In reply to ]
What is the robust way to use Python to read in an XML and turn it into a
JSON file?

JSON dictionary is actually a tree. It is much easier to manage the
tree-structured data.

Regards,

David
--
https://mail.python.org/mailman/listinfo/python-list
Re: tree representation of Python data [ In reply to ]
On 2/8/2023 6:39 AM, Shaozhong SHI wrote:
> What is the robust way to use Python to read in an XML and turn it into
> a JSON file?
>
> JSON dictionary is actually a tree.  It is much easier to manage the
> tree-structured data.

XML and JSON are both for interchanging data. What are you trying to
accomplish? XML elements form a tree, XML attributes can be thought of
as a dictionary. JSON can contain both lists and associative arrays
(dictionaries). Personally, I would not say it's easier to "manage"
tree-structured data than dictionaries, but either way it sounds like
you want to send or receive data, rather than "managing" data in program
data structures.

So why do you want to convert data in XML form to JSON? Once you have
XML ingested into some Python data structure, you can use the json
library to output JSON- see

https://docs.python.org/3/library/json.html

There is a pip-installable program (which I have never used), xmltodict,
that stores an XML file as a Python dictionary. You then can write it
to JSON as above. I don't know how general this program is - XML is not
isomorphic to JSON, but maybe your XML sources never use anything
besides elements and attributes, and don't use namespaces. Then it's
pretty easy.

--
https://mail.python.org/mailman/listinfo/python-list