graphit/graph_io/io_pydata_format.py
def read_pydata(data, graph=None, parser_classes=ORMDEFS_LEVEL1, level=1):
    """
    Parse (hierarchical) python data structures to a graph

    Many data formats are first parsed to a python structure before they are
    converted to a graph using the `read_pydata` function.
    The function supports any object that is an instance of, or behaves as, a
    Python dictionary, list, tuple or set and converts these (nested)
    structures to graph nodes and edges for connectivity. Data is stored in
    nodes using the node and edge 'key_tag' and 'value_tag' attributes in the
    Graph class.

    Data type and format information are also stored as part of the nodes to
    enable reconstruction of the Python data structure on export using the
    `write_pydata` function. Changing type and format on a node or edge
    allows for customized data export.

    Parsing of data structures to nodes and edges is handled by parser classes
    that need to define the methods `deserialize` for reading and `serialize`
    for writing. In `write_pydata` these classes are registered with the ORM
    to fully customize the use of the `serialize` method. In the `read_pydata`
    function the ORM cannot be used because the nodes/edges themselves do not
    yet exist. Instead they are provided as a dictionary through the
    `parser_classes` argument. The dictionary defined the string representation
    of the Python data type as key and parser class as value.

    Parser customization is important as Python data structures can be
    represented as a graph structure in different ways. This is certainly true
    for dictionaries where key/value pairs can be part of the node attributes,
    as separate nodes or as a combination of the two.
    `read_pydata` has quick support for two scenario's using the `level`
    argument:

        * level 0: every dictionary key/value pair is represented as a node
          regardless of its position in the nested data structure
        * level 1: all keys at the same level in the hierarchy that have a
          primitive type value are stored as part of the node attributes.

    If the `graph` is empty, the first node added to the graph is assigned
    as root node. If the `graph` is not empty, new nodes and edges will be
    added to it as subgraph. Edge connections between the two will have to be
    made afterwards.
    
    :param data:            Python (hierarchical) data structure
    :param graph:           GraphAxis object to import dictionary data in
    :type graph:            :graphit:GraphAxis
    :param parser_classes:  parser class definition for different Python data
                            types
    :type parser_classes:   :py:dict
    :param level:           dictionary parsing mode
    :type level:            :py:int
    
    :return:                GraphAxis object
    :rtype:                 :graphit:GraphAxis
    """
    
    # User defined or default GraphAxis object
    if graph is None:
        graph = GraphAxis()
    if not isinstance(graph, GraphAxis):
        raise TypeError('Unsupported graph type {0}'.format(type(graph)))

    # Define the parser classes based on level if not a custom dict.
    if parser_classes in (ORMDEFS_LEVEL0, ORMDEFS_LEVEL1):
        if level == 0:
            parser_classes = ORMDEFS_LEVEL0

    # Define root
    if graph.empty():
        graph.root = graph._nodeid
    
    # Start recursive parsing by calling the `deserialize` method on the parser object
    parser = parser_classes.get(return_instance_type(data), parser_classes['fallback'])
    p = parser()
    p.deserialize(data, graph, parser_classes)
    
    return graph

    Parse (hierarchical) python data structures to a graph

    Many data formats are first parsed to a python structure before they are
    converted to a graph using the `read_pydata` function.
    The function supports any object that is an instance of, or behaves as, a
    Python dictionary, list, tuple or set and converts these (nested)
    structures to graph nodes and edges for connectivity. Data is stored in
    nodes using the node and edge 'key_tag' and 'value_tag' attributes in the
    Graph class.

    Data type and format information are also stored as part of the nodes to
    enable reconstruction of the Python data structure on export using the
    `write_pydata` function. Changing type and format on a node or edge
    allows for customized data export.

    Parsing of data structures to nodes and edges is handled by parser classes
    that need to define the methods `deserialize` for reading and `serialize`
    for writing. In `write_pydata` these classes are registered with the ORM
    to fully customize the use of the `serialize` method. In the `read_pydata`
    function the ORM cannot be used because the nodes/edges themselves do not
    yet exist. Instead they are provided as a dictionary through the
    `parser_classes` argument. The dictionary defined the string representation
    of the Python data type as key and parser class as value.

    Parser customization is important as Python data structures can be
    represented as a graph structure in different ways. This is certainly true
    for dictionaries where key/value pairs can be part of the node attributes,
    as separate nodes or as a combination of the two.
    `read_pydata` has quick support for two scenario's using the `level`
    argument:

        * level 0: every dictionary key/value pair is represented as a node
          regardless of its position in the nested data structure
        * level 1: all keys at the same level in the hierarchy that have a
          primitive type value are stored as part of the node attributes.

    If the `graph` is empty, the first node added to the graph is assigned
    as root node. If the `graph` is not empty, new nodes and edges will be
    added to it as subgraph. Edge connections between the two will have to be
    made afterwards.
    
    :param data:            Python (hierarchical) data structure
    :param graph:           GraphAxis object to import dictionary data in
    :type graph:            :graphit:GraphAxis
    :param parser_classes:  parser class definition for different Python data
                            types
    :type parser_classes:   :py:dict
    :param level:           dictionary parsing mode
    :type level:            :py:int
    
    :return:                GraphAxis object
    :rtype:                 :graphit:GraphAxis
    
