Try to import from collections.abc for Python 3.3+ (#887)

* Try to import from collections.abc for Python 3.3+
This commit is contained in:
Kenneth Lyons 2019-05-25 16:25:49 -07:00 committed by Ogi Moore
parent 957aab008c
commit deab37d533
2 changed files with 19 additions and 8 deletions

View File

@ -2,13 +2,17 @@ from __future__ import division
from ..Qt import QtGui, QtCore from ..Qt import QtGui, QtCore
import numpy as np import numpy as np
import collections
from .. import functions as fn from .. import functions as fn
from .. import debug as debug from .. import debug as debug
from .GraphicsObject import GraphicsObject from .GraphicsObject import GraphicsObject
from ..Point import Point from ..Point import Point
from .. import getConfigOption from .. import getConfigOption
try:
from collections.abc import Callable
except ImportError:
# fallback for python < 3.3
from collections import Callable
__all__ = ['ImageItem'] __all__ = ['ImageItem']
@ -357,7 +361,7 @@ class ImageItem(GraphicsObject):
# Request a lookup table if this image has only one channel # Request a lookup table if this image has only one channel
if self.image.ndim == 2 or self.image.shape[2] == 1: if self.image.ndim == 2 or self.image.shape[2] == 1:
if isinstance(self.lut, collections.Callable): if isinstance(self.lut, Callable):
lut = self.lut(self.image) lut = self.lut(self.image)
else: else:
lut = self.lut lut = self.lut
@ -624,7 +628,7 @@ class ImageItem(GraphicsObject):
mask = self.drawMask mask = self.drawMask
src = dk src = dk
if isinstance(self.drawMode, collections.Callable): if isinstance(self.drawMode, Callable):
self.drawMode(dk, self.image, mask, ss, ts, ev) self.drawMode(dk, self.image, mask, ss, ts, ev)
else: else:
src = src[ss] src = src[ss]

View File

@ -10,8 +10,9 @@ Includes:
- ThreadsafeDict, ThreadsafeList - Self-mutexed data structures - ThreadsafeDict, ThreadsafeList - Self-mutexed data structures
""" """
import threading, sys, copy, collections import threading
#from debug import * import sys
import copy
try: try:
from collections import OrderedDict from collections import OrderedDict
@ -19,6 +20,12 @@ except ImportError:
# fallback: try to use the ordereddict backport when using python 2.6 # fallback: try to use the ordereddict backport when using python 2.6
from ordereddict import OrderedDict from ordereddict import OrderedDict
try:
from collections.abc import Sequence
except ImportError:
# fallback for python < 3.3
from collections import Sequence
class ReverseDict(dict): class ReverseDict(dict):
"""extends dict so that reverse lookups are possible by requesting the key as a list of length 1: """extends dict so that reverse lookups are possible by requesting the key as a list of length 1:
@ -326,7 +333,7 @@ class ProtectedDict(dict):
class ProtectedList(collections.Sequence): class ProtectedList(Sequence):
""" """
A class allowing read-only 'view' of a list or dict. A class allowing read-only 'view' of a list or dict.
The object can be treated like a normal list, but will never modify the original list it points to. The object can be treated like a normal list, but will never modify the original list it points to.
@ -408,7 +415,7 @@ class ProtectedList(collections.Sequence):
raise Exception("This is a list. It does not poop.") raise Exception("This is a list. It does not poop.")
class ProtectedTuple(collections.Sequence): class ProtectedTuple(Sequence):
""" """
A class allowing read-only 'view' of a tuple. A class allowing read-only 'view' of a tuple.
The object can be treated like a normal tuple, but its contents will be returned as protected objects. The object can be treated like a normal tuple, but its contents will be returned as protected objects.