From deab37d533975f4eccc318dc0b5ede2dfafd2181 Mon Sep 17 00:00:00 2001 From: Kenneth Lyons Date: Sat, 25 May 2019 16:25:49 -0700 Subject: [PATCH] Try to import from collections.abc for Python 3.3+ (#887) * Try to import from collections.abc for Python 3.3+ --- pyqtgraph/graphicsItems/ImageItem.py | 10 +++++++--- pyqtgraph/pgcollections.py | 17 ++++++++++++----- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/pyqtgraph/graphicsItems/ImageItem.py b/pyqtgraph/graphicsItems/ImageItem.py index 2ebce2c7..65e87eec 100644 --- a/pyqtgraph/graphicsItems/ImageItem.py +++ b/pyqtgraph/graphicsItems/ImageItem.py @@ -2,13 +2,17 @@ from __future__ import division from ..Qt import QtGui, QtCore import numpy as np -import collections from .. import functions as fn from .. import debug as debug from .GraphicsObject import GraphicsObject from ..Point import Point from .. import getConfigOption +try: + from collections.abc import Callable +except ImportError: + # fallback for python < 3.3 + from collections import Callable __all__ = ['ImageItem'] @@ -357,7 +361,7 @@ class ImageItem(GraphicsObject): # Request a lookup table if this image has only one channel 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) else: lut = self.lut @@ -624,7 +628,7 @@ class ImageItem(GraphicsObject): mask = self.drawMask src = dk - if isinstance(self.drawMode, collections.Callable): + if isinstance(self.drawMode, Callable): self.drawMode(dk, self.image, mask, ss, ts, ev) else: src = src[ss] diff --git a/pyqtgraph/pgcollections.py b/pyqtgraph/pgcollections.py index ac7f68fe..ef3db258 100644 --- a/pyqtgraph/pgcollections.py +++ b/pyqtgraph/pgcollections.py @@ -10,15 +10,22 @@ Includes: - ThreadsafeDict, ThreadsafeList - Self-mutexed data structures """ -import threading, sys, copy, collections -#from debug import * +import threading +import sys +import copy try: from collections import OrderedDict except ImportError: # fallback: try to use the ordereddict backport when using python 2.6 from ordereddict import OrderedDict - + +try: + from collections.abc import Sequence +except ImportError: + # fallback for python < 3.3 + from collections import Sequence + class ReverseDict(dict): """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. 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.") -class ProtectedTuple(collections.Sequence): +class ProtectedTuple(Sequence): """ 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.