Skip to content

Improve large graph performance, fix typo, remove spurious file. #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pygraphml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 25 additions & 1 deletion pygraphml/attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Attribute:
"""
"""

def __init__(self, name, value, type = "string"):
def __init__(self, name, value, type="string"):
"""
"""

Expand All @@ -26,3 +26,27 @@ def __str__(self):
s += "%s : %s" % (self.name, str(self.value))
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"
9 changes: 7 additions & 2 deletions pygraphml/edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
"""

Expand Down Expand Up @@ -50,7 +52,6 @@ def child(self):

return self.node2


def directed(self):
"""
"""
Expand All @@ -63,3 +64,7 @@ def set_directed(self, dir):

self._directed = dir

def set_attribute(self, name, value):
"""
"""
self.attr[name] = EdgeAttribute(name, value, type)
16 changes: 9 additions & 7 deletions pygraphml/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -133,12 +133,14 @@ def add_edge(self, n1, n2, directed=False):
"""

if n1 not in self._nodes:
raise Test("fff")
print(n1)
raise Test("n1 has not been added")
if n2 not in self._nodes:
raise Test("fff")
print(n2)
raise Test("n2 has not been added")

e = Edge(n1, n2, directed)
self._edges.append(e)
self._edges.add(e)

return e

Expand Down Expand Up @@ -192,7 +194,7 @@ def set_root_by_attribute(self, value, attribute='label'):
self.set_root(n)
return n

def get_attributs(self):
def get_attributes(self):
"""
"""

Expand Down
149 changes: 0 additions & 149 deletions pygraphml/graphml_parser

This file was deleted.

11 changes: 6 additions & 5 deletions pygraphml/graphml_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from . import Node
from . import Edge


class GraphMLParser:
"""
"""
Expand All @@ -30,11 +31,12 @@ 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)
attr_node.setAttribute('attr.type', a.type)
attr_node.setAttribute('for', a.for_str())
root.appendChild(attr_node)

graph_node = doc.createElement('graph')
Expand Down Expand Up @@ -65,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')
Expand All @@ -85,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]
Expand Down Expand Up @@ -131,5 +134,3 @@ def parse(self, fname):
g = parser.parse('test.graphml')

g.show(True)


10 changes: 5 additions & 5 deletions pygraphml/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from . import Attribute


class Item(object):
"""
"""
Expand Down Expand Up @@ -37,17 +38,16 @@ def __str__(self):

return s

def __setitem__(self, name, value):
def set_attribute(self, name, value):
"""
"""
return NotImplemented

self.attr[name] = Attribute(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):
"""
Expand Down
7 changes: 6 additions & 1 deletion pygraphml/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@


from . import Item
from . import NodeAttribute


class Node(Item):
"""
Expand All @@ -26,7 +28,6 @@ def edges(self, ):

return self._edges


def children(self):
"""
"""
Expand All @@ -49,3 +50,7 @@ def parent(self):

return parent

def set_attribute(self, name, value, type):
"""
"""
self.attr[name] = NodeAttribute(name, value, type)