Skip to content

Commit

Permalink
remove Python2 crumbs
Browse files Browse the repository at this point in the history
  • Loading branch information
a-detiste committed Apr 28, 2024
1 parent a89d844 commit 09e490a
Show file tree
Hide file tree
Showing 26 changed files with 43 additions and 73 deletions.
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ init-import=no

# List of qualified module names which can have objects that can redefine
# builtins.
redefining-builtins-modules=six.moves,past.builtins,future.builtins,builtins,io
redefining-builtins-modules=builtins,io


[CLASSES]
Expand Down
2 changes: 1 addition & 1 deletion docs/source/examples/lif.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import numpy as np


class LIF(object):
class LIF:

def __init__(self, stepsize=0.0001, offset=1.6, tau_m=0.025, tau_a=0.02, da=0.0, D=3.5):
self.stepsize = stepsize # simulation stepsize [s]
Expand Down
11 changes: 1 addition & 10 deletions nixio/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
from sys import maxsize as maxint
import numpy as np
from inspect import isclass
from six import string_types
try:
from collections.abc import OrderedDict
except ImportError:
from collections import OrderedDict
import sys

from .util import find as finders
from .compression import Compression
Expand Down Expand Up @@ -302,13 +300,6 @@ def create_data_frame(self, name="", type_="", col_dict=None,
return self.data_frames[objid]

util.check_entity_name_and_type(name, type_)
if (isinstance(col_dict, dict)
and not isinstance(col_dict, OrderedDict)
and sys.version_info[0] < 3):
raise TypeError("Cannot create a DataFrame from a dictionary "
"in Python 2 as the order of keys is not "
"preserved. Please use the OrderedDict class "
"from the collections module instead.")

if data is not None:
shape = len(data)
Expand Down Expand Up @@ -358,7 +349,7 @@ def create_data_frame(self, name="", type_="", col_dict=None,
if col_dict is not None:
for nam, dt in col_dict.items():
if isclass(dt):
if any(issubclass(dt, st) for st in string_types) \
if any(issubclass(dt, st) for st in str) \
or issubclass(dt, np.string_):
col_dict[nam] = util.vlen_str_dtype
if 'U' in str(dt) or dt == np.string_:
Expand Down
2 changes: 1 addition & 1 deletion nixio/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from . import util


class Container(object):
class Container:
"""
Container acts as an interface to container groups in the backend. In the
case of HDF5, this is a group that is used as a container for other groups.
Expand Down
4 changes: 1 addition & 3 deletions nixio/data_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# Redistribution and use in source and binary forms, with or without
# modification, are permitted under the terms of the BSD License. See
# LICENSE file in the root of the Project.
from __future__ import (absolute_import, division, print_function)
try:
from collections.abc import Iterable
except ImportError:
Expand All @@ -20,7 +19,6 @@
from .data_set import DataSet
from .datatype import DataType
from .section import Section
from six import string_types
import csv


Expand Down Expand Up @@ -57,7 +55,7 @@ def append_column(self, column, name, datatype=None):
if datatype is None:
datatype = DataType.get_dtype(column[0])
if isclass(datatype) and any(issubclass(datatype, st)
for st in string_types):
for st in str):
datatype = util.vlen_str_dtype
dt_arr = [(n, dty) for n, dty in zip(self.column_names, self.dtype)]
dt_arr.append((name, datatype))
Expand Down
2 changes: 1 addition & 1 deletion nixio/data_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import numpy as np


class DataSet(object):
class DataSet:
"""
Data IO object for DataArray.
"""
Expand Down
5 changes: 2 additions & 3 deletions nixio/datatype.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
# modification, are permitted under the terms of the BSD License. See
# LICENSE file in the root of the Project.
from numbers import Integral, Real
from six import string_types

import numpy as np


BOOLS = (bool, np.bool_)


class DataType(object):
class DataType:
UInt8 = np.uint8
UInt16 = np.uint16
UInt32 = np.uint32
Expand All @@ -41,7 +40,7 @@ def get_dtype(cls, value):
return cls.Int64
elif isinstance(value, Real):
return cls.Double
elif isinstance(value, string_types):
elif isinstance(value, str):
return cls.String
else:
raise ValueError("Unknown type for value {}".format(value))
Expand Down
4 changes: 2 additions & 2 deletions nixio/dimensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def _inst_item(self, item):
return cls(self._parent, idx)


class DimensionLink(object):
class DimensionLink:
"""
Links a Dimension to a data object (DataArray or DataFrame).
Expand Down Expand Up @@ -223,7 +223,7 @@ def _data_object_type(self):
return self._h5group.get_attr("data_object_type")


class Dimension(object):
class Dimension:

def __init__(self, nixfile, data_array, index):
dimgroup = data_array._h5group.open_group("dimensions")
Expand Down
2 changes: 1 addition & 1 deletion nixio/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from . import util


class Entity(object):
class Entity:

def __init__(self, nixfile, nixparent, h5group):
util.check_entity_id(h5group.get_attr("entity_id"))
Expand Down
5 changes: 2 additions & 3 deletions nixio/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
from .data_frame import DataFrame
from .link_type import LinkType
from .exceptions import UnsupportedLinkType
from six import string_types
from .util import util


class Feature(object):
class Feature:

def __init__(self, nixfile, nixparent, h5group):
util.check_entity_id(h5group.get_attr("entity_id"))
Expand Down Expand Up @@ -53,7 +52,7 @@ def link_type(self):

@link_type.setter
def link_type(self, link_type):
if isinstance(link_type, string_types):
if isinstance(link_type, str):
link_type = link_type.lower()
link_type = LinkType(link_type)
self._h5group.set_attr("link_type", link_type.value)
Expand Down
4 changes: 2 additions & 2 deletions nixio/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def can_read(nixfile):
return False


class FileMode(object):
class FileMode:
ReadOnly = 'r'
ReadWrite = 'a'
Overwrite = 'w'
Expand Down Expand Up @@ -82,7 +82,7 @@ def make_fcpl():
return fcpl


class File(object):
class File:

def __init__(self, path, mode=FileMode.ReadWrite,
compression=Compression.Auto,
Expand Down
7 changes: 3 additions & 4 deletions nixio/hdf5/h5dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
# Redistribution and use in source and binary forms, with or without
# modification, are permitted under the terms of the BSD License. See
# LICENSE file in the root of the Project.
from six import ensure_str
import numpy as np
from ..datatype import DataType
from .. import util


class H5DataSet(object):
class H5DataSet:

def __init__(self, parent, name, dtype=None, shape=None,
compression=False):
Expand Down Expand Up @@ -61,7 +60,7 @@ def read_data(self, slc=None):
# Let's change it to IndexError
raise IndexError(te_exc)
if data.dtype == util.vlen_str_dtype:
data = np.reshape(np.array(list(map(ensure_str, data.ravel())), dtype=object), data.shape)
data = np.reshape(np.array(list(map(str, data.ravel())), dtype=object), data.shape)
elif data.dtype.fields:
data = self._convert_string_cols(data)
return data
Expand All @@ -75,7 +74,7 @@ def _convert_string_cols(data):

def conv_row(row):
for field in str_cols:
row[field] = ensure_str(row[field])
row[field] = str(row[field])
if str_cols:
if not data.shape:
# single row
Expand Down
2 changes: 1 addition & 1 deletion nixio/hdf5/h5group.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .. import util


class H5Group(object):
class H5Group:

def __init__(self, parent, name, create=False):
self._parent = parent
Expand Down
11 changes: 5 additions & 6 deletions nixio/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from collections import Sequence, Iterable
from enum import Enum
from numbers import Number
from six import string_types, ensure_str, ensure_text
import numpy as np

from .datatype import DataType
Expand Down Expand Up @@ -232,7 +231,7 @@ def odml_type(self, new_type):
def _read_old_values(self):
val = self._h5dataset.dataset[:]
if len(val) > 0 and isinstance(val[0]["value"], bytes):
return tuple(ensure_str(v["value"]) for v in val)
return tuple(str(v["value"]) for v in val)
return tuple(v["value"] for v in val)

@property
Expand All @@ -249,7 +248,7 @@ def values(self):

def data_to_value(dat):
if isinstance(dat, bytes):
dat = ensure_str(dat) # py2compat
dat = str(dat) # py2compat
return dat

values = tuple(map(data_to_value, data))
Expand All @@ -268,13 +267,13 @@ def values(self, vals):
self.delete_values()
return

if not isinstance(vals, (Sequence, Iterable)) or isinstance(vals, string_types):
if not isinstance(vals, (Sequence, Iterable)) or isinstance(vals, str):
vals = [vals]

# Make sure all values are of the same data type
vtype = self._check_new_value_types(vals)
if vtype == DataType.String:
vals = [ensure_text(v) for v in vals] # py2compat
vals = [str(v) for v in vals]
self._h5dataset.shape = np.shape(vals)
data = np.array(vals, dtype=vtype)
self._h5dataset.write_data(data)
Expand All @@ -294,7 +293,7 @@ def extend_values(self, data):
dataset.write_data(arr, slc=np.s_[src_len: src_len + dlen])

def _check_new_value_types(self, data):
if isinstance(data, (Sequence, Iterable)) and not isinstance(data, string_types):
if isinstance(data, (Sequence, Iterable)) and not isinstance(data, str):
single_val = data[0]
else:
single_val = data
Expand Down
5 changes: 2 additions & 3 deletions nixio/section.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from collections.abc import Sequence, Iterable
except ImportError:
from collections import Sequence, Iterable
from six import string_types
import numpy as np
from .container import Container, SectionContainer
from .datatype import DataType
Expand All @@ -25,7 +24,7 @@
from . import exceptions


class S(object): # pylint: disable=invalid-name
class S: # pylint: disable=invalid-name
def __init__(self, section_type, section=None):
self.section_type = section_type
self.section = section
Expand Down Expand Up @@ -147,7 +146,7 @@ def create_property(self, name="", values_or_dtype=0, oid=None,
# Make sure all values are of the same data type
single_val = vals
if (isinstance(vals, (Sequence, Iterable)) and
not isinstance(vals, string_types)):
not isinstance(vals, str)):
single_val = vals[0]
else:
# Make sure the data will always be created with an array.
Expand Down
3 changes: 1 addition & 2 deletions nixio/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import warnings

import numpy as np
from six import string_types

from .entity import Entity
from .source_link_container import SourceLinkContainer
Expand Down Expand Up @@ -107,7 +106,7 @@ def create_feature(self, data, link_type):
:returns: The created feature object.
:rtype: nixio.Feature
"""
if isinstance(link_type, string_types):
if isinstance(link_type, str):
link_type = link_type.lower()
link_type = LinkType(link_type)
features = self._h5group.open_group("features")
Expand Down
9 changes: 3 additions & 6 deletions nixio/test/test_data_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
# LICENSE file in the root of the Project.
import os
import time
from six import string_types
import sys
import unittest
import numpy as np
import nixio as nix
Expand Down Expand Up @@ -278,8 +276,7 @@ def test_data_array_dtype(self):
assert da.dtype == test_data.dtype

bdata = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
if sys.version_info[0] == 3:
bdata = [bytes(x, 'UTF-8') for x in bdata]
bdata = [bytes(x, 'UTF-8') for x in bdata]

void_data = np.array(bdata, dtype='V1')
da = self.block.create_data_array('dtype_opaque', 'b', data=void_data)
Expand Down Expand Up @@ -307,8 +304,8 @@ def test_data_array_dimensions(self):
self.assertRaises(IndexError, lambda: self.array.dimensions[-4])
self.assertRaises(IndexError, lambda: self.array.dimensions[3])

assert isinstance(str(self.array.dimensions), string_types)
assert isinstance(repr(self.array.dimensions), string_types)
assert isinstance(str(self.array.dimensions), str)
assert isinstance(repr(self.array.dimensions), str)

dims = list(self.array.dimensions)
for i in range(3):
Expand Down
13 changes: 5 additions & 8 deletions nixio/test/test_data_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
import os
import time
import numpy as np
from six import string_types
try:
from collections.abc import OrderedDict
except ImportError:
from collections import OrderedDict
import sys


class TestDataFrame(unittest.TestCase):
Expand Down Expand Up @@ -65,7 +63,7 @@ def test_create_with_list(self):
assert df_li.column_names == self.df1.column_names
assert df_li.dtype == self.df1.dtype
for i in df_li[:]:
self.assertIsInstance(i['id'], string_types)
self.assertIsInstance(i['id'], str)
self.assertIsInstance(i['sig2'], np.int32)

def test_column_name_collision(self):
Expand Down Expand Up @@ -220,11 +218,10 @@ def test_df_shape(self):
assert tuple(self.df1.df_shape) == (10, 5)
# create df with incorrect dimension to see if Error is raised
arr = np.arange(1000).reshape(10, 10, 10)
if sys.version_info[0] == 3:
with self.assertRaises(ValueError):
self.block.create_data_frame('err', 'err',
{'name': np.int64},
data=arr)
with self.assertRaises(ValueError):
self.block.create_data_frame('err', 'err',
{'name': np.int64},
data=arr)

def test_data_type(self):
assert self.df1.dtype[4] == np.int32
Expand Down
Loading

0 comments on commit 09e490a

Please sign in to comment.