Permalink
Browse files

Added examples and setup.py

  • Loading branch information...
1 parent 32569d4 commit a2ee5fec42a12e6cff2db4d48a273f7a75fb36cc @hgrecco committed Nov 12, 2012
View
0 examples/__init__.py
No changes.
View
69 examples/common.py
@@ -0,0 +1,69 @@
+# -*- coding: utf-8 -*-
+
+import time
+import logging
+
+
+from pizco import Signal
+
+logger = logging.getLogger(__name__)
+logger.setLevel(logging.DEBUG)
+ch = logging.StreamHandler()
+ch.setLevel(logging.INFO)
+logger.addHandler(ch)
+
+COLORS = ('green', 'blue', 'white', 'yellow')
+
+class House(object):
+
+ def __init__(self):
+ logger.info('Welcome to your new house')
+ self._door_open = False
+ self._lights_on = False
+
+ self.door_open_changed = Signal()
+ self.lights_on_changed = Signal()
+
+ self.color_changed = Signal()
+
+ @property
+ def door_open(self):
+ logger.info('Getting door_open')
+ return self._door_open
+
+ @door_open.setter
+ def door_open(self, value):
+ if value not in (True, False):
+ raise ValueError("'{}' is not a valid value for door_open".format(value))
+ logger.info('Setting door_open to {}'.format(value))
+ if self._door_open == value:
+ logger.info('No need to change door_open')
+ return
+
+ self.door_open_changed.emit(value, self._door_open)
+ self._door_open = value
+
+ @property
+ def lights_on(self):
+ return self._lights_on
+
+ @lights_on.setter
+ def lights_on(self, value):
+ if value not in (True, False):
+ raise ValueError("'{}' is not a valid value for lights_on".format(value))
+ logger.info('Setting lights_on to {}'.format(value))
+ if self._lights_on == value:
+ logger.info('No need to change lights_on')
+ return
+ self.lights_on_changed.emit(value, self._lights_on)
+ self._lights_on = value
+
+ def paint(self, color):
+ if color not in COLORS:
+ raise ValueError("'{}' is not a valid color ({})".format(color, COLORS))
+ logger.info('Painting: {}'.format(color))
+ time.sleep(.5)
+ self.color_changed.emit(color)
+ logger.info('Painted: {}'.format(color))
+
+
View
19 examples/exception_handling.py
@@ -0,0 +1,19 @@
+# -*- coding: utf-8 -*-
+from __future__ import print_function
+
+import sys
+import time
+
+if sys.version_info < (3,0):
+ input = raw_input
+
+from pizco import Proxy
+
+proxy = Proxy('tcp://127.0.0.1:8000')
+
+while True:
+ try:
+ proxy.paint(input('New color for the house: '))
+ except ValueError as ex:
+ print('Oops!')
+ print(ex)
View
101 examples/gui_client.py
@@ -0,0 +1,101 @@
+# -*- coding: utf-8 -*-
+
+import sys
+from PyQt4 import QtCore, QtGui
+from PyQt4.QtCore import QSize
+from PyQt4.QtGui import QCheckBox, QMainWindow, QVBoxLayout, QHBoxLayout, QLayout, QLabel, QLineEdit, QWidget
+
+from pizco import Proxy
+
+
+class ControlForm(QtGui.QDialog):
+
+ def __init__(self, proxy, parent=None):
+ QtGui.QDialog.__init__(self, parent)
+ self.setupUi(self)
+
+ self.proxy = proxy
+ self.proxy.lights_on_changed.connect(self.object_lights_on_changed)
+ self.proxy.door_open_changed.connect(self.object_door_open_changed)
+ self.proxy.color_changed.connect(self.object_color_changed)
+
+ def on_lights_on_changed(self, value):
+ proxy.lights_on = self.lights.isChecked()
+
+ def on_door_open_changed(self, value):
+ proxy.door_open = self.door_open.isChecked()
+
+ def object_lights_on_changed(self, value, old_value, other):
+ if value == self.lights.isChecked():
+ return
+ self.lights.setChecked(value)
+
+ def object_door_open_changed(self, value, old_value, other):
+ if value == self.door_open.isChecked():
+ return
+ self.door_open.setChecked(value)
+
+ def object_color_changed(self, value, old_value, other):
+ if value == self.color.text():
+ return
+ self.color.setText(value)
+ self.color.textChanged.emit(value)
+
+ def on_color_text_changed(self, value):
+ self.color_box.setStyleSheet("QLabel { background-color: %s }" % value)
+
+ def setupUi(self, parent):
+ self.resize(275, 172)
+ self.setWindowTitle('House')
+
+ self.layout = QVBoxLayout(parent)
+ self.layout.setSizeConstraint(QLayout.SetFixedSize)
+ #align = (Qt.AlignRight | Qt.AlignTrailing | Qt.AlignVCenter)
+
+ self.layout1 = QHBoxLayout()
+ self.label1 = QLabel()
+ self.label1.setMinimumSize(QSize(100, 0))
+ self.label1.setText('Lights:')
+ self.lights = QCheckBox()
+ self.lights.setTristate(False)
+ self.layout1.addWidget(self.label1)
+ self.layout1.addWidget(self.lights)
+
+ self.layout2 = QHBoxLayout()
+ self.label2 = QLabel()
+ self.label2.setMinimumSize(QSize(100, 0))
+ self.label2.setText('Front door:')
+ self.door_open = QCheckBox()
+ self.door_open.setTristate(False)
+ self.layout2.addWidget(self.label2)
+ self.layout2.addWidget(self.door_open)
+
+ self.layout3 = QHBoxLayout()
+ self.label3 = QLabel()
+ self.label3.setMinimumSize(QSize(100, 0))
+ self.label3.setText('Color:')
+ self.color = QLineEdit()
+ self.color.setReadOnly(True)
+ self.color.textChanged.connect(self.on_color_text_changed)
+ self.color_box = QLabel()
+ self.color_box.setText(' ')
+ self.layout3.addWidget(self.label3)
+ self.layout3.addWidget(self.color)
+ self.layout3.addWidget(self.color_box)
+
+ self.lights.stateChanged.connect(self.on_lights_on_changed)
+ self.door_open.stateChanged.connect(self.on_door_open_changed)
+
+ self.layout.addLayout(self.layout1)
+ self.layout.addLayout(self.layout2)
+ self.layout.addLayout(self.layout3)
+
+
+if __name__ == "__main__":
+ proxy = Proxy('tcp://127.0.0.1:8000')
+ app = QtGui.QApplication(sys.argv)
+ main = ControlForm(proxy)
+ main.show()
+ if sys.platform.startswith('darwin'):
+ main.raise_()
+ sys.exit(app.exec_())
View
10 examples/house_server.py
@@ -0,0 +1,10 @@
+# -*- coding: utf-8 -*-
+
+from pizco import Server
+
+from common import House
+
+s = Server(House(), 'tcp://127.0.0.1:8000')
+
+s.serve_forever()
+
View
29 examples/observing_client.py
@@ -0,0 +1,29 @@
+# -*- coding: utf-8 -*-
+
+import time
+
+from pizco import Proxy
+
+def on_lights_on(value, old_value, others):
+ print('The lights_on has been changed from {} to {}'.format(old_value, value))
+
+def on_door_open(value, old_value, others):
+ print('The front door has been changed from {} to {}'.format(old_value, value))
+
+def on_color_change(value, old_value, others):
+ print('The color has been changed from {} to {}'.format(old_value, value))
+
+proxy = Proxy('tcp://127.0.0.1:8000')
+
+connected = False
+while True:
+ input('Press enter to {} ...\n'.format('disconnect' if connected else 'connect'))
+ if connected:
+ proxy.lights_on_changed.disconnect(on_lights_on)
+ proxy.door_open_changed.disconnect(on_door_open)
+ proxy.color_changed.disconnect(on_color_change)
+ else:
+ proxy.lights_on_changed.connect(on_lights_on)
+ proxy.door_open_changed.connect(on_door_open)
+ proxy.color_changed.connect(on_color_change)
+ connected = not connected
View
26 examples/remote_start_server.py
@@ -0,0 +1,26 @@
+# -*- coding: utf-8 -*-
+
+import time
+
+from pizco import Server
+
+from common import House
+
+proxy = Server.serve_in_process(House, (), {}, 'tcp://127.0.0.1:8000')
+
+time.sleep(1)
+
+proxy.door_open = True
+proxy.lights_on = True
+time.sleep(.1)
+proxy.paint('green')
+proxy.lights_on = False
+proxy.door_open = False
+
+for step in range(3, 0, -1):
+ time.sleep(1)
+ print('Stopping in {}'.format(step))
+
+proxy._proxy_stop_server()
+
+print('Bye!')
View
23 examples/simple_client.py
@@ -0,0 +1,23 @@
+# -*- coding: utf-8 -*-
+
+import sys
+import time
+import random
+
+if sys.version_info < (3,0):
+ input = raw_input
+
+from pizco import Proxy
+
+proxy = Proxy('tcp://127.0.0.1:8000')
+
+colors = ('green', 'blue', 'white', 'yellow')
+
+while True:
+ input('Press enter to run ...\n')
+ proxy.door_open = True
+ proxy.lights_on = True
+ time.sleep(.1)
+ proxy.paint(random.choice(colors))
+ proxy.lights_on = False
+ proxy.door_open = False
View
38 setup.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python
+
+from distutils.core import setup
+
+with open('README') as file:
+ long_description = file.read()
+
+setup(
+ name='Pizco',
+ version='0.1',
+ description='Python remote objects with ZMQ',
+ long_description=long_description,
+ author='Hernan E. Grecco',
+ author_email='hernan.grecco@gmail.com',
+ url='https://github.com/hgrecco/pizco',
+ packages=['pizco'],
+ package_data={},
+ extras_require = {
+ 'pyzmq': ['pyzmq'],
+ },
+ license='BSD',
+ classifiers=[
+ 'Development Status :: 4 - Beta',
+ 'Intended Audience :: Developers',
+ 'License :: OSI Approved :: BSD License',
+ 'Operating System :: MacOS :: MacOS X',
+ 'Operating System :: Microsoft :: Windows',
+ 'Operating System :: POSIX',
+ 'Programming Language :: Python',
+ 'Topic :: Scientific/Engineering',
+ 'Topic :: Software Development :: Libraries',
+ 'Programming Language :: Python :: 2.6',
+ 'Programming Language :: Python :: 2.7',
+ 'Programming Language :: Python :: 3.0',
+ 'Programming Language :: Python :: 3.1',
+ 'Programming Language :: Python :: 3.2',
+ 'Programming Language :: Python :: 3.3',
+ ])

0 comments on commit a2ee5fe

Please sign in to comment.