From a66abdec80bf85b5345234db787fe44b6be47f25 Mon Sep 17 00:00:00 2001 From: Gilles DAVID Date: Fri, 31 Mar 2017 10:48:31 +0200 Subject: [PATCH] Python3: tools --- .../src/python/controller/PlotController.py | 24 +++++++++---------- .../src/python/controller/__init__.py | 6 ++--- .../CurvePlot/src/python/model/PlotManager.py | 10 ++++---- .../src/python/model/XYPlotSetModel.py | 4 ++-- .../CurvePlot/src/python/pyqtside/__init__.py | 6 ++--- tools/CurvePlot/src/python/pyqtside/uic.py | 2 +- .../src/python/test/PlotCurve_Standalone.py | 2 +- .../CurvePlot/src/python/test/PlotTestBase.py | 4 ++-- .../CurvePlot/src/python/test/TestDesktop.py | 12 +++++----- tools/CurvePlot/src/python/test/plot_test.py | 14 +++++------ tools/CurvePlot/src/python/ui/PlotSettings.py | 8 +++---- .../src/python/views/CurveBrowserView.py | 8 +++---- .../src/python/views/CurveTabsView.py | 6 ++--- tools/CurvePlot/src/python/views/XYView.py | 12 +++++----- tools/dlgfactory/dlgfactory.py | 2 +- 15 files changed, 60 insertions(+), 60 deletions(-) diff --git a/tools/CurvePlot/src/python/controller/PlotController.py b/tools/CurvePlot/src/python/controller/PlotController.py index 76be5e6a6..f5670e52e 100644 --- a/tools/CurvePlot/src/python/controller/PlotController.py +++ b/tools/CurvePlot/src/python/controller/PlotController.py @@ -104,13 +104,13 @@ class PlotController(object): if model is None or self._blockNotifications: return - if not self._modelViews.has_key(model): + if model not in self._modelViews: return for view in self._modelViews[model]: method = "on%s" % what if what != "" and what is not None and hasattr(view, method): - exec "view.%s()" % method + exec("view.%s()" % method) elif hasattr(view, "update"): # Generic update: view.update() @@ -268,12 +268,12 @@ class PlotController(object): return -1 ps = control._plotManager.removeXYPlotSet(plot_set_id) - for _, crv in ps._curves.items(): + for _, crv in list(ps._curves.items()): control.removeModelListeners(crv) control.removeModelListeners(ps) psets = control._plotManager._plotSets if len(psets): - control._plotManager.setCurrentPlotSet(psets.keys()[-1]) + control._plotManager.setCurrentPlotSet(list(psets.keys())[-1]) return plot_set_id @classmethod @@ -282,7 +282,7 @@ class PlotController(object): gc.collect() import resource m = resource.getrusage(resource.RUSAGE_SELF)[2]*resource.getpagesize()/1e6 - print "** Used memory: %.2f Mb" % m + print("** Used memory: %.2f Mb" % m) @classmethod def DeleteCurrentItem(cls): @@ -397,7 +397,7 @@ class PlotController(object): """ @return the first plot set whose name matches the provided name. Otherwise returns -1 """ pm = cls.GetInstance()._plotManager - for _, ps in pm._plotSets.items(): + for _, ps in list(pm._plotSets.items()): if ps._title == name: return ps.getID() return -1 @@ -407,10 +407,10 @@ class PlotController(object): """ @return two lists: plot set names, and corresponding plot set IDs """ pm = cls.GetInstance()._plotManager - it = pm._plotSets.items() + it = list(pm._plotSets.items()) ids, inst, titles = [], [], [] if len(it): - ids, inst = zip(*it) + ids, inst = list(zip(*it)) if len(inst): titles = [i.getTitle() for i in inst] return list(ids), titles @@ -477,7 +477,7 @@ class PlotController(object): @return True if plot_set_id is the identifier of a valid and existing plot set. """ control = cls.GetInstance() - return control._plotManager._plotSets.has_key(plot_set_id) + return plot_set_id in control._plotManager._plotSets @classmethod def GetSalomeViewID(cls, plot_set_id): @@ -535,7 +535,7 @@ class PlotController(object): raise ValueError("Invalid marker: '%s'" % marker) cont = cls.GetInstance() - for mod, views in cont._modelViews.items(): + for mod, views in list(cont._modelViews.items()): if isinstance(mod, CurveModel) and mod.getID() == crv_id: for v in views: if isinstance(v, CurveView): @@ -566,11 +566,11 @@ class PlotController(object): from XYView import XYView cont = cls.GetInstance() - for mod, views in cont._modelViews.items(): + for mod, views in list(cont._modelViews.items()): if isinstance(mod, XYPlotSetModel) and mod.getID() == ps_id: for v in views: if isinstance(v, XYView): - exec "v.%s(*args, **kwargs)" % func + exec("v.%s(*args, **kwargs)" % func) found = True if not found: raise Exception("Invalid plot set ID or plot set currently not displayed (ps_id=%d)!" % ps_id) diff --git a/tools/CurvePlot/src/python/controller/__init__.py b/tools/CurvePlot/src/python/controller/__init__.py index fd0b521e3..62d412f2d 100644 --- a/tools/CurvePlot/src/python/controller/__init__.py +++ b/tools/CurvePlot/src/python/controller/__init__.py @@ -10,11 +10,11 @@ try: import matplotlib matplotlib.use('Qt4Agg') import matplotlib.pyplot as plt # must come after the PySide/PyQt4 switch! - plt.rcParams['font.sans-serif'].insert(0, u"DejaVu Sans") + plt.rcParams['font.sans-serif'].insert(0, "DejaVu Sans") except: - print "Warning: could not switch matplotlib to 'Qt4agg' backend. Some characters might be displayed improperly!" + print("Warning: could not switch matplotlib to 'Qt4agg' backend. Some characters might be displayed improperly!") -from PlotController import PlotController +from .PlotController import PlotController from TableModel import TableModel from CurveModel import CurveModel from PlotManager import PlotManager diff --git a/tools/CurvePlot/src/python/model/PlotManager.py b/tools/CurvePlot/src/python/model/PlotManager.py index 665c2a763..b874737e0 100644 --- a/tools/CurvePlot/src/python/model/PlotManager.py +++ b/tools/CurvePlot/src/python/model/PlotManager.py @@ -16,7 +16,7 @@ class PlotManager(Model): return len(self._plotSets) == 0 def setCurrentPlotSet(self, plotSetID, silent=False): - if not self._plotSets.has_key(plotSetID) and plotSetID != -1: + if plotSetID not in self._plotSets and plotSetID != -1: raise ValueError("Invalid plot set ID (%d)!" % plotSetID) self._currentPlotSet = self._plotSets.get(plotSetID, None) if not silent: @@ -26,8 +26,8 @@ class PlotManager(Model): return self._currentPlotSet def getPlotSetContainingCurve(self, curveID): - for ps in self._plotSets.values(): - if ps._curves.has_key(curveID): + for ps in list(self._plotSets.values()): + if curveID in ps._curves: return ps return None @@ -67,8 +67,8 @@ class PlotManager(Model): def removeXYPlotSet(self, plotSetID): Logger.Debug("====> PlotManager::removeXYPlotSet() %d" % plotSetID) - if not self._plotSets.has_key(plotSetID): - print self._plotSets + if plotSetID not in self._plotSets: + print(self._plotSets) raise ValueError("Plot set ID (%d) not found for deletion!" % plotSetID) ps = self._plotSets.pop(plotSetID) if self._currentPlotSet is ps: diff --git a/tools/CurvePlot/src/python/model/XYPlotSetModel.py b/tools/CurvePlot/src/python/model/XYPlotSetModel.py index 18bb60e2f..2edacfb9e 100644 --- a/tools/CurvePlot/src/python/model/XYPlotSetModel.py +++ b/tools/CurvePlot/src/python/model/XYPlotSetModel.py @@ -26,7 +26,7 @@ class XYPlotSetModel(Model): return self._title def setCurrentCurve(self, curveID, silent=False): - if not self._curves.has_key(curveID) and curveID != -1: + if curveID not in self._curves and curveID != -1: raise ValueError("Invalid curve ID (%d)!" % curveID) self._currentCurve = self._curves.get(curveID, None) if not silent: @@ -41,7 +41,7 @@ class XYPlotSetModel(Model): self.notifyChange("AddCurve") def removeCurve(self, curveID, silent=False): - if not self._curves.has_key(curveID): + if curveID not in self._curves: raise ValueError("Curve ID (%d) not found for deletion!" % curveID) c = self._curves.pop(curveID) if self._currentCurve is c: diff --git a/tools/CurvePlot/src/python/pyqtside/__init__.py b/tools/CurvePlot/src/python/pyqtside/__init__.py index ef46e4b6d..1cba014b1 100644 --- a/tools/CurvePlot/src/python/pyqtside/__init__.py +++ b/tools/CurvePlot/src/python/pyqtside/__init__.py @@ -8,12 +8,12 @@ try: raise Exception import PyQt4 _use_pyqt = True - print "Using PyQt4 run-time ..." + print("Using PyQt4 run-time ...") except: try: import PySide _use_pyqt = False - print "Using PySide run-time ..." + print("Using PySide run-time ...") except: raise Exception("Neither PyQt4 nor PySide could be imported!") @@ -24,7 +24,7 @@ try: if _use_pyqt: back = 'PyQt4' else: back = 'PySide' matplotlib.rcParams['backend.qt4'] = back - print "Matplotlib found - Set matplotlib backend to '%s'!" % back + print("Matplotlib found - Set matplotlib backend to '%s'!" % back) except: # No matplotlib, silently discard err message. pass diff --git a/tools/CurvePlot/src/python/pyqtside/uic.py b/tools/CurvePlot/src/python/pyqtside/uic.py index 3f9565b8e..6cc146510 100644 --- a/tools/CurvePlot/src/python/pyqtside/uic.py +++ b/tools/CurvePlot/src/python/pyqtside/uic.py @@ -2,6 +2,6 @@ from . import _use_pyqt if _use_pyqt: from PyQt4.uic import loadUi as loadUiGen else: - from pyside_dynamic import loadUi as loadUiGen + from .pyside_dynamic import loadUi as loadUiGen diff --git a/tools/CurvePlot/src/python/test/PlotCurve_Standalone.py b/tools/CurvePlot/src/python/test/PlotCurve_Standalone.py index ad2886efd..4097dedf0 100755 --- a/tools/CurvePlot/src/python/test/PlotCurve_Standalone.py +++ b/tools/CurvePlot/src/python/test/PlotCurve_Standalone.py @@ -51,7 +51,7 @@ def main(args) : trans = QTranslator() for f in ts_files: if not trans.load(f): - print "could not load translation %s!" % f + print("could not load translation %s!" % f) app.installTranslator(trans) dw = app.desktop() x, y = dw.width()*0.25, dw.height()*0.7 diff --git a/tools/CurvePlot/src/python/test/PlotTestBase.py b/tools/CurvePlot/src/python/test/PlotTestBase.py index 361b0d1e7..e9cfc9aaa 100644 --- a/tools/CurvePlot/src/python/test/PlotTestBase.py +++ b/tools/CurvePlot/src/python/test/PlotTestBase.py @@ -72,7 +72,7 @@ class PlotTestBase(unittest.TestCase): self.tmpBaselineDir = os.path.join(tempfile.gettempdir(), "curveplot_baselines") if not os.path.isdir(self.tmpBaselineDir): os.mkdir(self.tmpBaselineDir) - print "### Rebuilding base lines. Reference files will be saved to '%s'" % self.tmpBaselineDir + print("### Rebuilding base lines. Reference files will be saved to '%s'" % self.tmpBaselineDir) PlotController.WITH_CURVE_BROWSER = True XYView._DEFAULT_LEGEND_STATE = True # always show legend by default @@ -146,7 +146,7 @@ class PlotTestBase(unittest.TestCase): if not ret: # Keep file if assert is false self.keepDir = True - print "[%s] -- Failed screenshot equality, or unable to open baseline file - directory is kept alive: %s" % (self.getTestName(), self.tmpDir) + print("[%s] -- Failed screenshot equality, or unable to open baseline file - directory is kept alive: %s" % (self.getTestName(), self.tmpDir)) return ret def showTabWidget(self): diff --git a/tools/CurvePlot/src/python/test/TestDesktop.py b/tools/CurvePlot/src/python/test/TestDesktop.py index 8d72fd675..142fcf64f 100644 --- a/tools/CurvePlot/src/python/test/TestDesktop.py +++ b/tools/CurvePlot/src/python/test/TestDesktop.py @@ -177,21 +177,21 @@ class TestDesktop(QMainWindow): for _ in range(nC): x, y = self.__generateRandomData(1000) lx.append(x); ly.append(y) - print "Done generating" + print("Done generating") from time import time t0 = time() curveplot.LockRepaint() for i in range(nC): curveplot.AddCurve(lx[i], ly[i], append=True) curveplot.UnlockRepaint() - print "Elapsed: %.2f" % ( time() - t0) + print("Elapsed: %.2f" % ( time() - t0)) def clearPlotSet(self): curveplot.ClearPlotSet() def addPS(self): # Also a test for unicode! - curveplot.AddPlotSet(u'ça m embête') + curveplot.AddPlotSet('ça m embête') def addTab(self): pass @@ -216,12 +216,12 @@ class TestDesktop(QMainWindow): @Slot() def memPrint(self): i, t = curveplot.GetAllPlotSets() - print zip(i, t) + print(list(zip(i, t))) new_id = curveplot.CopyCurve(curve_id=0, plot_set_id=1) - print "created curve: %d" % new_id + print("created curve: %d" % new_id) import resource m = resource.getrusage(resource.RUSAGE_SELF)[2]*resource.getpagesize()/1e6 - print "** Used memory: %.2f Mb" % m + print("** Used memory: %.2f Mb" % m) if self.cnt >= 0 and self.cnt < self.MAX_CNT: self.cnt += 1 QTimer.singleShot(self.timeLap, self, SLOT("curveSameFig()")) diff --git a/tools/CurvePlot/src/python/test/plot_test.py b/tools/CurvePlot/src/python/test/plot_test.py index 204406cd4..3058925ff 100644 --- a/tools/CurvePlot/src/python/test/plot_test.py +++ b/tools/CurvePlot/src/python/test/plot_test.py @@ -148,7 +148,7 @@ class PlotTest(PlotTestBase): def testAddCurve(self): x, y = self.generateSine() tw = self.showTabWidget() - PlotController.AddCurve(x, y, curve_label="My curve", x_label=u"Lèés X (unicode!)", y_label=u"Et des ŷ", append=False) + PlotController.AddCurve(x, y, curve_label="My curve", x_label="Lèés X (unicode!)", y_label="Et des ŷ", append=False) self.assertTrue(self.areScreenshotEqual(tw)) def testAddCurveAppend(self): @@ -304,21 +304,21 @@ class PlotTest(PlotTestBase): def testSetLabelX(self): tw = self.showTabWidget() ps_id = PlotController.AddPlotSet("My plotset") - PlotController.SetXLabel(u"The X-s éà", ps_id) + PlotController.SetXLabel("The X-s éà", ps_id) self.assertTrue(self.areScreenshotEqual(tw)) def testSetLabelY(self): tw = self.showTabWidget() ps_id = PlotController.AddPlotSet("My plotset") - PlotController.SetYLabel(u"Tutu", ps_id) - PlotController.SetYLabel(u"The Y-s uûàç", ps_id) + PlotController.SetYLabel("Tutu", ps_id) + PlotController.SetYLabel("The Y-s uûàç", ps_id) self.assertTrue(self.areScreenshotEqual(tw)) def testSetPlotSetTitle(self): tw = self.showTabWidget() ps_id = PlotController.AddPlotSet("tutu") PlotController.AddPlotSet("tata") - PlotController.SetPlotSetTitle(u"un titre àé", ps_id) + PlotController.SetPlotSetTitle("un titre àé", ps_id) PlotController.SetCurrentPlotSet(ps_id) self.assertTrue(self.areScreenshotEqual(tw)) @@ -354,7 +354,7 @@ class PlotTest(PlotTestBase): dlg_test.showLegendCheckBox.setChecked(True) return True dlg_test.exec_ = fun - t = PlotController.GetInstance()._curveTabsView._XYViews.items() + t = list(PlotController.GetInstance()._curveTabsView._XYViews.items()) t[0][1].onSettings(dlg_test=dlg_test) self.assertTrue(self.areScreenshotEqual(tw)) @@ -389,7 +389,7 @@ class PlotTest(PlotTestBase): dlg_test.showLegendCheckBox.setChecked(True) return True dlg_test.exec_ = fun - t = PlotController.GetInstance()._curveTabsView._XYViews.items() + t = list(PlotController.GetInstance()._curveTabsView._XYViews.items()) t[0][1].onSettings(dlg_test=dlg_test) self.assertTrue(self.areScreenshotEqual(tw)) diff --git a/tools/CurvePlot/src/python/ui/PlotSettings.py b/tools/CurvePlot/src/python/ui/PlotSettings.py index bd6aac256..d26461895 100644 --- a/tools/CurvePlot/src/python/ui/PlotSettings.py +++ b/tools/CurvePlot/src/python/ui/PlotSettings.py @@ -53,10 +53,10 @@ class PlotSettings(QtGui.QDialog): return pix def accept(self): - xminText = unicode(self.axisXMinEdit.text()) - xmaxText = unicode(self.axisXMaxEdit.text()) - yminText = unicode(self.axisYMinEdit.text()) - ymaxText = unicode(self.axisYMaxEdit.text()) + xminText = str(self.axisXMinEdit.text()) + xmaxText = str(self.axisXMaxEdit.text()) + yminText = str(self.axisYMinEdit.text()) + ymaxText = str(self.axisYMaxEdit.text()) if (yminText == "" or ymaxText == "") : QtGui.QMessageBox.critical(self, "Plot settings", "A field \"YMin\" or \"YMax\" is empty") else : diff --git a/tools/CurvePlot/src/python/views/CurveBrowserView.py b/tools/CurvePlot/src/python/views/CurveBrowserView.py index ad2facd56..df296b60a 100644 --- a/tools/CurvePlot/src/python/views/CurveBrowserView.py +++ b/tools/CurvePlot/src/python/views/CurveBrowserView.py @@ -35,11 +35,11 @@ class CurveBrowserView( View, CurveTreeDockWidget) : plotSets = self._model._plotSets # The second (hidden) column in the tree bares the ID of the object and its nature (plotset or curve) - for p in plotSets.values(): - item = QtGui.QTreeWidgetItem([unicode(p.getTitle()), unicode(p.getID()) + '_set']) + for p in list(plotSets.values()): + item = QtGui.QTreeWidgetItem([str(p.getTitle()), str(p.getID()) + '_set']) treeWidget.addTopLevelItem(item) - for c in p._curves.values(): - chld = QtGui.QTreeWidgetItem([unicode(c.getTitle()), unicode(c.getID()) + '_crv']) + for c in list(p._curves.values()): + chld = QtGui.QTreeWidgetItem([str(c.getTitle()), str(c.getID()) + '_crv']) item.addChild(chld) treeWidget.expandAll() diff --git a/tools/CurvePlot/src/python/views/CurveTabsView.py b/tools/CurvePlot/src/python/views/CurveTabsView.py index 3454837c6..9189239f9 100644 --- a/tools/CurvePlot/src/python/views/CurveTabsView.py +++ b/tools/CurvePlot/src/python/views/CurveTabsView.py @@ -32,7 +32,7 @@ class CurveTabsView(View): def mapModId2ViewId(self): """ Gives a map from model ID (the model behind the XYView) to view ID """ - lst = [(v._model.getID(), view_id) for view_id, v in self._XYViews.items()] + lst = [(v._model.getID(), view_id) for view_id, v in list(self._XYViews.items())] return dict(lst) def update(self): @@ -44,7 +44,7 @@ class CurveTabsView(View): # Check list of tabs: set_mod = set(self._model._plotSets.keys()) - set_view = set([ v._model.getID() for v in self._XYViews.values() ]) + set_view = { v._model.getID() for v in list(self._XYViews.values()) } mp = self.mapModId2ViewId() # Deleted/Added curves: @@ -62,7 +62,7 @@ class CurveTabsView(View): newViews.append(self.createXYView(self._model._plotSets[a])) # Now update all tabs individually (this will trigger creation of new ones if not already there): - for v in self._XYViews.values() + newViews: + for v in list(self._XYViews.values()) + newViews: # The update on newViews will trigger the SALOME view creation: v.update() diff --git a/tools/CurvePlot/src/python/views/XYView.py b/tools/CurvePlot/src/python/views/XYView.py index e92b7cf52..d457b4cfc 100644 --- a/tools/CurvePlot/src/python/views/XYView.py +++ b/tools/CurvePlot/src/python/views/XYView.py @@ -158,7 +158,7 @@ class XYView(View): if event.mouseevent.button == 1: selected_id = -1 a = event.artist - for crv_id, cv in self._curveViews.items(): + for crv_id, cv in list(self._curveViews.items()): if cv._mplLines[0] is a: selected_id = crv_id # Use the plotmanager so that other plot sets get their current reset: @@ -301,10 +301,10 @@ class XYView(View): return action = self._curveActionGroup.checkedAction() if action is self._pointsAction : - for crv_view in self._curveViews.values(): + for crv_view in list(self._curveViews.values()): crv_view.setLineStyle("None") elif action is self._linesAction : - for crv_view in self._curveViews.values(): + for crv_view in list(self._curveViews.values()): crv_view.setLineStyle("-") else : raise NotImplementedError @@ -682,7 +682,7 @@ class XYView(View): point = event.canvas.mapToGlobal(QtCore.QPoint(event.x,canvasSize.height()-event.y)) self._popupMenu.exec_(point) else : - print "Press event on the other button" + print("Press event on the other button") #if event.button == 3 : # canvasSize = event.canvas.geometry() # point = event.canvas.mapToGlobal(QtCore.QPoint(event.x,canvasSize.height()-event.y)) @@ -690,13 +690,13 @@ class XYView(View): # self._popupMenu.show() def onMotionEvent(self, event): - print "OnMotionEvent ",event.button + print("OnMotionEvent ",event.button) #if event.button == 3 : # event.button = None # return True def onReleaseEvent(self, event): - print "OnReleaseEvent ",event.button + print("OnReleaseEvent ",event.button) #if event.button == 3 : # event.button = None # return False diff --git a/tools/dlgfactory/dlgfactory.py b/tools/dlgfactory/dlgfactory.py index f320d57a5..96b8dc4bf 100755 --- a/tools/dlgfactory/dlgfactory.py +++ b/tools/dlgfactory/dlgfactory.py @@ -118,7 +118,7 @@ if __name__ == "__main__": if options.verbose: print("Note that the following directives should be present in your CMakeLists.txt (or something like that): \n") - print(__msg_str.replace( "__CLASSNAME__", className )) + print((__msg_str.replace( "__CLASSNAME__", className ))) pass pass -- 2.39.2