Skip to content

Commit

Permalink
tox test works for python 3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
perrette committed Oct 9, 2018
1 parent 902947f commit 7e98453
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 30 deletions.
20 changes: 12 additions & 8 deletions dimarray/compat/pycompat.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
basestring = str
unicode_type = str
bytes_type = bytes
def iteritems(d):
return iter(d.items())
def itervalues(d):
return iter(d.values())
def dictitems(d):
return list(d.items())
def dictkeys(d):
return list(d.keys())
def dictvalues(d):
return list(d.values())
range = range
zip = zip
from collections import OrderedDict
Expand All @@ -18,10 +20,12 @@ def itervalues(d):
basestring = basestring
unicode_type = unicode
bytes_type = str
def iteritems(d):
return d.iteritems()
def itervalues(d):
return d.itervalues()
def dictitems(d):
return d.items()
def dictkeys(d):
return d.keys()
def dictvalues(d):
return d.values()
range = xrange
from itertools import izip as zip
try:
Expand Down
2 changes: 1 addition & 1 deletion dimarray/core/align.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def _get_aligned_axes(arrays, join='outer', axis=None , sort=False, strict=False
for jj, d in enumerate(dims):

# arrays which have that dimension
ii = filter(lambda i: d in arrays[i].dims, range(len(arrays)))
ii = [i for i in range(len(arrays)) if d in arrays[i].dims]

if strict and len(ii) != len(arrays):
raise ValueError("align (strict=True): some arrays lack dimension {}".format(d))
Expand Down
9 changes: 5 additions & 4 deletions dimarray/core/bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
"""
from __future__ import print_function
from __future__ import absolute_import
from future.utils import string_types
from future.utils import string_types, PY2
import warnings
import copy
from collections import OrderedDict as odict
import numpy as np
from dimarray.config import get_option
from dimarray.compat.pycompat import iteritems, zip
from dimarray.tools import is_numeric
from dimarray.core.indexing import locate_one, locate_many, locate_slice, expanded_indexer
from dimarray.prettyprinting import repr_axis, repr_dataset, repr_axes, str_axes, str_dataset, str_dimarray
Expand All @@ -28,7 +27,7 @@ def __getattr__(self, name):
elif name not in self.__metadata_include__ \
and (name.startswith('_') or name in self.__metadata_exclude__):
pass # raise error
elif hasattr(self, 'dims') and name in self.dims:
elif hasattr(type(self), 'dims') and name in self.dims:
return self.axes[name].values # return axis values
elif name in self.attrs.keys():
return self.attrs[name]
Expand All @@ -40,7 +39,7 @@ def __setattr__(self, name, value):
or name in self.__metadata_exclude__ \
or hasattr(self.__class__, name)):
object.__setattr__(self, name, value) # do nothing special
elif hasattr(self, 'axes') and name in self.dims:
elif hasattr(type(self), 'axes') and name in self.dims:
self.axes[name][()] = value # modify axis values
else:
self.attrs[name] = value # add as metadata
Expand All @@ -54,6 +53,8 @@ def __delattr__(self, name):
else:
return object.__delattr__(self, name)



# class GetSetDelAttrMixin(object):
# pass
#
Expand Down
9 changes: 5 additions & 4 deletions dimarray/core/dimarraycls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from dimarray.config import get_option
from dimarray import plotting
from dimarray.prettyprinting import repr_dimarray
from dimarray.compat.pycompat import dictkeys, dictvalues

# from .metadata import MetadataBase
from .bases import AbstractDimArray, GetSetDelAttrMixin, OpMixin
Expand Down Expand Up @@ -430,9 +431,9 @@ def from_nested(cls, nested_data, labels=None, dims=None, align=True):
# convert dict to sequence if needed
if _is_dictlike(nested_data):
if label0 is None:
label0 = nested_data.keys()
label0 = dictkeys(nested_data)
if callable(nested_data.values):
nested_data = nested_data.values()
nested_data = dictvalues(nested_data)
else:
nested_data = nested_data.values

Expand Down Expand Up @@ -1674,11 +1675,11 @@ def array(data, *args, **kwargs):
from collections import OrderedDict as odict

d = odict()
keys = kwargs.pop('keys', data.keys())
keys = kwargs.pop('keys', dictkeys(data))
for k in keys:
d[k] = data[k]

data = d.values()
data = dictvalues(d)
kwargs['keys'] = keys

# sequence: align axes and stack arrays
Expand Down
5 changes: 3 additions & 2 deletions dimarray/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import dimarray as da # for the doctest, so that they are testable via py.test
from dimarray.tools import format_doc, isscalar
from dimarray.config import get_option
from dimarray.compat.pycompat import dictkeys, dictvalues

from .core import DimArray, array, Axis, Axes
from .core import align as align_axes, stack, concatenate
Expand Down Expand Up @@ -70,8 +71,8 @@ def __init__(self, *args, **kwargs):
super(Dataset, self).__init__()
#self.data = odict()

values = data.values()
keys = data.keys()
values = dictvalues(data)
keys = dictkeys(data)

# Check everything is a DimArray
#for key, value in zip(keys, values):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def test_dataset_derived_axes(ds):
def test_append_axis():
ds = da.Dataset()
ds.axes.append(da.Axis([10,20,30], 'myaxis'))
assert ds.keys() == []
assert list(ds.keys()) == []
assert ds.dims == ("myaxis",)

# Test indexing dataset
Expand Down
18 changes: 9 additions & 9 deletions tests/test_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def teardown_class(cls):
def test_csiro(self):
" manual check for the CSIRO dataset "
ds = self.ds
assert ds.keys() == [u'tsl', u'temp']
assert list(ds.keys()) == [u'tsl', u'temp']
assert ds.dims == ('time', 'scenario')
assert ds.scenario[:].tolist() == ['historical', 'rcp26', 'rcp45', 'rcp60', 'rcp85']
assert_equal( ds.time , np.arange(1850, 2301) )
Expand Down Expand Up @@ -112,7 +112,7 @@ def test_access(self):

def test_indexing(self):

n0 = len(self.ds.nc.dimensions.values()[0])
n0 = len(list(self.ds.nc.dimensions.values())[0])
boolidx = ([True, False]*n0)[:n0] # every second index is True, starting from the first
indices = [5, -1, [0, 1, 2], boolidx, (0,1), slice(None), (), slice(2,10,2)]

Expand Down Expand Up @@ -204,7 +204,7 @@ def test_read_position_index(self):

# position indexing
with da.open_nc(self.ncfile) as ds_disk:
v0 = ds_disk.keys()[0]
v0 = list(ds_disk.keys())[0]
for idx in indices:
print(v0, self.ds_var[v0].shape, idx)
expected = self.ds_var[v0].ix[idx]
Expand All @@ -224,7 +224,7 @@ def test_read_label_index(self):
labels.append(boolidx)

with da.open_nc(self.ncfile) as ds_disk:
v0 = ds_disk.keys()[0]
v0 = list(ds_disk.keys())[0]
for lidx in labels:
expected = self.ds_var[v0][lidx]
actual = ds_disk[v0][lidx]
Expand Down Expand Up @@ -287,34 +287,34 @@ def test_standalone_axis(tmpdir):
# see test in test_dataset.py
ds = da.Dataset()
ds.axes.append(da.Axis([10,20,30], 'myaxis'))
assert ds.keys() == []
assert list(ds.keys()) == []
assert ds.dims == ("myaxis",)

fname = tmpdir.join("test_standalone_axis.nc").strpath # have test.nc in some temporary directory
ds.write_nc(fname)
# read again
ds = da.read_nc(fname)
assert ds.keys() == []
assert list(ds.keys()) == []
assert ds.dims == ("myaxis",)

def test_redundant_axis(tmpdir):
# see test in test_dataset.py
ds = da.Dataset()
ds["myaxis"] = da.DimArray([10,20,30], da.Axis([10,20,30], 'myaxis'))
assert ds.keys() == ["myaxis"]
assert list(ds.keys()) == ["myaxis"]
assert ds.dims == ("myaxis",)

fname = tmpdir.join("test_redundant_axis.nc").strpath # have test.nc in some temporary directory
ds.write_nc(fname)

# read whole dataset: variable not counted as variable
ds = da.read_nc(fname)
assert ds.keys() == []
assert list(ds.keys()) == []
assert ds.dims == ("myaxis",)

# specify dimension variable
ds = da.read_nc(fname, ["myaxis"])
assert ds.keys() == ["myaxis"]
assert list(ds.keys()) == ["myaxis"]
assert ds.dims == ("myaxis",)


Expand Down
2 changes: 1 addition & 1 deletion tests/test_transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def test3d(self):
" test 3-D interp"
dima_3d = self.dima.newaxis('bla',np.arange(2)).newaxis('bli',np.arange(3)) # should be 10^4 slower
interpolated = dima_3d.interp_axis(self.newindex, axis=-1)
for i in xrange(2):
for i in range(2):
actual = interpolated.ix[i,0]
expected = dima_3d.ix[i,0].interp_axis(self.newindex) # use np.interp directly
assert_equal_dimarrays(actual, expected)
Expand Down

0 comments on commit 7e98453

Please sign in to comment.