Parse (hierarchical) python data structures to a graph
Many data formats are first parsed to a python structure before they are
converted to a graph using the `read_pydata` function.
The function supports any object that is an instance of, or behaves as, a
Python dictionary, list, tuple or set and converts these (nested)
structures to graph nodes and edges for connectivity. Data is stored in
nodes using the node and edge 'key_tag' and 'value_tag' attributes in the
Graph class.

Data type and format information are also stored as part of the nodes to
enable reconstruction of the Python data structure on export using the
`write_pydata` function. Changing type and format on a node or edge
allows for customized data export.

Parsing of data structures to nodes and edges is handled by parser classes
that need to define the methods `deserialize` for reading and `serialize`
for writing. In `write_pydata` these classes are registered with the ORM
to fully customize the use of the `serialize` method. In the `read_pydata`
function the ORM cannot be used because the nodes/edges themselves do not
yet exist. Instead they are provided as a dictionary through the
`parser_classes` argument. The dictionary defined the string representation
of the Python data type as key and parser class as value.

Parser customization is important as Python data structures can be
represented as a graph structure in different ways. This is certainly true
for dictionaries where key/value pairs can be part of the node attributes,
as separate nodes or as a combination of the two.
`read_pydata` has quick support for two scenario's using the `level`
argument.

level 0: every dictionary key/value pair is represented as a node
regardless of its position in the nested data structure
level 1: all keys at the same level in the hierarchy that have a
primitive type value are stored as part of the node attributes.

If the `graph` is empty, the first node added to the graph is assigned
as root node. If the `graph` is not empty, new nodes and edges will be
added to it as subgraph. Edge connections between the two will have to be
made afterwards.
Parse ( hierarchical ) python data structures to a graph Many data formats are first parsed to a python structure before they are converted to a graph using the ` read_pydata ` function . The function supports any object that is an instance of or behaves as a Python dictionary list tuple or set and converts these ( nested ) structures to graph nodes and edges for connectivity . Data is stored in nodes using the node and edge ' key_tag ' and ' value_tag ' attributes in the Graph class . Data type and format information are also stored as part of the nodes to enable reconstruction of the Python data structure on export using the ` write_pydata ` function . Changing type and format on a node or edge allows for customized data export . Parsing of data structures to nodes and edges is handled by parser classes that need to define the methods ` deserialize ` for reading and ` serialize ` for writing . In ` write_pydata ` these classes are registered with the ORM to fully customize the use of the ` serialize ` method . In the ` read_pydata ` function the ORM cannot be used because the nodes / edges themselves do not yet exist . Instead they are provided as a dictionary through the ` parser_classes ` argument . The dictionary defined the string representation of the Python data type as key and parser class as value . Parser customization is important as Python data structures can be represented as a graph structure in different ways . This is certainly true for dictionaries where key / value pairs can be part of the node attributes as separate nodes or as a combination of the two . ` read_pydata ` has quick support for two scenario ' s using the ` level ` argument . level 0 : every dictionary key / value pair is represented as a node regardless of its position in the nested data structure level 1 : all keys at the same level in the hierarchy that have a primitive type value are stored as part of the node attributes . If the ` graph ` is empty the first node added to the graph is assigned as root node . If the ` graph ` is not empty new nodes and edges will be added to it as subgraph . Edge connections between the two will have to be made afterwards .
def read_pydata(data, graph=None, parser_classes=ORMDEFS_LEVEL1, level=1):
    if graph is None:
        graph = GraphAxis()
    if not isinstance(graph, GraphAxis):
        raise TypeError('Unsupported graph type {0}'.format(type(graph)))
    if parser_classes in (ORMDEFS_LEVEL0, ORMDEFS_LEVEL1):
        if level == 0:
            parser_classes = ORMDEFS_LEVEL0
    if graph.empty():
        graph.root = graph._nodeid
    parser = parser_classes.get(return_instance_type(data), parser_classes['fallback'])
    p = parser()
    p.deserialize(data, graph, parser_classes)
    return graph
