|
2 | 2 | import itertools
|
3 | 3 | import textwrap
|
4 | 4 | import random
|
| 5 | +import pkg_resources |
5 | 6 | from operator import or_
|
6 | 7 | from functools import reduce
|
7 | 8 |
|
8 |
| -import py2neo |
9 |
| -import py2neo.packages.httpstream |
10 | 9 | import pandas
|
11 | 10 | from tqdm import tqdm
|
12 | 11 |
|
13 | 12 | import hetio.hetnet
|
14 | 13 |
|
15 |
| -# Get py2neo version |
16 |
| -PY2NEO_VER = int(py2neo.__version__[0]) |
17 | 14 |
|
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 |
20 | 31 |
|
21 | 32 | def export_neo4j(graph, uri, node_queue=200, edge_queue=5, show_progress=False):
|
22 | 33 | """Export hetnet to neo4j"""
|
| 34 | + py2neo, _ = import_py2neo() |
23 | 35 |
|
24 | 36 | if isinstance(uri, py2neo.Graph):
|
25 | 37 | db_graph = uri
|
@@ -89,11 +101,13 @@ def append(self, x):
|
89 | 101 | self.create()
|
90 | 102 |
|
91 | 103 | def create(self):
|
| 104 | + py2neo, version_tuple = import_py2neo() |
| 105 | + |
92 | 106 | if not self:
|
93 | 107 | return
|
94 | 108 |
|
95 | 109 | # http://stackoverflow.com/a/37697792/4651668
|
96 |
| - if PY2NEO_VER >= 3: |
| 110 | + if version_tuple[0] >= 3: |
97 | 111 | self.db_graph.create(reduce(or_, self))
|
98 | 112 | else:
|
99 | 113 | 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
|
487 | 501 | Randomization Techniques for Graphs. SIAM International Conference on
|
488 | 502 | Data Mining. https://doi.org/10.1137/1.9781611972795.67
|
489 | 503 | """
|
| 504 | + py2neo, _ = import_py2neo() |
490 | 505 |
|
491 | 506 | neo = py2neo.Graph(uri)
|
492 | 507 |
|
|
0 commit comments