graphit/graph_io/io_pydata_format.py
def write_pydata(graph, nested=True, sep='.', default=None, allow_none=True, export_all=False, include_root=False):
    """
    Export a graph to a (nested) dictionary
    
    Convert graph representation of the dictionary tree into a dictionary
    using a nested or flattened representation of the dictionary hierarchy.
    
    In a flattened representation, the keys are concatenated using the `sep`
    separator.
    Dictionary keys and values are obtained from the node attributes using
    `key_tag` and `value_tag`. The key_tag is set to
    graph key_tag by default.
    
    Exporting only primary key_tag/value_tag pairs is default
    behaviour. Use the 'export_all' argument to export the full node
    dictionary.
    
    TODO: include ability to export multiple isolated subgraphs
    
    :param graph:          Graph object to export
    :type graph:           :graphit:GraphAxis
    :param nested:         return a nested or flattened dictionary
    :type nested:          :py:bool
    :param sep:            key separator used in flattening the dictionary
    :type sep:             :py:str
    :param default:        value to use when node value was not found using
                           value_tag.
    :type default:         mixed
    :param allow_none:     allow None values in the output
    :type allow_none:      :py:bool
    :param export_all:     Export the full node storage dictionary.
    :type export_all:      :py:bool
    
    :rtype:                :py:dict
    """
    
    # No nodes, return empty dict
    if graph.empty():
        logging.info('Graph is empty: {0}'.format(repr(graph)))
        return {}
    
    # Graph should be of type GraphAxis with a root node nid defined
    if not isinstance(graph, GraphAxis):
        raise TypeError('Unsupported graph type {0}'.format(type(graph)))
    if graph.root is None:
        raise GraphitException('No graph root node defines')
    
    # Build ORM with format specific conversion classes
    pydataorm = GraphORM(inherit=False)
    pydataorm.node_mapping.add(ParseDictionaryType, lambda x: x.get('format') == 'dict')
    pydataorm.node_mapping.add(ParseListType, lambda x: x.get('format') == 'list')
    pydataorm.node_mapping.add(ParseSetType, lambda x: x.get('format') == 'set')
    pydataorm.node_mapping.add(ParseTupleType, lambda x: x.get('format') == 'tuple')
    
    # Set current ORM aside and register new one.
    curr_orm = graph.orm
    graph.orm = pydataorm
    
    # Set current NodeTools aside and register new one
    curr_nt = graph.node_tools
    graph.node_tools = PyDataNodeTools
    
    # Define start node for recursive export
    if len(graph) > 1:
        root = graph.getnodes(resolve_root_node(graph))
    else:
        root = graph.getnodes(list(graph.nodes.keys()))
    
    # If we export the full node dictionary, also export None key/value pairs
    if export_all:
        allow_none = True
    
    # Start recursive parsing
    root_key, data = root.serialize(allow_none=allow_none, export_all=export_all, default=default)
    
    # Include root_key or not
    if include_root and root_key:
        data = {root_key: data}
    
    # Flatten the dictionary if needed
    if not nested:
        data = flatten_nested_dict(data, sep=sep)
    
    # Restore original ORM and NodeTools
    graph.node_tools = curr_nt
    graph.orm = curr_orm
    
    return data

    Export a graph to a (nested) dictionary
    
    Convert graph representation of the dictionary tree into a dictionary
    using a nested or flattened representation of the dictionary hierarchy.
    
    In a flattened representation, the keys are concatenated using the `sep`
    separator.
    Dictionary keys and values are obtained from the node attributes using
    `key_tag` and `value_tag`. The key_tag is set to
    graph key_tag by default.
    
    Exporting only primary key_tag/value_tag pairs is default
    behaviour. Use the 'export_all' argument to export the full node
    dictionary.
    
    TODO: include ability to export multiple isolated subgraphs
    
    :param graph:          Graph object to export
    :type graph:           :graphit:GraphAxis
    :param nested:         return a nested or flattened dictionary
    :type nested:          :py:bool
    :param sep:            key separator used in flattening the dictionary
    :type sep:             :py:str
    :param default:        value to use when node value was not found using
                           value_tag.
    :type default:         mixed
    :param allow_none:     allow None values in the output
    :type allow_none:      :py:bool
    :param export_all:     Export the full node storage dictionary.
    :type export_all:      :py:bool
    
    :rtype:                :py:dict
    
Export a graph to a (nested) dictionary
Convert graph representation of the dictionary tree into a dictionary
using a nested or flattened representation of the dictionary hierarchy.

In a flattened representation, the keys are concatenated using the `sep`
separator.
Dictionary keys and values are obtained from the node attributes using
`key_tag` and `value_tag`. The key_tag is set to
graph key_tag by default.

Exporting only primary key_tag/value_tag pairs is default
behaviour. Use the 'export_all' argument to export the full node
dictionary.

