More PySide testing
authorEd Page <eopage@byu.net>
Thu, 7 Jul 2011 01:09:22 +0000 (20:09 -0500)
committerEd Page <eopage@byu.net>
Thu, 7 Jul 2011 01:09:22 +0000 (20:09 -0500)
hand_tests/double_disconnect.py [new file with mode: 0755]
hand_tests/producer_consumer.py [new file with mode: 0755]
hand_tests/threading.py [deleted file]

diff --git a/hand_tests/double_disconnect.py b/hand_tests/double_disconnect.py
new file mode 100755 (executable)
index 0000000..2acd797
--- /dev/null
@@ -0,0 +1,75 @@
+#!/usr/bin/env python
+
+from __future__ import with_statement
+from __future__ import division
+
+import sys
+import logging
+
+PYSIDE = False
+DISCONNECT_ON_DELETE = False
+
+if PYSIDE:
+       import PySide.QtCore as QtCore
+       import PySide.QtGui as QtGui
+else:
+       import PyQt4.QtCore as QtCore
+       import PyQt4.QtGui as QtGui
+
+
+_moduleLogger = logging.getLogger(__name__)
+
+
+class Signaller(QtCore.QObject):
+
+       if PYSIDE:
+               s1 = QtCore.Signal()
+               s2 = QtCore.Signal()
+       else:
+               s1 = QtCore.pyqtSignal()
+               s2 = QtCore.pyqtSignal()
+
+
+class Window(object):
+
+       def __init__(self, s):
+               self._window = QtGui.QMainWindow()
+               self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
+               self._window.setWindowTitle("Demo!")
+               if DISCONNECT_ON_DELETE:
+                       self._window.destroyed.connect(self._on_destroyed)
+
+               self._s = s
+               self._s.s1.connect(self._on_signal)
+               self._s.s2.connect(self._on_signal)
+
+       def show(self):
+               self._window.show()
+
+       def _on_signal(self):
+               print "Signal!"
+               self._window.setWindowTitle("Signaled!")
+
+       def _on_destroyed(self, obj = None):
+               print "Main window destroyed"
+               self._s.s1.disconnect(self._on_signal)
+               self._s.s2.disconnect(self._on_signal)
+
+
+if __name__ == "__main__":
+       app = QtGui.QApplication([])
+
+       s = Signaller()
+       w = Window(s)
+       w.show()
+
+       val = app.exec_()
+       del w
+
+       print "s1"
+       s.s1.emit()
+       print "s2"
+       s.s2.emit()
+
+       print "Exiting"
+       sys.exit(val)
diff --git a/hand_tests/producer_consumer.py b/hand_tests/producer_consumer.py
new file mode 100755 (executable)
index 0000000..22084ed
--- /dev/null
@@ -0,0 +1,157 @@
+#!/usr/bin/env python
+
+from __future__ import with_statement
+from __future__ import division
+
+import functools
+import time
+
+
+FORCE_PYQT = False
+DECORATE = True
+
+
+try:
+       if FORCE_PYQT:
+               raise ImportError()
+       import PySide.QtCore as _QtCore
+       QtCore = _QtCore
+       USES_PYSIDE = True
+except ImportError:
+       import sip
+       sip.setapi('QString', 2)
+       sip.setapi('QVariant', 2)
+       import PyQt4.QtCore as _QtCore
+       QtCore = _QtCore
+       USES_PYSIDE = False
+
+
+if USES_PYSIDE:
+       Signal = QtCore.Signal
+       Slot = QtCore.Slot
+       Property = QtCore.Property
+else:
+       Signal = QtCore.pyqtSignal
+       Slot = QtCore.pyqtSlot
+       Property = QtCore.pyqtProperty
+
+
+def log_exception():
+
+       def log_exception_decorator(func):
+
+               @functools.wraps(func)
+               def wrapper(*args, **kwds):
+                       try:
+                               return func(*args, **kwds)
+                       except Exception:
+                               print "Exception", func.__name__
+                               raise
+
+               if DECORATE:
+                       return wrapper
+               else:
+                       return func
+
+       return log_exception_decorator
+
+
+class QThread44(QtCore.QThread):
+       """
+       This is to imitate QThread in Qt 4.4+ for when running on older version
+       See http://labs.trolltech.com/blogs/2010/06/17/youre-doing-it-wrong
+       (On Lucid I have Qt 4.7 and this is still an issue)
+       """
+
+       def __init__(self, parent = None):
+               QtCore.QThread.__init__(self, parent)
+
+       def run(self):
+               self.exec_()
+
+
+class Producer(QtCore.QObject):
+
+       data = Signal(int)
+       done = Signal()
+
+       def __init__(self):
+               QtCore.QObject.__init__(self)
+
+       @Slot()
+       @log_exception()
+       def process(self):
+               print "Starting producer"
+               for i in xrange(10):
+                       self.data.emit(i)
+                       time.sleep(0.1)
+               self.done.emit()
+
+
+class Consumer(QtCore.QObject):
+
+       def __init__(self):
+               QtCore.QObject.__init__(self)
+
+       @Slot()
+       @log_exception()
+       def process(self):
+               print "Starting consumer"
+
+       @Slot()
+       @log_exception()
+       def print_done(self):
+               print "Done"
+
+       @Slot(int)
+       @log_exception()
+       def print_data(self, i):
+               print i
+
+
+def run_producer_consumer():
+       app = QtCore.QCoreApplication([])
+
+       producerThread = QThread44()
+       producer = Producer()
+       producer.moveToThread(producerThread)
+       producerThread.started.connect(producer.process)
+
+       consumerThread = QThread44()
+       consumer = Consumer()
+       consumer.moveToThread(consumerThread)
+       consumerThread.started.connect(consumer.process)
+
+       producer.data.connect(consumer.print_data)
+       producer.done.connect(consumer.print_done)
+
+       @Slot()
+       @log_exception()
+       def producer_done():
+               print "Shutting down"
+               producerThread.quit()
+               consumerThread.quit()
+               print "Done"
+       producer.done.connect(producer_done)
+
+       count = [0]
+
+       @Slot()
+       @log_exception()
+       def thread_done():
+               print "Thread done"
+               count[0] += 1
+               if count[0] == 2:
+                       print "Quitting"
+                       app.exit(0)
+               print "Done"
+       producerThread.finished.connect(thread_done)
+       consumerThread.finished.connect(thread_done)
+
+       producerThread.start()
+       consumerThread.start()
+       print "Status %s" % app.exec_()
+
+
+if __name__ == "__main__":
+       run_producer_consumer()
diff --git a/hand_tests/threading.py b/hand_tests/threading.py
deleted file mode 100755 (executable)
index 22084ed..0000000
+++ /dev/null
@@ -1,157 +0,0 @@
-#!/usr/bin/env python
-
-from __future__ import with_statement
-from __future__ import division
-
-import functools
-import time
-
-
-FORCE_PYQT = False
-DECORATE = True
-
-
-try:
-       if FORCE_PYQT:
-               raise ImportError()
-       import PySide.QtCore as _QtCore
-       QtCore = _QtCore
-       USES_PYSIDE = True
-except ImportError:
-       import sip
-       sip.setapi('QString', 2)
-       sip.setapi('QVariant', 2)
-       import PyQt4.QtCore as _QtCore
-       QtCore = _QtCore
-       USES_PYSIDE = False
-
-
-if USES_PYSIDE:
-       Signal = QtCore.Signal
-       Slot = QtCore.Slot
-       Property = QtCore.Property
-else:
-       Signal = QtCore.pyqtSignal
-       Slot = QtCore.pyqtSlot
-       Property = QtCore.pyqtProperty
-
-
-def log_exception():
-
-       def log_exception_decorator(func):
-
-               @functools.wraps(func)
-               def wrapper(*args, **kwds):
-                       try:
-                               return func(*args, **kwds)
-                       except Exception:
-                               print "Exception", func.__name__
-                               raise
-
-               if DECORATE:
-                       return wrapper
-               else:
-                       return func
-
-       return log_exception_decorator
-
-
-class QThread44(QtCore.QThread):
-       """
-       This is to imitate QThread in Qt 4.4+ for when running on older version
-       See http://labs.trolltech.com/blogs/2010/06/17/youre-doing-it-wrong
-       (On Lucid I have Qt 4.7 and this is still an issue)
-       """
-
-       def __init__(self, parent = None):
-               QtCore.QThread.__init__(self, parent)
-
-       def run(self):
-               self.exec_()
-
-
-class Producer(QtCore.QObject):
-
-       data = Signal(int)
-       done = Signal()
-
-       def __init__(self):
-               QtCore.QObject.__init__(self)
-
-       @Slot()
-       @log_exception()
-       def process(self):
-               print "Starting producer"
-               for i in xrange(10):
-                       self.data.emit(i)
-                       time.sleep(0.1)
-               self.done.emit()
-
-
-class Consumer(QtCore.QObject):
-
-       def __init__(self):
-               QtCore.QObject.__init__(self)
-
-       @Slot()
-       @log_exception()
-       def process(self):
-               print "Starting consumer"
-
-       @Slot()
-       @log_exception()
-       def print_done(self):
-               print "Done"
-
-       @Slot(int)
-       @log_exception()
-       def print_data(self, i):
-               print i
-
-
-def run_producer_consumer():
-       app = QtCore.QCoreApplication([])
-
-       producerThread = QThread44()
-       producer = Producer()
-       producer.moveToThread(producerThread)
-       producerThread.started.connect(producer.process)
-
-       consumerThread = QThread44()
-       consumer = Consumer()
-       consumer.moveToThread(consumerThread)
-       consumerThread.started.connect(consumer.process)
-
-       producer.data.connect(consumer.print_data)
-       producer.done.connect(consumer.print_done)
-
-       @Slot()
-       @log_exception()
-       def producer_done():
-               print "Shutting down"
-               producerThread.quit()
-               consumerThread.quit()
-               print "Done"
-       producer.done.connect(producer_done)
-
-       count = [0]
-
-       @Slot()
-       @log_exception()
-       def thread_done():
-               print "Thread done"
-               count[0] += 1
-               if count[0] == 2:
-                       print "Quitting"
-                       app.exit(0)
-               print "Done"
-       producerThread.finished.connect(thread_done)
-       consumerThread.finished.connect(thread_done)
-
-       producerThread.start()
-       consumerThread.start()
-       print "Status %s" % app.exec_()
-
-
-if __name__ == "__main__":
-       run_producer_consumer()