def read_pydata ( data , graph = None , parser_classes = ORMDEFS_LEVEL1 , level = 1 ) : if graph is None : graph = GraphAxis ( ) if not isinstance ( graph , GraphAxis ) : raise TypeError ( 'Unsupported graph type {0}' . format ( type ( graph ) ) ) if parser_classes in ( ORMDEFS_LEVEL0 , ORMDEFS_LEVEL1 ) : if level == 0 : parser_classes = ORMDEFS_LEVEL0 if graph . empty ( ) : graph . root = graph . _nodeid parser = parser_classes . get ( return_instance_type ( data ) , parser_classes [ 'fallback' ] ) p = parser ( ) p . deserialize ( data , graph , parser_classes ) return graph
Parse (hierarchical) python data structures to a graph
Many data formats are first parsed to a python structure before they are
converted to a graph using the `read_pydata` function.
Parse ( hierarchical ) python data structures to a graph Many data formats are first parsed to a python structure before they are converted to a graph using the ` read_pydata ` function .
"""
    Parse (hierarchical) python data structures to a graph

    Many data formats are first parsed to a python structure before they are
    converted to a graph using the `read_pydata` function.
    The function supports any object that is an instance of, or behaves as, a
    Python dictionary, list, tuple or set and converts these (nested)
    structures to graph nodes and edges for connectivity. Data is stored in
    nodes using the node and edge 'key_tag' and 'value_tag' attributes in the
    Graph class.

    Data type and format information are also stored as part of the nodes to
    enable reconstruction of the Python data structure on export using the
    `write_pydata` function. Changing type and format on a node or edge
    allows for customized data export.

    Parsing of data structures to nodes and edges is handled by parser classes
    that need to define the methods `deserialize` for reading and `serialize`
    for writing. In `write_pydata` these classes are registered with the ORM
    to fully customize the use of the `serialize` method. In the `read_pydata`
    function the ORM cannot be used because the nodes/edges themselves do not
    yet exist. Instead they are provided as a dictionary through the
    `parser_classes` argument. The dictionary defined the string representation
    of the Python data type as key and parser class as value.

    Parser customization is important as Python data structures can be
    represented as a graph structure in different ways. This is certainly true
    for dictionaries where key/value pairs can be part of the node attributes,
    as separate nodes or as a combination of the two.
    `read_pydata` has quick support for two scenario's using the `level`
    argument:

        * level 0: every dictionary key/value pair is represented as a node
          regardless of its position in the nested data structure
        * level 1: all keys at the same level in the hierarchy that have a
          primitive type value are stored as part of the node attributes.

    If the `graph` is empty, the first node added to the graph is assigned
    as root node. If the `graph` is not empty, new nodes and edges will be
    added to it as subgraph. Edge connections between the two will have to be
    made afterwards.
    
    :param data:            Python (hierarchical) data structure
    :param graph:           GraphAxis object to import dictionary data in
    :type graph:            :graphit:GraphAxis
    :param parser_classes:  parser class definition for different Python data
                            types
    :type parser_classes:   :py:dict
    :param level:           dictionary parsing mode
    :type level:            :py:int
    
    :return:                GraphAxis object
    :rtype:                 :graphit:GraphAxis
    """ # User defined or default GraphAxis object # Define the parser classes based on level if not a custom dict. # Define root # Start recursive parsing by calling the `deserialize` method on the parser object
None
:graphit:GraphAxis
data
Python (hierarchical) data structure
Python ( hierarchical ) data structure
graph
GraphAxis object to import dictionary data in
GraphAxis object to import dictionary data in
parser_classes
parser class definition for different Python data
types
parser class definition for different Python data types
level
dictionary parsing mode
dictionary parsing mode