Skip to content

Commit 7280c58

Browse files
ben-heildhimmel
authored andcommitted
Separate py2neo import to fix httpstream ImportError
Merges #32 Closes #31
1 parent 956f0de commit 7280c58

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

hetio/neo4j.py

+22-7
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,36 @@
22
import itertools
33
import textwrap
44
import random
5+
import pkg_resources
56
from operator import or_
67
from functools import reduce
78

8-
import py2neo
9-
import py2neo.packages.httpstream
109
import pandas
1110
from tqdm import tqdm
1211

1312
import hetio.hetnet
1413

15-
# Get py2neo version
16-
PY2NEO_VER = int(py2neo.__version__[0])
1714

18-
# Avoid SocketError
19-
py2neo.packages.httpstream.http.socket_timeout = 1e8
15+
@functools.lru_cache()
16+
def import_py2neo():
17+
"""
18+
Imports the py2neo library, checks its version, and sets the socket timeout if necessary
19+
"""
20+
import py2neo
21+
# Get py2neo version
22+
PY2NEO_VER = pkg_resources.parse_version(py2neo.__version__)
23+
version_tuple = PY2NEO_VER._version.release
24+
25+
# https://github.com/dhimmel/learn/issues/1
26+
if version_tuple[0] < 4:
27+
import py2neo.packages.httpstream
28+
# Avoid SocketError
29+
py2neo.packages.httpstream.http.socket_timeout = 1e8
30+
return py2neo, version_tuple
2031

2132
def export_neo4j(graph, uri, node_queue=200, edge_queue=5, show_progress=False):
2233
"""Export hetnet to neo4j"""
34+
py2neo, _ = import_py2neo()
2335

2436
if isinstance(uri, py2neo.Graph):
2537
db_graph = uri
@@ -89,11 +101,13 @@ def append(self, x):
89101
self.create()
90102

91103
def create(self):
104+
py2neo, version_tuple = import_py2neo()
105+
92106
if not self:
93107
return
94108

95109
# http://stackoverflow.com/a/37697792/4651668
96-
if PY2NEO_VER >= 3:
110+
if version_tuple[0] >= 3:
97111
self.db_graph.create(reduce(or_, self))
98112
else:
99113
self.db_graph.create(*self)
@@ -487,6 +501,7 @@ def permute_rel_type(uri, rel_type, nswap=None, max_tries=None, nswap_mult=10, m
487501
Randomization Techniques for Graphs. SIAM International Conference on
488502
Data Mining. https://doi.org/10.1137/1.9781611972795.67
489503
"""
504+
py2neo, _ = import_py2neo()
490505

491506
neo = py2neo.Graph(uri)
492507

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
'numpy',
2424
'scipy',
2525
'neo4j',
26-
'py2neo == 3',
26+
'py2neo',
2727
'pandas',
2828
'tqdm'
2929
]

0 commit comments

Comments
 (0)