|
2 | 2 | Main Taxonomy object/class code.
|
3 | 3 | """
|
4 | 4 | import gzip
|
5 |
| -import itertools |
6 | 5 | import networkx
|
7 | 6 | import os
|
| 7 | +import sys |
| 8 | + |
8 | 9 | try:
|
9 | 10 | import simplejson as json
|
10 | 11 | except ImportError:
|
11 | 12 | import json
|
12 | 13 |
|
| 14 | +try: |
| 15 | + from itertools import izip_longest as zip_longest |
| 16 | +except ImportError: |
| 17 | + from itertools import zip_longest |
| 18 | + |
| 19 | +try: |
| 20 | + from functools import reduce |
| 21 | +except ImportError: |
| 22 | + pass |
| 23 | + |
13 | 24 | from networkx.readwrite import json_graph
|
14 | 25 |
|
15 | 26 | from taxonomy.exceptions import TaxonomyException
|
@@ -74,13 +85,13 @@ def save(self, f, compress=True):
|
74 | 85 | out = {}
|
75 | 86 | out['node_link_data'] = json_graph.node_link_data(self.tax_graph)
|
76 | 87 | out['metadata'] = self.metadata
|
77 |
| - if isinstance(f, (file, gzip.GzipFile)): |
| 88 | + if (sys.version_info[0] == 2 and isinstance(f, (file, gzip.GzipFile))) or hasattr(f, 'write'): |
78 | 89 | json.dump(out, f)
|
79 | 90 | else:
|
80 | 91 | if gzip:
|
81 | 92 | if os.path.splitext(f)[1] != '.gz':
|
82 | 93 | f = f + '.gz'
|
83 |
| - json.dump(out, gzip.open(f, mode='w')) |
| 94 | + json.dump(out, gzip.open(f, mode='wt')) |
84 | 95 | else:
|
85 | 96 | json.dump(out, open(f, mode='w'))
|
86 | 97 |
|
@@ -133,7 +144,7 @@ def lowest_common_ancestor_double(self, tax_id_1, tax_id_2):
|
133 | 144 | parent_gen_2 = networkx.dfs_preorder_nodes(self.tax_graph, tax_id_2)
|
134 | 145 | parent_tax_ids1 = set()
|
135 | 146 | parent_tax_ids2 = set()
|
136 |
| - for ptax_id_1, ptax_id_2 in itertools.izip_longest(parent_gen_1, parent_gen_2, |
| 147 | + for ptax_id_1, ptax_id_2 in zip_longest(parent_gen_1, parent_gen_2, |
137 | 148 | fillvalue='1'):
|
138 | 149 | parent_tax_ids1.add(ptax_id_1)
|
139 | 150 | if ptax_id_2 in parent_tax_ids1:
|
|
0 commit comments