include ability to export multiple isolated subgraphs
Export a graph to a ( nested ) dictionary Convert graph representation of the dictionary tree into a dictionary using a nested or flattened representation of the dictionary hierarchy . In a flattened representation the keys are concatenated using the ` sep ` separator . Dictionary keys and values are obtained from the node attributes using ` key_tag ` and ` value_tag ` . The key_tag is set to graph key_tag by default . Exporting only primary key_tag / value_tag pairs is default behaviour . Use the ' export_all ' argument to export the full node dictionary . include ability to export multiple isolated subgraphs
def write_pydata(graph, nested=True, sep='.', default=None, allow_none=True, export_all=False, include_root=False):
    if graph.empty():
        logging.info('Graph is empty: {0}'.format(repr(graph)))
        return {}
    if not isinstance(graph, GraphAxis):
        raise TypeError('Unsupported graph type {0}'.format(type(graph)))
    if graph.root is None:
        raise GraphitException('No graph root node defines')
    pydataorm = GraphORM(inherit=False)
    pydataorm.node_mapping.add(ParseDictionaryType, lambda x: x.get('format') == 'dict')
    pydataorm.node_mapping.add(ParseListType, lambda x: x.get('format') == 'list')
    pydataorm.node_mapping.add(ParseSetType, lambda x: x.get('format') == 'set')
    pydataorm.node_mapping.add(ParseTupleType, lambda x: x.get('format') == 'tuple')
    curr_orm = graph.orm
    graph.orm = pydataorm
    curr_nt = graph.node_tools
    graph.node_tools = PyDataNodeTools
    if len(graph) > 1:
        root = graph.getnodes(resolve_root_node(graph))
    else:
        root = graph.getnodes(list(graph.nodes.keys()))
    if export_all:
        allow_none = True
    root_key, data = root.serialize(allow_none=allow_none, export_all=export_all, default=default)
    if include_root and root_key:
        data = {root_key: data}
    if not nested:
        data = flatten_nested_dict(data, sep=sep)
    graph.node_tools = curr_nt
    graph.orm = curr_orm
    return data
def write_pydata ( graph , nested = True , sep = '.' , default = None , allow_none = True , export_all = False , include_root = False ) : if graph . empty ( ) : logging . info ( 'Graph is empty: {0}' . format ( repr ( graph ) ) ) return { } if not isinstance ( graph , GraphAxis ) : raise TypeError ( 'Unsupported graph type {0}' . format ( type ( graph ) ) ) if graph . root is None : raise GraphitException ( 'No graph root node defines' ) pydataorm = GraphORM ( inherit = False ) pydataorm . node_mapping . add ( ParseDictionaryType , lambda x : x . get ( 'format' ) == 'dict' ) pydataorm . node_mapping . add ( ParseListType , lambda x : x . get ( 'format' ) == 'list' ) pydataorm . node_mapping . add ( ParseSetType , lambda x : x . get ( 'format' ) == 'set' ) pydataorm . node_mapping . add ( ParseTupleType , lambda x : x . get ( 'format' ) == 'tuple' ) curr_orm = graph . orm graph . orm = pydataorm curr_nt = graph . node_tools graph . node_tools = PyDataNodeTools if len ( graph ) > 1 : root = graph . getnodes ( resolve_root_node ( graph ) ) else : root = graph . getnodes ( list ( graph . nodes . keys ( ) ) ) if export_all : allow_none = True root_key , data = root . serialize ( allow_none = allow_none , export_all = export_all , default = default ) if include_root and root_key : data = { root_key : data } if not nested : data = flatten_nested_dict ( data , sep = sep ) graph . node_tools = curr_nt graph . orm = curr_orm return data
Export a graph to a (nested) dictionary
Convert graph representation of the dictionary tree into a dictionary
using a nested or flattened representation of the dictionary hierarchy.
Export a graph to a ( nested ) dictionary Convert graph representation of the dictionary tree into a dictionary using a nested or flattened representation of the dictionary hierarchy .
"""
    Export a graph to a (nested) dictionary
    
    Convert graph representation of the dictionary tree into a dictionary
    using a nested or flattened representation of the dictionary hierarchy.
    
    In a flattened representation, the keys are concatenated using the `sep`
    separator.
    Dictionary keys and values are obtained from the node attributes using
    `key_tag` and `value_tag`. The key_tag is set to
    graph key_tag by default.
    
    Exporting only primary key_tag/value_tag pairs is default
    behaviour. Use the 'export_all' argument to export the full node
    dictionary.
    
    TODO: include ability to export multiple isolated subgraphs
    
    :param graph:          Graph object to export
    :type graph:           :graphit:GraphAxis
    :param nested:         return a nested or flattened dictionary
    :type nested:          :py:bool
    :param sep:            key separator used in flattening the dictionary
    :type sep:             :py:str
    :param default:        value to use when node value was not found using
                           value_tag.
    :type default:         mixed
    :param allow_none:     allow None values in the output
    :type allow_none:      :py:bool
    :param export_all:     Export the full node storage dictionary.
    :type export_all:      :py:bool
    
    :rtype:                :py:dict
    """ # No nodes, return empty dict # Graph should be of type GraphAxis with a root node nid defined # Build ORM with format specific conversion classes # Set current ORM aside and register new one. # Set current NodeTools aside and register new one # Define start node for recursive export # If we export the full node dictionary, also export None key/value pairs # Start recursive parsing # Include root_key or not # Flatten the dictionary if needed # Restore original ORM and NodeTools
None
:py:dict
graph
Graph object to export
Graph object to export
nested
return a nested or flattened dictionary
return a nested or flattened dictionary
sep
key separator used in flattening the dictionary
key separator used in flattening the dictionary
default
value to use when node value was not found using
value_tag.
value to use when node value was not found using value_tag .
allow_none
allow None values in the output
allow None values in the output
export_all
Export the full node storage dictionary.
Export the full node storage dictionary .
include_root