Skip to content

Commit

Permalink
Use six.py instead of 2to3 - code base should now be fully Python 2.x…
Browse files Browse the repository at this point in the history
… and 3.x-compatible by default.
  • Loading branch information
astrofrog committed Sep 1, 2013
1 parent 034b72e commit cd8f902
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 15 deletions.
3 changes: 2 additions & 1 deletion astrodendro/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from astropy import units as u
from astropy.wcs import WCS

from . import six
from .structure import Structure

__all__ = ['ppv_catalog', 'pp_catalog']
Expand Down Expand Up @@ -181,7 +182,7 @@ def __init__(self, key, description, default=None, strict=False):
If True, raise KeyError if metadata not provided.
This overrides default
"""
if not isinstance(key, basestring):
if not isinstance(key, six.string_types):
raise TypeError("Key is", key, type(key))
self.key = key
self.description = description or 'no description'
Expand Down
8 changes: 4 additions & 4 deletions astrodendro/dendrogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from .progressbar import AnimatedProgressBar
from .io import IO_FORMATS
from . import pruning

from . import six

class Dendrogram(object):
"""
Expand Down Expand Up @@ -272,7 +272,7 @@ def next_idx():
print("") # newline

# Create trunk from objects with no ancestors
self.trunk = [structure for structure in structures.itervalues() if structure.parent is None]
self.trunk = [structure for structure in six.itervalues(structures) if structure.parent is None]

# Remove orphan leaves that aren't large enough
leaves_in_trunk = [structure for structure in self.trunk if structure.is_leaf]
Expand Down Expand Up @@ -305,7 +305,7 @@ def _index(self):
# add dendrogram index
ti = TreeIndex(self)

for s in self._structures_dict.itervalues():
for s in six.itervalues(self._structures_dict):
s._tree_index = ti


Expand Down Expand Up @@ -365,7 +365,7 @@ def leaves(self):
"""
A flattened list of all leaves in the dendrogram
"""
return [i for i in self._structures_dict.itervalues() if i.is_leaf]
return [i for i in six.itervalues(self._structures_dict) if i.is_leaf]

def to_newick(self):
#this caches newicks, and prevents too much recursion
Expand Down
3 changes: 2 additions & 1 deletion astrodendro/io/util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np

from .. import six

def parse_newick(string):

Expand Down Expand Up @@ -95,7 +96,7 @@ def _construct_tree(repr):
# What we do is look at the heights of this branch's
# 1st child as stored in the newick representation, and then
# work backwards to compute the merge level of this branch
first_child_repr = sub_structures_repr.itervalues().next()
first_child_repr = six.next(six.itervalues(sub_structures_repr))
if type(first_child_repr) == tuple:
height = first_child_repr[1]
else:
Expand Down
8 changes: 6 additions & 2 deletions astrodendro/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,14 @@ def __init__(self, indices, values, children=[], idx=None):
self._indices = [indices]
self._values = [values]
self._vmin, self._vmax = values, values
else: # values are for a sequence of pixels
elif isinstance(indices, list) and isinstance(values, list):
self._indices = indices
self._values = values
self._vmin, self._vmax = min(values), max(values)
else: # could be an array or iterator
self._indices = [x for x in indices]
self._values = [x for x in values]
self._vmin, self._vmax = min(values), max(values)

self.idx = idx

Expand Down Expand Up @@ -314,7 +318,7 @@ def descendants(self):
to_add = [self] # branches with children we will need to add to the list
while True:
children = []
map(children.extend, [branch.children for branch in to_add])
list(map(children.extend, [branch.children for branch in to_add]))
self._descendants.extend(children)
# Then proceed, essentially recursing through child branches:
to_add = [b for b in children if not b.is_leaf]
Expand Down
2 changes: 1 addition & 1 deletion astrodendro/test/test_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def test_4dim(self):
assert leaf.vmin == 2
assert leaf.get_npix() == 1 + 6 + 2 # Contains 1x '5', 6x '3', and 2x '2'. The other '2' should be in the branch
# Check that the only pixel in the branch is a '2' at [0,0,2,2]
assert (zip(*branches[0].indices(subtree=False)), branches[0].values(subtree=False)) == ([(0, 0, 2, 2), ], [2., ])
assert (list(zip(*branches[0].indices(subtree=False))), branches[0].values(subtree=False)) == ([(0, 0, 2, 2), ], [2., ])


from .build_benchmark import BENCHMARKS
Expand Down
4 changes: 2 additions & 2 deletions astrodendro/test/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def assert_permuted_fancyindex(x, y):
raise TypeError("Second argument not a fancy index: %s" % y)

dtype = [('%i' % i, 'i') for i in range(len(x))]
x = np.array(zip(*x), dtype=dtype)
y = np.array(zip(*y), dtype=dtype)
x = np.array(list(zip(*x)), dtype=dtype)
y = np.array(list(zip(*y)), dtype=dtype)
np.testing.assert_array_equal(np.sort(x),
np.sort(y))

Expand Down
5 changes: 1 addition & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

from setuptools import setup, Command

try: # Python 3.x
from distutils.command.build_py import build_py_2to3 as build_py
except ImportError: # Python 2.x
from distutils.command.build_py import build_py
from distutils.command.build_py import build_py


class DendroTest(Command):
Expand Down

0 comments on commit cd8f902

Please sign in to comment.