From 804c82a36b2c1af6a54ffee95b70ef1ae162c89c Mon Sep 17 00:00:00 2001 From: Carl Pearson Date: Mon, 18 Dec 2017 12:07:43 -0600 Subject: [PATCH 1/4] Remove extra file pygraphml_parser --- pygraphml/graphml_parser | 149 --------------------------------------- 1 file changed, 149 deletions(-) delete mode 100644 pygraphml/graphml_parser diff --git a/pygraphml/graphml_parser b/pygraphml/graphml_parser deleted file mode 100644 index f30d723..0000000 --- a/pygraphml/graphml_parser +++ /dev/null @@ -1,149 +0,0 @@ -# -*- coding: utf-8 -*- - -from __future__ import unicode_literals -from __future__ import division -from __future__ import absolute_import -from __future__ import print_function - - -from xml.dom import minidom - -from . import Graph -from . import Node -from . import Edge - -class GraphMLParser: - """ - """ - - def __init__(self): - """ - """ - - def write(self, graph, fname): - """ - """ - - doc = minidom.Document() - - root = doc.createElement('graphml') - doc.appendChild(root) - - # Add attributs - for a in graph.get_attributs(): - attr_node = doc.createElement('key') - attr_node.setAttribute('id', a.name) - attr_node.setAttribute('attr.name', a.name) - attr_node.setAttribute('attr.type', a.type) - root.appendChild(attr_node) - - graph_node = doc.createElement('graph') - graph_node.setAttribute('id', graph.name) - if graph.directed: - graph_node.setAttribute('edgedefault', 'directed') - else: - graph_node.setAttribute('edgedefault', 'undirected') - root.appendChild(graph_node) - - # Add nodes - for n in graph.nodes(): - - node = doc.createElement('node') - node.setAttribute('id', n['label']) - for a in n.attributes(): - if a != 'label': - data = doc.createElement('data') - data.setAttribute('key', a) - data.appendChild(doc.createTextNode(str(n[a]))) - node.appendChild(data) - graph_node.appendChild(node) - - # Add edges - for e in graph.edges(): - - edge = doc.createElement('edge') - edge.setAttribute('source', e.node1['label']) - edge.setAttribute('target', e.node2['label']) - if e.directed() != graph.directed: - edge.setAttribute('directed', 'true' if e.directed() else 'false') - for a in e.attributes(): - if e != 'label': - data = doc.createElement('data') - data.setAttribute('key', a) - data.appendChild(doc.createTextNode(e[a])) - edge.appendChild(data) - graph_node.appendChild(edge) - - f = open(fname, 'w') - f.write(doc.toprettyxml(indent = ' ')) - - @staticmethod - def parse_dom(dom): - """Parse dom into a Graph - - :param dom: dom as returned by minidom.parse or minidom.parseString - :return: A Graph representation - """ - root = dom.getElementsByTagName("graphml")[0] - graph = root.getElementsByTagName("graph")[0] - name = graph.getAttribute('id') - - g = Graph(name) - - # # Get attributes - # attributes = [] - # for attr in root.getElementsByTagName("key"): - # attributes.append(attr) - - # Get nodes - for node in graph.getElementsByTagName("node"): - n = g.add_node(id=node.getAttribute('id')) - - for attr in node.getElementsByTagName("data"): - if attr.firstChild: - n[attr.getAttribute("key")] = attr.firstChild.data - else: - n[attr.getAttribute("key")] = "" - - # Get edges - for edge in graph.getElementsByTagName("edge"): - source = edge.getAttribute('source') - dest = edge.getAttribute('target') - - # source/target attributes refer to IDs: http://graphml.graphdrawing.org/xmlns/1.1/graphml-structure.xsd - e = g.add_edge_by_id(source, dest) - - for attr in edge.getElementsByTagName("data"): - if attr.firstChild: - e[attr.getAttribute("key")] = attr.firstChild.data - else: - e[attr.getAttribute("key")] = "" - - return g - - def parse(self, fname): - """Parse a file into a Graph - - :param fname: Filename - :return: Graph - """ - with open(fname, 'r') as f: - dom = minidom.parse(f) - return self.parse_dom(dom) - - def parse_string(self, string): - """Parse a string into a Graph - - :param string: String that is to be passed into Grapg - :return: Graph - """ - dom = minidom.parseString(string) - return self.parse_dom(dom) - - -if __name__ == '__main__': - - parser = GraphMLParser() - g = parser.parse('test.graphml') - - g.show(True) From 3d4de2a746fbf65b269e83c344d60817b7860f81 Mon Sep 17 00:00:00 2001 From: Carl Pearson Date: Mon, 18 Dec 2017 12:08:31 -0600 Subject: [PATCH 2/4] Change node list to node set to improve performance for large graphs. Fix get_attributs() -> get_attributes() --- pygraphml/graph.py | 14 +++++++------- pygraphml/graphml_parser.py | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pygraphml/graph.py b/pygraphml/graph.py index d900be0..1a71297 100644 --- a/pygraphml/graph.py +++ b/pygraphml/graph.py @@ -23,8 +23,8 @@ def __init__(self, name=""): self.name = name - self._nodes = [] - self._edges = [] + self._nodes = set() + self._edges = set() self._root = None self.directed = True @@ -124,7 +124,7 @@ def add_node(self, label="", id=None): n = Node(id) n['label'] = label - self._nodes.append(n) + self._nodes.add(n) return n @@ -133,12 +133,12 @@ def add_edge(self, n1, n2, directed=False): """ if n1 not in self._nodes: - raise Test("fff") + raise Test("n1 has not been added") if n2 not in self._nodes: - raise Test("fff") + raise Test("n2 has not been added") e = Edge(n1, n2, directed) - self._edges.append(e) + self._edges.add(e) return e @@ -192,7 +192,7 @@ def set_root_by_attribute(self, value, attribute='label'): self.set_root(n) return n - def get_attributs(self): + def get_attributes(self): """ """ diff --git a/pygraphml/graphml_parser.py b/pygraphml/graphml_parser.py index 91f3d8b..bc9ecbb 100644 --- a/pygraphml/graphml_parser.py +++ b/pygraphml/graphml_parser.py @@ -30,7 +30,7 @@ def write(self, graph, fname=None): doc.appendChild(root) # Add attributs - for a in graph.get_attributs(): + for a in graph.get_attributes(): attr_node = doc.createElement('key') attr_node.setAttribute('id', a.name) attr_node.setAttribute('attr.name', a.name) From 0449c015088a7c6dc965ab4f250bde799245a216 Mon Sep 17 00:00:00 2001 From: Carl Pearson Date: Mon, 18 Dec 2017 16:58:25 -0600 Subject: [PATCH 3/4] . --- pygraphml/attribute.py | 3 +-- pygraphml/edge.py | 2 +- pygraphml/graph.py | 2 ++ pygraphml/graphml_parser.py | 1 + pygraphml/item.py | 8 ++++---- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/pygraphml/attribute.py b/pygraphml/attribute.py index ae4bec9..0dd79cd 100644 --- a/pygraphml/attribute.py +++ b/pygraphml/attribute.py @@ -24,5 +24,4 @@ def __str__(self): s = "" s += "%s : %s" % (self.name, str(self.value)) - return s - + return s \ No newline at end of file diff --git a/pygraphml/edge.py b/pygraphml/edge.py index b8f4f8f..2c8a5d9 100644 --- a/pygraphml/edge.py +++ b/pygraphml/edge.py @@ -26,6 +26,7 @@ def __init__(self, node1, node2, directed = False): self._directed = directed + def node(self, node): """ Return the other node @@ -62,4 +63,3 @@ def set_directed(self, dir): """ self._directed = dir - diff --git a/pygraphml/graph.py b/pygraphml/graph.py index 1a71297..f321903 100644 --- a/pygraphml/graph.py +++ b/pygraphml/graph.py @@ -133,8 +133,10 @@ def add_edge(self, n1, n2, directed=False): """ if n1 not in self._nodes: + print(n1) raise Test("n1 has not been added") if n2 not in self._nodes: + print(n2) raise Test("n2 has not been added") e = Edge(n1, n2, directed) diff --git a/pygraphml/graphml_parser.py b/pygraphml/graphml_parser.py index bc9ecbb..33e9298 100644 --- a/pygraphml/graphml_parser.py +++ b/pygraphml/graphml_parser.py @@ -35,6 +35,7 @@ def write(self, graph, fname=None): attr_node.setAttribute('id', a.name) attr_node.setAttribute('attr.name', a.name) attr_node.setAttribute('attr.type', a.type) + attr_node.setAttribute('for', a.forStr) root.appendChild(attr_node) graph_node = doc.createElement('graph') diff --git a/pygraphml/item.py b/pygraphml/item.py index 387fcd1..2d96ac4 100644 --- a/pygraphml/item.py +++ b/pygraphml/item.py @@ -37,17 +37,17 @@ def __str__(self): return s - def __setitem__(self, name, value): + def __getitem__(self, name): """ """ - self.attr[name] = Attribute(name, value) + return self.attr[name].value - def __getitem__(self, name): + def set_attribute(self, name, value, type): """ """ - return self.attr[name].value + self.attr[name] = Attribute(name, value, type) def attributes(self): """ From ed878c8788c93f84dbc269faf0d2b2b64fbe84f3 Mon Sep 17 00:00:00 2001 From: Carl Pearson Date: Mon, 18 Dec 2017 17:53:07 -0600 Subject: [PATCH 4/4] . --- pygraphml/__init__.py | 2 +- pygraphml/attribute.py | 29 +++++++++++++++++++++++++++-- pygraphml/edge.py | 11 ++++++++--- pygraphml/graphml_parser.py | 10 +++++----- pygraphml/item.py | 6 +++--- pygraphml/node.py | 7 ++++++- 6 files changed, 50 insertions(+), 15 deletions(-) diff --git a/pygraphml/__init__.py b/pygraphml/__init__.py index 95b5cce..ec29d50 100644 --- a/pygraphml/__init__.py +++ b/pygraphml/__init__.py @@ -5,7 +5,7 @@ from __future__ import absolute_import from __future__ import print_function -from .attribute import Attribute +from .attribute import Attribute, EdgeAttribute, NodeAttribute from .item import Item from .point import Point from .node import Node diff --git a/pygraphml/attribute.py b/pygraphml/attribute.py index 0dd79cd..d6c6c9b 100644 --- a/pygraphml/attribute.py +++ b/pygraphml/attribute.py @@ -10,7 +10,7 @@ class Attribute: """ """ - def __init__(self, name, value, type = "string"): + def __init__(self, name, value, type="string"): """ """ @@ -24,4 +24,29 @@ def __str__(self): s = "" s += "%s : %s" % (self.name, str(self.value)) - return s \ No newline at end of file + return s + + def for_str(self): + return NotImplemented + + +class EdgeAttribute(Attribute): + """ + """ + + def __init__(self, name, value, type, forStr): + Attribute.__init__(self, name, value, type) + + def for_str(self): + return "edge" + + +class NodeAttribute(Attribute): + """ + """ + + def __init__(self, name, value, type, forStr): + Attribute.__init__(self, name, value, type) + + def for_str(self): + return "node" diff --git a/pygraphml/edge.py b/pygraphml/edge.py index 2c8a5d9..0ce714e 100644 --- a/pygraphml/edge.py +++ b/pygraphml/edge.py @@ -7,12 +7,14 @@ from . import Item +from . import EdgeAttribute + class Edge(Item): """ """ - def __init__(self, node1, node2, directed = False): + def __init__(self, node1, node2, directed=False): """ """ @@ -26,7 +28,6 @@ def __init__(self, node1, node2, directed = False): self._directed = directed - def node(self, node): """ Return the other node @@ -51,7 +52,6 @@ def child(self): return self.node2 - def directed(self): """ """ @@ -63,3 +63,8 @@ def set_directed(self, dir): """ self._directed = dir + + def set_attribute(self, name, value): + """ + """ + self.attr[name] = EdgeAttribute(name, value, type) diff --git a/pygraphml/graphml_parser.py b/pygraphml/graphml_parser.py index 33e9298..efb8d24 100644 --- a/pygraphml/graphml_parser.py +++ b/pygraphml/graphml_parser.py @@ -12,6 +12,7 @@ from . import Node from . import Edge + class GraphMLParser: """ """ @@ -35,7 +36,7 @@ def write(self, graph, fname=None): attr_node.setAttribute('id', a.name) attr_node.setAttribute('attr.name', a.name) attr_node.setAttribute('attr.type', a.type) - attr_node.setAttribute('for', a.forStr) + attr_node.setAttribute('for', a.for_str()) root.appendChild(attr_node) graph_node = doc.createElement('graph') @@ -66,7 +67,8 @@ def write(self, graph, fname=None): edge.setAttribute('source', e.node1['label']) edge.setAttribute('target', e.node2['label']) if e.directed() != graph.directed: - edge.setAttribute('directed', 'true' if e.directed() else 'false') + edge.setAttribute( + 'directed', 'true' if e.directed() else 'false') for a in e.attributes(): if e != 'label': data = doc.createElement('data') @@ -86,7 +88,7 @@ def parse(self, fname): """ g = None - with open( fname, 'r' ) as f: + with open(fname, 'r') as f: dom = minidom.parse(f) root = dom.getElementsByTagName("graphml")[0] graph = root.getElementsByTagName("graph")[0] @@ -132,5 +134,3 @@ def parse(self, fname): g = parser.parse('test.graphml') g.show(True) - - diff --git a/pygraphml/item.py b/pygraphml/item.py index 2d96ac4..7046031 100644 --- a/pygraphml/item.py +++ b/pygraphml/item.py @@ -8,6 +8,7 @@ from . import Attribute + class Item(object): """ """ @@ -37,11 +38,10 @@ def __str__(self): return s - def __getitem__(self, name): + def set_attribute(self, name, value): """ """ - - return self.attr[name].value + return NotImplemented def set_attribute(self, name, value, type): """ diff --git a/pygraphml/node.py b/pygraphml/node.py index 8f5ce02..776d2ab 100644 --- a/pygraphml/node.py +++ b/pygraphml/node.py @@ -7,6 +7,8 @@ from . import Item +from . import NodeAttribute + class Node(Item): """ @@ -26,7 +28,6 @@ def edges(self, ): return self._edges - def children(self): """ """ @@ -49,3 +50,7 @@ def parent(self): return parent + def set_attribute(self, name, value, type): + """ + """ + self.attr[name] = NodeAttribute(name, value, type)