• tree representation of Python data

    From Dino@21:1/5 to All on Sat Jan 21 10:03:28 2023
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Stefan Ram on Sat Jan 21 17:02:48 2023
    ram@zedat.fu-berlin.de (Stefan Ram) writes:
    object = \

    The new version has some minor modifications:

    object = \
    { 'unit/':
    { 'mocks': None,
    'plugins':
    { 'ast': None,
    'editor': None,
    'editor-autosuggest': None,
    'editor-metadata': None,
    'json-schema-validator': { 'test-documents': None },
    'validate-semantic':
    { '2and3': None, 'bugs': None, 'oas3': None }},
    'standalone': { 'topbar-insert': None }}}

    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

    def display( object ): return display_( object, [] )

    print( display( object ))

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Dino on Sat Jan 21 16:20:43 2023
    Dino <dino@no.spam.ar> writes:
    unit/
    ├── mocks
    ├── plugins
    │   ├── ast
    │   ├── editor
    │   ├── editor-autosuggest
    │   ├── editor-metadata
    │   ├── json-schema-validator
    │   │   └── test-documents
    │   └── validate-semantic
    │   ├── 2and3
    │   ├── bugs
    │   └── oas3
    └── standalone
    └── topbar-insert
    Of course I am aware of indent (json.dumps()) and pprint, and they are

    In my days, "indent" was the name of a beautifier for C source code.

    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.

    Well, it's a nice exercise! But I only made it work for the
    specific example given. I have not tested whether it always
    works.

    object = \
    { 'unit/':
    { 'mocks': None,
    'plugins':
    { 'ast': None,
    'editor': None,
    'editor-autosuggest': None,
    'editor-metadata': None,
    'json-schema-validator':
    { 'test-documents': None },
    'validate-semantic':
    { '2and3': None,
    'bugs': None,
    'oas3': None }},
    'standalone':
    { 'topbar-insert': None }}}

    NoneType = type( None )

    def display( object, last ):
    if isinstance( object, str ):
    string = object
    result = str( string )
    elif isinstance( object, dict ):
    dir = object
    result = ''
    d = len( dir )
    for entry in dir:
    d -= 1
    name = display( entry, last +[ d ])
    indent = ''
    for x in last[1:]:
    indent += '│   ' if x else ' '
    if last:
    elem = '├──' if d else '└──'
    else:
    elem = ''
    indent += elem
    result += '\n' + indent + name
    value = display( dir[ entry ], last +[ d ])
    if value is not None:
    result += value
    elif isinstance( object, NoneType ):
    result = None
    return result

    print( display( object, [] ))

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Stefan Ram on Sat Jan 21 19:41:04 2023
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Weatherby,Gerard@21:1/5 to All on Sat Jan 21 20:32:17 2023
    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$>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dino@21:1/5 to Stefan Ram on Sat Jan 21 17:58:50 2023
    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



    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Passin@21:1/5 to Dino on Sun Jan 22 12:11:20 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Shaozhong SHI@21:1/5 to All on Wed Feb 8 11:39:40 2023
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Shaozhong SHI on Wed Feb 8 13:22:30 2023
    Shaozhong SHI <shishaozhong@gmail.com> writes:
    What is the robust way to use Python to read in an XML and turn it into a >JSON file?

    "The robust way" is vague, so I take the question to be:

    |How does one read in an XML document and turn it into a JSON file?

    . You need to decide how you want to represent the prolog,
    elements, attributes, and nesting of elements in JSON.
    Then you can use xml.dom.minidom to read the XML file,
    go trough it recursively using the DOM functions and write
    out JSON.

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

    The XML dom is usually thought of as a tree:

    |Document Object Model (DOM) is an API that allows for
    |navigation of the entire document as if it were a tree of
    |node objects representing the document's contents.¯¯¯¯

    .

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Passin@21:1/5 to Shaozhong SHI on Wed Feb 8 09:37:19 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)