Skip to content

Commit e7399ac

Browse files
committed
Merge pull request #11 from hayd/graph
TST graph tests pass
2 parents 16cc2c6 + 646e5fc commit e7399ac

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ pattern/web/cache/tmp/
1111
web/cache/tmp/
1212
test/pattern_unittest_db
1313
pattern_unittest_db
14+
examples/06-graph/test/
1415

1516
.DS_Store

pattern/graph/__init__.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,18 @@ def __init__(self, id="", radius=5, **kwargs):
133133
self.fill = kwargs.pop("fill", None)
134134
self.stroke = kwargs.pop("stroke", (0, 0, 0, 1))
135135
self.strokewidth = kwargs.pop("strokewidth", 1)
136+
137+
if not isinstance(id, unicode):
138+
id = str(id).decode("utf-8", "ignore")
139+
140+
# FIXME this is a mess.
136141
self.text = kwargs.get("text", True) and \
137-
Text(isinstance(id, unicode) and id or str(id).decode("utf-8", "ignore"),
142+
Text(id,
138143
width=85,
139144
fill=kwargs.pop("text", (0, 0, 0, 1)),
140-
fontsize=kwargs.pop("fontsize", 11), **kwargs) or None
145+
fontsize=kwargs.pop("fontsize", 11),
146+
**kwargs) or None
147+
141148
self._weight = None # Calculated by Graph.eigenvector_centrality().
142149
# Calculated by Graph.betweenness_centrality().
143150
self._centrality = None
@@ -260,11 +267,19 @@ def contains(self, x, y):
260267
def __repr__(self):
261268
return "%s(id=%s)" % (self.__class__.__name__, repr(self.id))
262269

263-
def __eq__(self, node):
264-
return isinstance(node, Node) and self.id == node.id
270+
def __eq__(self, other):
271+
return isinstance(other, Node) and self.id == other.id
272+
273+
def __ne__(self, other):
274+
return not self.__eq__(other)
275+
276+
def __lt__(self, other):
277+
return isinstance(other, Node) and self.id < other.id
265278

266-
def __ne__(self, node):
267-
return not self.__eq__(node)
279+
def __hash__(self):
280+
# an alternative might be to use hash(self.id) in some way
281+
# since this is supposed to be unique.
282+
return id(self)
268283

269284
#--- NODE LINKS ----------------------------------------------------------
270285

@@ -1181,7 +1196,7 @@ def partition(graph):
11811196
g[i] = union(g[i], g[j])
11821197
g[j] = []
11831198
g = [graph.copy(nodes=[graph[id] for id in n]) for n in g if n]
1184-
g.sort(lambda a, b: len(b) - len(a))
1199+
g.sort(key=len, reverse=True)
11851200
return g
11861201

11871202

pattern/graph/commonsense.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
from __future__ import absolute_import
1010

11-
from codecs import BOM_UTF8
1211
from itertools import chain
12+
import os
13+
import sys
1314

1415
try:
1516
from urllib.request import urlopen
@@ -19,7 +20,11 @@
1920
from .__init__ import Graph, Node, Edge, bfs
2021
from .__init__ import WEIGHT, CENTRALITY, EIGENVECTOR, BETWEENNESS
2122

22-
import os
23+
from codecs import BOM_UTF8
24+
if sys.version > "3":
25+
BOM_UTF8 = BOM_UTF8.decode("utf-8")
26+
27+
basestring = str
2328

2429
try:
2530
MODULE = os.path.dirname(os.path.realpath(__file__))
@@ -125,7 +130,10 @@ def __init__(self, data=os.path.join(MODULE, "commonsense.csv"), **kwargs):
125130
if data is not None:
126131
s = open(data).read()
127132
s = s.strip(BOM_UTF8)
128-
s = s.decode("utf-8")
133+
try:
134+
s = s.decode("utf-8")
135+
except AttributeError: # python 3
136+
pass
129137
s = ((v.strip("\"") for v in r.split(",")) for r in s.splitlines())
130138
for concept1, relation, concept2, context, weight in s:
131139
self.add_edge(concept1, concept2,

test/test_graph.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ def test_fringe(self):
288288
# Assert leaf fetching.
289289
g = self.g.copy()
290290
self.assertEqual(g.fringe(0), [g["a"], g["c"]])
291-
self.assertEqual(g.fringe(1), [g["a"], g["b"], g["c"]])
291+
# FIXME the ordering is variable in python3
292+
self.assertEqual(set(g.fringe(1)), set([g["a"], g["b"], g["c"]]))
292293
print("pattern.graph.Graph.fringe()")
293294

294295
def test_split(self):

0 commit comments

Comments
 (0)