From 242df7435767bf2cc29f6a655860463750981dc2 Mon Sep 17 00:00:00 2001 From: David Eldon Date: Fri, 3 Aug 2018 10:01:15 -0700 Subject: [PATCH] Progress toward python 3 support (#54) - close_all function in tracker - Import Qt modules from pyqtgraph instead of PyQt4 - Should allow them to be sourced from PyQt5 when working in python3, as pyqtgraph already handles this - Lead pgmpl submodule imports with pgmpl.* - `import pgmpl.info` instead of `import info` - Adjust dealias for python 3 compatibility - Stuff with iterators and dictionaries - Remove leading zeros - https://stackoverflow.com/a/36386530/6605826 --- pgmpl/__init__.py | 8 ++++---- pgmpl/axes.py | 12 ++++++------ pgmpl/colorbar.py | 4 ++-- pgmpl/contour.py | 6 +++--- pgmpl/figure.py | 10 +++++----- pgmpl/legend.py | 4 ++-- pgmpl/pyplot.py | 8 ++++---- pgmpl/text.py | 2 +- pgmpl/tracking.py | 6 +++++- pgmpl/translate.py | 8 ++++---- tests/examples.py | 4 ++-- tests/test_tracking.py | 7 +++++++ tests/test_translate.py | 2 +- 13 files changed, 46 insertions(+), 35 deletions(-) diff --git a/pgmpl/__init__.py b/pgmpl/__init__.py index fca8aa0..ef07225 100644 --- a/pgmpl/__init__.py +++ b/pgmpl/__init__.py @@ -7,16 +7,16 @@ import os # GUI imports -from PyQt4 import QtGui +from pyqtgraph import QtGui # Plotting imports import pyqtgraph as pg from matplotlib import rcParams # pgmpl imports -from info import * # Defines __version__, etc. -from util import printd -from translate import color_translator +from pgmpl.info import * # Defines __version__, etc. +from pgmpl.util import printd +from pgmpl.translate import color_translator __all__ = ['figure', 'axes', 'pyplot', 'translate', 'text', 'util'] diff --git a/pgmpl/axes.py b/pgmpl/axes.py index 4346f16..6895559 100644 --- a/pgmpl/axes.py +++ b/pgmpl/axes.py @@ -23,12 +23,12 @@ # pgmpl # noinspection PyUnresolvedReferences -import __init__ # __init__ does setup stuff like making sure a QApp exists -from translate import plotkw_translator, color_translator, setup_pen_kw, color_map_translator, dealias -from legend import Legend -from util import printd, tolist, is_numeric -from text import Text -from contour import QuadContourSet +import pgmpl.__init__ # __init__ does setup stuff like making sure a QApp exists +from pgmpl.translate import plotkw_translator, color_translator, setup_pen_kw, color_map_translator, dealias +from pgmpl.legend import Legend +from pgmpl.util import printd, tolist, is_numeric +from pgmpl.text import Text +from pgmpl.contour import QuadContourSet class Axes(pg.PlotItem): diff --git a/pgmpl/colorbar.py b/pgmpl/colorbar.py index cdb26c2..1072587 100644 --- a/pgmpl/colorbar.py +++ b/pgmpl/colorbar.py @@ -20,8 +20,8 @@ import pyqtgraph as pg # pgmpl -import __init__ -from util import printd, tolist +import pgmpl.__init__ +from pgmpl.util import printd, tolist class ColorbarBase(object): diff --git a/pgmpl/contour.py b/pgmpl/contour.py index fb5ccfa..ac243a3 100644 --- a/pgmpl/contour.py +++ b/pgmpl/contour.py @@ -22,9 +22,9 @@ # pgmpl # noinspection PyUnresolvedReferences -import __init__ # __init__ does setup stuff like making sure a QApp exists -from translate import plotkw_translator, color_translator, setup_pen_kw, color_map_translator, dealias -from util import printd, tolist, is_numeric +import pgmpl.__init__ # __init__ does setup stuff like making sure a QApp exists +from pgmpl.translate import plotkw_translator, color_translator, setup_pen_kw, color_map_translator, dealias +from pgmpl.util import printd, tolist, is_numeric class ContourSet(object): diff --git a/pgmpl/figure.py b/pgmpl/figure.py index 938313f..2cc8450 100644 --- a/pgmpl/figure.py +++ b/pgmpl/figure.py @@ -22,11 +22,11 @@ # pgmpl # noinspection PyUnresolvedReferences -import __init__ # __init__ does setup stuff like making sure a QApp exists -from tracking import tracker -from axes import Axes -from util import printd, tolist -from colorbar import Colorbar +import pgmpl.__init__ # __init__ does setup stuff like making sure a QApp exists +from pgmpl.tracking import tracker +from pgmpl.axes import Axes +from pgmpl.util import printd, tolist +from pgmpl.colorbar import Colorbar class Figure(pg.PlotWidget): diff --git a/pgmpl/legend.py b/pgmpl/legend.py index fcd2fcb..afec070 100644 --- a/pgmpl/legend.py +++ b/pgmpl/legend.py @@ -21,8 +21,8 @@ # pgmpl # noinspection PyUnresolvedReferences -import __init__ # __init__ does setup stuff like making sure a QApp exists -from util import printd, tolist, is_numeric +import pgmpl.__init__ # __init__ does setup stuff like making sure a QApp exists +from pgmpl.util import printd, tolist, is_numeric class Legend: diff --git a/pgmpl/pyplot.py b/pgmpl/pyplot.py index 91e7c66..33ca233 100644 --- a/pgmpl/pyplot.py +++ b/pgmpl/pyplot.py @@ -12,10 +12,10 @@ import numpy as np # pgmpl -from tracking import tracker -from figure import Figure -from axes import Axes -from util import printd +from pgmpl.tracking import tracker +from pgmpl.figure import Figure +from pgmpl.axes import Axes +from pgmpl.util import printd def figure(*args, **kwargs): diff --git a/pgmpl/text.py b/pgmpl/text.py index 5e23558..b151cb8 100644 --- a/pgmpl/text.py +++ b/pgmpl/text.py @@ -14,7 +14,7 @@ # Plotting imports import pyqtgraph as pg -from translate import color_translator, dealias +from pgmpl.translate import color_translator, dealias class Text(pg.TextItem): diff --git a/pgmpl/tracking.py b/pgmpl/tracking.py index 97de493..43bb9e2 100644 --- a/pgmpl/tracking.py +++ b/pgmpl/tracking.py @@ -10,7 +10,7 @@ import warnings # pgmpl -from util import printd +from pgmpl.util import printd class WTracker: @@ -34,5 +34,9 @@ def window_opened(self, win): def status(self): printd(' {} tracked windows = {}'.format(len(self.open_windows), self.open_windows)) + def close_all(self): + while len(self.open_windows): + self.open_windows[0].close() + tracker = WTracker() diff --git a/pgmpl/translate.py b/pgmpl/translate.py index 9ab4bef..bb5eae5 100644 --- a/pgmpl/translate.py +++ b/pgmpl/translate.py @@ -15,7 +15,7 @@ import numpy as np # Plotting imports -from PyQt4 import QtCore +from pyqtgraph import QtCore import pyqtgraph as pg import matplotlib.cm from matplotlib import rcParams @@ -27,7 +27,7 @@ to_rgba = colorConverter.to_rgba # pgmpl imports -from util import set_debug, printd, tolist +from pgmpl.util import set_debug, printd, tolist def dealias(**kws): @@ -49,12 +49,12 @@ def dealias(**kws): 'verticalalignment': ['va'], 'horizontalalignment': ['ha'], } - for primary, aliases in alias_lists.iteritems(): + for primary, aliases in list(alias_lists.items()): # https://stackoverflow.com/a/13998534/6605826 aliasv = {alias: kws.pop(alias, missing_value_mark) for alias in aliases} not_missing = [v != missing_value_mark for v in aliasv.values()] if primary not in kws.keys() and any(not_missing): # The aliases only need be considered if the primary is missing. - aliasu = np.atleast_1d(aliasv.keys())[np.atleast_1d(not_missing)][0] + aliasu = np.atleast_1d(list(aliasv.keys()))[np.atleast_1d(not_missing)][0] kws[primary] = aliasv[aliasu] printd(" assigned kws['{}'] = kws.pop('{}')".format(primary, aliasu)) else: diff --git a/tests/examples.py b/tests/examples.py index fd90f4e..224d358 100755 --- a/tests/examples.py +++ b/tests/examples.py @@ -74,10 +74,10 @@ def demo_plot(): axs[1, 1].errorbar(-x[40:45], y1[40:45], y1[40:45] * 0.1, x[40:45] * 0.05, color='g', xuplims=True, xlolims=True) axs[2, 0].plot(x, y1, color='m', marker='o', label='y1 purple circles') - axs[2, 0].scatter([0, 2, 4, 6, 8, 10], [80, 40, 90, 10, 20, 05], c=['r', 'b', 'g', 'k', 'm', 'y'], linewidths=1) + axs[2, 0].scatter([0, 2, 4, 6, 8, 10], [80, 40, 90, 10, 20, 5], c=['r', 'b', 'g', 'k', 'm', 'y'], linewidths=1) axs[2, 0].scatter(x, x*0+60, c=x, marker='v', s=4, edgecolors=' ') axs[2, 0].scatter(x, x*0+50, c=x, marker='s', s=5, cmap='plasma', linewidths=0) - axs[2, 0].scatter([0, 2, 4, 6, 8, 10], [50, 90, 80, 00, 50, 90], c=[90, 0, 50, 20, 75, 66], + axs[2, 0].scatter([0, 2, 4, 6, 8, 10], [50, 90, 80, 0, 50, 90], c=[90, 0, 50, 20, 75, 66], marker='^', linewidths=2, edgecolors='r') axs[2, 0].scatter(x, x*0-10, c=x, cmap='jet', marker=None, verts=[(0, 0), (0.5, 0.5), (0, 0.5), (-0.5, 0), (0, -0.5), (0.5, -0.5)]) diff --git a/tests/test_tracking.py b/tests/test_tracking.py index 09523fe..fe31cca 100755 --- a/tests/test_tracking.py +++ b/tests/test_tracking.py @@ -16,6 +16,7 @@ # pgmpl from pgmpl import __init__ # __init__ does setup stuff like making sure a QApp exists from pgmpl.tracking import WTracker, tracker +from pgmpl.figure import Figure class TestPgmplTracking(unittest.TestCase): @@ -28,6 +29,7 @@ def printv(self, *args): def test_tracker(self): assert isinstance(tracker, WTracker) + tracker.close_all() dummy = 'dummy_window' open_windows0 = copy.deepcopy(tracker.open_windows) tracker.window_opened(dummy) @@ -53,6 +55,11 @@ def test_tracker_warnings(self): self.printv(' test_tracker_warnings: called window_closed with a nonsense window to trigger a warning ' 'and got {}/{} warnings.'.format(len(w), warnings_expected)) + def test_close_all(self): + Figure() + tracker.close_all() + assert len(tracker.open_windows) == 0 + def setUp(self): test_id = self.id() test_name = '.'.join(test_id.split('.')[-2:]) diff --git a/tests/test_translate.py b/tests/test_translate.py index 85a40be..762a47d 100755 --- a/tests/test_translate.py +++ b/tests/test_translate.py @@ -10,7 +10,7 @@ import os import unittest import numpy as np -from PyQt4 import QtCore, QtGui +from pyqtgraph import QtCore, QtGui import copy from matplotlib import rcParams