Example #1
0
 def test_remove_listener_with_invalid_event_failed(self):
     mediator = Mediator()
     listener = Listener()
     with self.assertRaises(TypeError) as context:
         mediator.remove_listener(1, listener)
     self.assertEqual(str(context.exception),
                      'Expects subclass of Event or str')
Example #2
0
 def test_add_listener_with_string(self):
     mediator = Mediator()
     listener = Listener()
     mediator.add_listener('Event', listener)
     event = Event()
     event = mediator.dispatch(event)
     self.assertEqual(len(listener.events), 1)
     self.assertIs(listener.events[0], event)
Example #3
0
 def test_add_listener_with_string(self):
     mediator = Mediator()
     listener = Listener()
     mediator.add_listener('Event', listener)
     event = Event()
     event = mediator.dispatch(event)
     self.assertEqual(len(listener.events), 1)
     self.assertIs(listener.events[0], event)
Example #4
0
    def test_event__load(self):
        '''
        Check that the load event gets called
        '''
        Mediator._load = mock.MagicMock()

        Mediator.load()

        Mediator._load.assert_called_once_with()
Example #5
0
class Executer():
    def __init__(self, queue):
        self.receiver = Queue()
        self.sender = Queue()
        self.mediator = Mediator(self.receiver, self.sender)
        self.mediator.start()
        rospy.init_node('executer', anonymous=False)
        rospy.on_shutdown(self.shutdown)
        self.rate = rospy.Rate(5)
        self.commander = rospy.Publisher('cmd_vel_mux/input/navi',
                                         Twist,
                                         queue_size=10)
        self.bumper = rospy.Subscriber('mobile_base/events/bumper',
                                       BumperEvent, self.processBump)

    def processBump(self, data):
        if (data.state == BumperEvent.PRESSED):
            package = {
                'type': 'bumper',
            }
            if data.bumper == 1:
                package['value'] = 'front'
            elif data.bumper == 0:
                package['value'] = 'left'
            elif data.bumper == 2:
                package['value'] = 'right'
            sender.put(package)
        else:
            rospy.loginfo("Bumper Event")
            rospy.loginfo(data.bumper)

    def run(self):

        twist = Twist()

        while not rospy.is_shutdown():

            if not self.receiver.empty():
                command = self.receiver.get()
                twist = Twist()
                if command['type'] == 'straight':
                    twist.linear.x = command['value']
                if command['type'] == 'turn':
                    twist.linear.x = 0
                    twist.angular.z = radians(command['value'])
                print(command)

            self.commander.publish(twist)
            self.rate.sleep()

    def shutdown(self):
        # stop turtlebot
        rospy.loginfo("Stopping")
        self.commander.publish(Twist())
        rospy.sleep(1)
Example #6
0
 def __init__(self, queue):
     self.receiver = Queue()
     self.sender = Queue()
     self.mediator = Mediator(self.receiver, self.sender)
     self.mediator.start()
     rospy.init_node('executer', anonymous=False)
     rospy.on_shutdown(self.shutdown)
     self.rate = rospy.Rate(5)
     self.commander = rospy.Publisher('cmd_vel_mux/input/navi',
                                      Twist,
                                      queue_size=10)
     self.bumper = rospy.Subscriber('mobile_base/events/bumper',
                                    BumperEvent, self.processBump)
Example #7
0
 def test_event_decorator(self):
     mediator = Mediator()
     event = stubs.VenusianEvent()
     mediator.dispatch(event)
     self.assertFalse(event.success)
     mediator.scan(package=stubs)
     mediator.dispatch(event)
     self.assertTrue(event.success)
Example #8
0
def createAll():
    global valves
    name = None
    control = None
    oscurl = None
    ctrl = None
    color = None
    instrGroup = None
    if context.isSensorPositive():
        context.updateContext()
        for name, group in config.groups.items():
            instrGroup = name
            print("*****************", group)
            for instrument in group['instruments']:
                name = instrument
                for control in group['controls']:
                    control = control
                    oscurl = os.path.join("/", name, control)
                    print(oscurl)
                    if "valve" in control:
                        print("creating valve ", control)
                        ctrl = "valveController"
                        color = colors[0]
                    elif "speed" in control:
                        print("creating speed ", control)
                        ctrl = "speedController"
                        color = colors[1]
                    elif "roller" in control:
                        print("creating speed ", control)
                        ctrl = "rollerController"
                        color = colors[1]
                    else:
                        print("creating controller ", control)
                        ctrl = "otherController"
                        color = colors[2]
                    med = Mediator(context.scene.addObject(ctrl, "Floor"))
                    med.oscurl = oscurl
                    med.id = name
                    med.control = control
                    med.group = instrGroup
                    med.stopDynamics()
                    # med.worldPosition = [(random() * 10) -5, (random() * 5) + 5, 1]
                    med.worldPosition.x = (random() * 20) -10
                    med.worldPosition.y = (random() * 20) -10
                    stackInstruments(med)
                    med.localScale = [0.5, 0.5, 0.5 + (random())]
                    med.color = color
                    controls.append(med)
Example #9
0
    def callback(message):
        # pull() blocks until a message is received
        print("in callback")
        data = message.data
        print(data)
        #msg_string = base64.b64decode(data)
        #print("msg_string",msg_string)
        msg_data = json.loads(data)
        #print("msg_data",msg_data)
        content_type = msg_data["contentType"]
        print("content_type", content_type)

        attributes = message.attributes
        print("attributes type: ", type(attributes))
        event_type = attributes['eventType']
        bucket_id = attributes['bucketId']
        object_id = attributes['objectId']
        generation = attributes['objectGeneration']
        #[END msg_format]

        print("{0} process starts".format(object_id))
        start_process = datetime.datetime.now()

        # <Your custom process>
        print("checking event_type: ", event_type)
        if event_type == 'OBJECT_FINALIZE':
            print("Instantiating Mediator")
            m = Mediator(bucket_id, object_id, content_type, PROJECT_ID,
                         dataset_id, table_id)
            print("Calling Speech to text")
            m.speech_to_text()
        # <End of your custom process>

        end_process = datetime.datetime.now()
        print("{0} process stops".format(object_id))

        # Write logs only if needed for analytics or debugging
        print(
            "{media_url} processed by instance {instance_hostname} in {amount_time}"
            .format(media_url=data,
                    instance_hostname=INSTANCE_NAME,
                    amount_time=str(end_process - start_process)))

        message.ack()
Example #10
0
 def test_event_decorator(self):
     mediator = Mediator()
     event = stubs.VenusianEvent()
     mediator.dispatch(event)
     self.assertFalse(event.success)
     mediator.scan(package=stubs)
     mediator.dispatch(event)
     self.assertTrue(event.success)
def buildMenu(mediator: Mediator, size_hint=None):
    menuDict = {
        'Jouw hand': lambda: mediator.showSpecification(),
        'Biedingen': lambda: mediator.showBiddingChooser(),
        'Over BidBud': lambda: mediator.showCredits()
    }
    widgetSizeHint = (1.0, 1.0) if size_hint == None else size_hint
    widget = Spinner(text='Menu', size_hint=widgetSizeHint)
    widget.values = list(menuDict.keys())
    widget.bind(text=lambda i, v: menuDict[v]())

    def resizeMenu(i, isOpen):
        if isOpen:
            widget.size_hint_x = None
            widget.width = Window.width / 2
        else:
            widget.size_hint = widgetSizeHint

    widget.bind(is_open=resizeMenu)

    return widget
Example #12
0
    def addContentTypes(self, app, template, content_types, default=None):
        """
        Associate a WSGI app and a template with one or more content-types
        """
        try:
            mediator = self.templates[template].app
        except KeyError:
            from mediator import Mediator
            # create a Mediator inside a normalised environment
            mediator = Mediator(check_response=False)
            self.templates[template] = EnvironNormalise(mediator)

        # Add the specified content types, wrapping them in middleware
        # that adds the content-type header.
        for content_type in content_types:
            wrapp = self.contentWrapper(app, content_type+';charset=utf-8')
            mediator.add(content_type, wrapp)

        # Add the default content type
        if default:
            wrapp = self.contentWrapper(app, default+';charset=utf-8')
            mediator.add('*/*', wrapp)
Example #13
0
    def __init__(self, parent=None):

        #
        QtGui.QWidget.__init__(self, parent)

        #		from PyQt4 import QString
        self.ui = Ui_Form()
        self.ui.setupUi(self, [
            (_('Reset'), self.reset),
            (_('Measuring mode'), self.setMeasuringMode),
            (_('Distance to measure in X axis'), self.askWantedX),
            (_('Distance to measure in Y axis'), self.askWantedY),
            (_('Calibrate Y axis'), self.calY),
            (_('Calibrate X axis'), self.calX),
            (_('Finish calibration'), self.finishCal),
            (_('Save calibration'), self.saveRatio),
            (_('Allow calibration'), self.allowCalibrate),
        ])

        print 'main widget: ', self.centralWidget()
        self.centralWidget().setMouseTracking(True)
        # mode of working
        self.mode = Mode(self, ("MEASURING", "CAL_X", "CAL_Y"))

        # storing programs options
        self.options = Options()

        # object that manages all other objects :)
        self.mediator = Mediator(self.cursor(), self)

        # distance the user wants the device to travel
        self.wantedX, self.wantedY = 0.0, 0.0

        self.setMeasuringMode()
        # set default values
        self.reset()
Example #14
0
def createMediator():
    if context.isSensorPositive():
        context.updateContext()
        for valve in config.valves:
            print(" ** Creating ", valve)
            med = Mediator(context.scene.addObject("valveController", "Floor"))
            med.oscurl = valve
            med.suspendDynamics()
            # med.worldPosition = [(random() * 10) -5, (random() * 5) + 5, 1]
            med.worldPosition.x = (random() * 20) -10
            med.worldPosition.y = (random() * 20) -10
            med.localScale = [0.7, 0.7, 0.7 + (random() * 3)]
            controls.append(med)
Example #15
0
	def __init__(self, parent=None):

		# 
		QtGui.QWidget.__init__(self, parent)


		
#		from PyQt4 import QString
		self.ui = Ui_Form()
		self.ui.setupUi(self, [  (_('Reset'), self.reset),
					 (_('Measuring mode'), self.setMeasuringMode),
					 (_('Distance to measure in X axis'), self.askWantedX),
					 (_('Distance to measure in Y axis'), self.askWantedY),
					 (_('Calibrate Y axis'), self.calY),
					 (_('Calibrate X axis'), self.calX),
					 (_('Finish calibration'), self.finishCal),
					 (_('Save calibration'), self.saveRatio),
					 (_('Allow calibration'), self.allowCalibrate),
					])


		
		print 'main widget: ',self.centralWidget()
		self.centralWidget().setMouseTracking(True)
		# mode of working
		self.mode = Mode(self, ("MEASURING", "CAL_X", "CAL_Y"))

		# storing programs options
		self.options = Options()

		# object that manages all other objects :)
		self.mediator = Mediator(self.cursor(), self)




		# distance the user wants the device to travel
		self.wantedX, self.wantedY = 0.0, 0.0

		self.setMeasuringMode()
		# set default values
		self.reset()
Example #16
0
    def build(self):
        mediator = Mediator(self.switchTo)
        for screen in [
                FileChooserScreen(mediator, name=fileChooserScreen),
                BiddingScreen(mediator, name=biddingScreen),
                SpecificationScreen(mediator, name=specificationScreen),
                AdviceScreen(mediator, name=adviceScreen),
                CreditsScreen(mediator, name=creditsScreen),
                BlankScreen(mediator, name=blankScreen)
        ]:
            self.sm.add_widget(screen)

            def createCallback(screen):
                def cb(instance):
                    screen.onDisplay()

                return cb

            screen.bind(on_pre_enter=createCallback(screen))
        return self.sm
Example #17
0
    def addContentTypes(self, app, template, content_types, default=None):
        """
        Associate a WSGI app and a template with one or more content-types
        """
        try:
            mediator = self.templates[template].app
        except KeyError:
            from mediator import Mediator
            # create a Mediator inside a normalised environment
            mediator = Mediator(check_response=False)
            self.templates[template] = EnvironNormalise(mediator)

        # Add the specified content types, wrapping them in middleware
        # that adds the content-type header.
        for content_type in content_types:
            wrapp = self.contentWrapper(app, content_type + ';charset=utf-8')
            mediator.add(content_type, wrapp)

        # Add the default content type
        if default:
            wrapp = self.contentWrapper(app, default + ';charset=utf-8')
            mediator.add('*/*', wrapp)
Example #18
0
def main(toprocess, subscription, refresh, dataset_id, table_id):
    sub = "projects/{0}/subscriptions/{1}".format(PROJECT_ID, subscription)

    r = Recurror(refresh - 10, postpone_ack)

    # pull() blocks until a message is received
    while True:
        #[START sub_pull]
        # Pull the data when available.
        resp = pubsub_client.projects().subscriptions().pull(
            subscription=sub,
            body={
                "maxMessages": toprocess
            }
        ).execute()
        #[END sub_pull]

        if resp:
            # Get the amount of media that one instance can processed
            # For this demo, we keep it to one per instance.
            m = resp.get('receivedMessages')[0]
            if m:
                message = m.get('message')
                ack_id = m.get('ackId')
                msg_string = base64.b64decode(message.get('data'))
                msg_data = json.loads(msg_string)
                bucket = msg_data["bucket"]
                filename = msg_data["name"]
                filetype = msg_data["type"]
                fn = filename.split('.')[0]

                # Start refreshing the acknowledge deadline.
                r.start(ack_ids=[ack_id], refresh=refresh, sub=sub)

                Logger.log_writer("{0} process starts".format(filename))
                start_process = datetime.datetime.now()

# <Your custom process>
                m = Mediator(bucket, filename, filetype, PROJECT_ID, dataset_id, table_id)
                m.speech_to_text()
# <End of your custom process>

                end_process = datetime.datetime.now()
                Logger.log_writer("{0} process stops".format(filename))

                
                #[START ack_msg]
                # Delete the message in the queue by acknowledging it.
                pubsub_client.projects().subscriptions().acknowledge(
                    subscription=sub,
                    body={
                        'ackIds': [ack_id]
                    }
                ).execute()
                #[END ack_msg]

                # Logs to see what's going on.
                Logger.log_writer(
                    "{media_url} processed by instance {instance_hostname} in {amount_time}"
                    .format(
                        media_url=msg_string,
                        instance_hostname=INSTANCE_NAME,
                        amount_time=str(end_process - start_process)
                    )
                )

                # Stop the ackDeadLine refresh until next message.
                r.stop()
Example #19
0
from mediator import Mediator
from multiprocessing import Queue

receive = Queue()
send = Queue()
medi = Mediator(receive, send)
medi.start()

while True:
    phrase = input()
    send.put({'text': phrase})
Example #20
0
    def test_add_subscriber(self):
        mediator = Mediator()

        with self.assertRaises(TypeError) as context:
            mediator.add_subscriber(object())
        self.assertEqual(str(context.exception),
                         'Expects instance of SubscriberInterface')

        subscriber = Subscriber()
        mediator.add_subscriber(subscriber)

        event = EventOne()
        mediator.dispatch(event)
        self.assertEqual(len(subscriber.results), 3)
        events = [result[0] for result in subscriber.results.values()]
        self.assertIs(events[0], events[1])
        self.assertIs(events[1], events[2])
        first, middle, last = (subscriber.results['first'][1],
                               subscriber.results['middle'][1],
                               subscriber.results['last'][1])
        self.assertTrue(first < middle < last,
                        '%r %r %r' % (first, middle, last))

        event = EventTwo()
        mediator.dispatch(event)
        self.assertIs(subscriber.results['another'], event)

        event = EventThree()
        mediator.dispatch(event)
        self.assertIs(subscriber.results['even_more'], event)

        with self.assertRaises(ValueError):
            mediator.add_subscriber(BadSubscriberOne())

        with self.assertRaises(ValueError):
            mediator.add_subscriber(BadSubscriberTwo())
Example #21
0
 def test_dispatch_failed(self):
     meditator = Mediator()
     with self.assertRaises(TypeError) as context:
         meditator.dispatch('unexpected_event')
     self.assertEqual(str(context.exception), 'Expects instance of Event')
Example #22
0
 def test_remove_listener_with_invalid_event_failed(self):
     mediator = Mediator()
     listener = Listener()
     with self.assertRaises(TypeError) as context:
         mediator.remove_listener(1, listener)
     self.assertEqual(str(context.exception), 'Expects subclass of Event or str')
Example #23
0
def main(toprocess, subscription, refresh, dataset_id, table_id):
    """
    """
    subscription_id = "projects/{0}/subscriptions/{1}".format(PROJECT_ID, subscription)
    subscription = pubsub.subscription.Subscription(subscription_id, client=pubsub_client)

    if not subscription.exists():
        sys.stderr.write('Cannot find subscription {0}\n'.format(sys.argv[1]))
        return

    r = Recurror(refresh - 10, postpone_ack)

    # pull() blocks until a message is received
    while True:
        #[START sub_pull]
        resp = subscriptions.pull("maxMessages": toprocess)
        #[END sub_pull]

        for ack_id, message in resp:
            # We need to do this to get contentType. The rest is in attributes
            #[START msg_format]
            data = message.data
            msg_string = base64.b64decode(data)
            msg_data = json.loads(msg_string)
            content_type = msg_data["contentType"]

            attributes = message.attributes
            event_type = attributes['eventType']
            bucket_id = attributes['bucketId']
            object_id = attributes['objectId']
            generation = attributes['objectGeneration']
            #[END msg_format]

            # Start refreshing the acknowledge deadline.
            r.start(ack_ids=[ack_id], refresh=refresh, sub=sub)

            Logger.log_writer("{0} process starts".format(object_id))
            start_process = datetime.datetime.now()

    # <Your custom process>
            if event_type == 'OBJECT_FINALIZE':
                m = Mediator(bucket_id, object_id, content_type, PROJECT_ID, dataset_id, table_id)
                m.speech_to_text()
    # <End of your custom process>

            end_process = datetime.datetime.now()
            Logger.log_writer("{0} process stops".format(object_id))

            #[START ack_msg]
            # Delete the message in the queue by acknowledging it.
            subscription.acknowledge([ack_id])
            #[END ack_msg]

            # Write logs only if needed for analytics or debugging
            Logger.log_writer(
                "{media_url} processed by instance {instance_hostname} in {amount_time}"
                .format(
                    media_url=msg_string,
                    instance_hostname=INSTANCE_NAME,
                    amount_time=str(end_process - start_process)
                )
            )

            # Stop the ackDeadLine refresh until next message.
            r.stop()
Example #24
0
def populateControls(family, array):
    name = None
    control = None
    oscurl = None
    ctrl = None
    color = None
    instrGroup = None
    controls = array
    if context.isSensorPositive():
        context.updateContext()
        for name, group in config.groups.items():
            if family in name:
                instrGroup = name
                for instrument in group["instruments"]:
                    name = instrument
                    print("--> instrument", instrument)
                    for control in group["controls"]:
                        print("  --> control: ", control)
                        control = control
                        oscurl = os.path.join("/", name, control)
                        if "valve" in control:
                            if 'Pipe' in oscurl:
                                ctrl = "pipeValve"
                                color = colors[0]
                            else:
                                print("creating valve ", control)
                                ctrl = "valveController"
                                color = colors[0]
                        elif "speed" in control:
                            print("creating speed ", control)
                            ctrl = "speedController"
                            color = colors[1]
                        elif "roller" in control:
                            print("creating roller ", control)
                            ctrl = "rollerController"
                            color = colors[1]
                        elif "length" in control:
                            print("creating roller ", control)
                            ctrl = "lengthController"
                            color = colors[2]
                        elif "dur" in control:
                            print("creating roller ", control)
                            ctrl = "lengthController"
                            color = colors[1]
                        else:
                            print("creating controller ", control)
                            ctrl = "otherController"
                            color = colors[2]
                        med = Mediator(context.scene.addObject(ctrl, name))
                        med.oscurl = oscurl
                        med.id = name
                        med.control = control
                        med.group = instrGroup
                        med.stopDynamics()
                        # med.worldPosition = [(random() * 10) -5, (random() * 5) + 5, 1]
                        #med.worldPosition.x = (random() * 20) -10
                        #med.worldPosition.y = (random() * 20) -10
                        stackInstruments(med)
                        med.localScale = [0.5, 0.5, 0.5 + (random())]
                        med.color = color
                        med.setStartingPosition(context.scene.objects[name].worldPosition)
                        controls.append(med)
Example #25
0
class MyForm(QtGui.QMainWindow):
    def modeChanged(self, mode):
        self.showInfoMessage(mode)
        pass

    def setMeasuringMode(self):
        self.mode.setMode("MEASURING")
        for i in (4, 5, 6, 7):
            self.ui.menu.deactivate(i)
        self.calibration = None
        self.reset()

    def showInfoMessage(self, mes):
        u"""shows info message"""
        QtGui.QMessageBox.information(self, programName, mes,
                                      QtGui.QMessageBox.Ok)

    def setDistance(self, distance):
        u"""called by the mediator when the distance changes
		updates fields distX and distY
		calls updateUi"""
        self.distX, self.distY = distance[0], distance[1]
        self.updateUi()

    def updateUi(self):
        u"""updates UI based on distX and distY values"""

        # global distance ran over detector in both axis
        formatedDistanceX, formatedDistanceY = "%.3F" % self.distX, "%.3F" % self.distY

        # natively show the distance ran over the detector
        textX, textY = formatedDistanceX, formatedDistanceY

        # update info about
        if self.mode.getMode() == "MEASURING":
            pass

        # the calibrated axis doesn't show the global way ran
        elif self.mode.getMode() == "CAL_X":
            textX = _("calibration")
        elif self.mode.getMode() == "CAL_Y":
            textY = _("calibration")

        self.textX = "<font color=red size=4><b>" + textX + "</b></font>"

        # fill the data into labels

        from PyQt4 import Qt
        # showing total distance ran over cursor
        self.ui.lbWayX.setText(textX)
        self.ui.lbWayY.setText(textY)

        # calculate the remaining distance and show it
        remainingX = self.wantedX - self.distX
        remainingY = self.wantedY - self.distY
        self.ui.lbRemainingX.setText("%3.3F" % remainingX)
        self.ui.lbRemainingY.setText("%3.3F" % remainingY)

        # show the distance user wants to measure
        self.ui.lbWantedX.setText("%3.3F" % self.wantedX)
        self.ui.lbWantedY.setText("%3.3F" % self.wantedY)

        # show info about current working mode
        self.ui.lbMode.setText(str(self.mode.getMode()))

    def __init__(self, parent=None):

        #
        QtGui.QWidget.__init__(self, parent)

        #		from PyQt4 import QString
        self.ui = Ui_Form()
        self.ui.setupUi(self, [
            (_('Reset'), self.reset),
            (_('Measuring mode'), self.setMeasuringMode),
            (_('Distance to measure in X axis'), self.askWantedX),
            (_('Distance to measure in Y axis'), self.askWantedY),
            (_('Calibrate Y axis'), self.calY),
            (_('Calibrate X axis'), self.calX),
            (_('Finish calibration'), self.finishCal),
            (_('Save calibration'), self.saveRatio),
            (_('Allow calibration'), self.allowCalibrate),
        ])

        print 'main widget: ', self.centralWidget()
        self.centralWidget().setMouseTracking(True)
        # mode of working
        self.mode = Mode(self, ("MEASURING", "CAL_X", "CAL_Y"))

        # storing programs options
        self.options = Options()

        # object that manages all other objects :)
        self.mediator = Mediator(self.cursor(), self)

        # distance the user wants the device to travel
        self.wantedX, self.wantedY = 0.0, 0.0

        self.setMeasuringMode()
        # set default values
        self.reset()

    def reset(self):
        u"""resets ui and mediator, loads ratio from file (if was calibrated at runtime and not saved, then calibration data will
		be lost, sets distance to 0"""

        # distance travelled by the input device in the measurements units (defalutly milimeters)
        self.distX, self.distY = 0.0, 0.0

        self.mediator.reset()
        self.mediator.storeRatio(self.options.getRatio())

        self.updateUi()

    def askWanted(self, axis):
        text, ok = QtGui.QInputDialog.getText(
            self, programName,
            _('Distance to measure in %s axis:') % axis)
        name = {'X': 'wantedX', 'Y': 'wantedY'}
        try:
            # force converting to float
            text = "%s.0" % text
            distance = float(eval(text))
            setattr(self, name[axis], distance)
        except:
            self.showInfoMessage(
                _("You entered inproper number. You can use arithmetic operators (+, -, * /)"
                  ))
        self.updateUi()

    def askWantedX(self):
        self.askWanted('X')

    def askWantedY(self):
        self.askWanted('Y')

    def allowCalibrate(self):
        text, ok = QtGui.QInputDialog.getText(
            self, programName,
            _("""Enter below "SUWMIARKA". 
It is protection against accidentally changing of the calibration configuration.
Entering word other than "SUWMIARKA" or pressing Cancel key simply returns to the mail menu and doesn't allow calibration.
If you choose to allow calibrating, and then with any cause you would like to turn off allowing calibration, choose option """
              ) + _('Measuring mode'))
        if ok:
            if text == "SUWMIARKA":
                for i in (4, 5):
                    self.ui.menu.activate(i)

    def saveRatio(self):
        u"""saves ratio to a file"""
        try:
            self.options.saveRatio()
        # if some problems occurred, show the info message
        except:
            self.showInfoMessage(
                _("Error during saving calibration data to file."))
        # else show message that everything was ok
        else:
            self.showInfoMessage(_("Calibration data saved correctly"))

    def finishCal(self):
        u"""calculates the ratio of the calibrated axis, saves it to the options object (not to a file!!)"""
        if self.calibration == None:
            self.showInfoMessage("Blad programu")
        elif self.calibration in ('X', 'Y'):

            #TODO: tu jest odwolanie do mediatora, koniecznie ustaw odpowiednie metody !!
            if self.calibration == 'X':
                przesuniecie = self.mediator.coordCounter.ticksX
            else:
                przesuniecie = self.mediator.coordCounter.ticksY

            self.showInfoMessage(
                _("General distance in axis %s: %s" %
                  (self.calibration, przesuniecie)))

            text, ok = QtGui.QInputDialog.getText(
                self, programName,
                _('Enter distance the input device has moved: '))
            if ok:
                calc = przesuniecie / float(text)
                if self.calibration == 'X': self.options.xRatio = calc
                elif self.calibration == 'Y': self.options.yRatio = calc
            self.showInfoMessage(
                _("Ticks on milimeter in axis ") + self.calibration +
                _(" is: %s") % calc)

        self.calibration = None
        self.setMeasuringMode()
        self.ui.menu.activate(7)

    def cal(self, axis):
        u"""starts calibrating an axis"""
        self.ui.menu.activate(6)
        reply = QtGui.QMessageBox.information(
            self, programName,
            _("Starting calibration of axis: %s") % axis, QtGui.QMessageBox.Ok)
        if reply == QtGui.QMessageBox.Ok:
            self.reset()
            self.calibration = axis

    def calX(self):
        self.mode.setMode("CAL_X")
        self.cal('X')

    def calY(self):
        self.mode.setMode("CAL_Y")
        self.cal('Y')

    def keyPressEvent(self, event):
        proc = {
            'A': self.ui.menu.keyUp,
            'Z': self.ui.menu.keyDown,
            'K': self.ui.menu.executeMethod
        }
        if type(event) == QtGui.QKeyEvent:
            char = chr(event.key())
            print 'char: ', char
            if char in proc: proc[char]()

    def mouseMoveEvent(self, event):
        try:
            self.mediator.coordChanged()
        except TickException, e:
            QtGui.QMessageBox.information(
                self, "", _("Inproper reading from input device."),
                QtGui.QMessageBox.Ok)
            self.reset()
Example #26
0
    def __init__(self,
                 environment,
                 bundles,
                 configuration_file="config/config.yml",
                 parameters_file="config/parameters.yml"):
        self.logger = None
        self.configuration_file = configuration_file
        self.parameters_file = parameters_file
        self.bundles = bundles
        self.environment = environment
        self.log_handlers = []
        self.is_shutdown = False
        max_workers = cpu_count() * 5
        self.thread_pool = ThreadPoolExecutor(max_workers=max_workers)
        self.running_services = []
        self.mediator = Mediator()

        try:
            self.configuration = self.load_configuration(environment)

            # Subscribe bundle events
            for bundle in self.bundles:
                if hasattr(bundle, 'event_listeners'):
                    for event_type, listener in bundle.event_listeners:
                        self.mediator.add_listener(event=event_type,
                                                   listener=listener)

                if hasattr(bundle, 'event_subscribers'):
                    for subscriber in bundle.event_subscribers:
                        self.mediator.add_subscriber(subscriber=subscriber)

            # Injection provided by the base system
            injection_bindings = {
                Kernel: self,
                Configuration: self.configuration,
                EventManager: self.mediator
            }
            self.mediator.dispatch(ConfigurationReadyEvent(self.configuration))
            # Injection from other bundles
            for bundle in self.bundles:
                if hasattr(bundle, 'injection_bindings'):
                    injection_bindings.update(bundle.injection_bindings)

            # Set this kernel and configuration available for injection
            def my_config(binder):
                for key, value in injection_bindings.items():
                    binder.bind(key, value)

            inject.configure(my_config)
            self.mediator.dispatch(InjectorReadyEvent())

            for bundle in self.bundles:
                if hasattr(bundle, 'log_handlers'):
                    self.log_handlers += bundle.log_handlers
        except Exception as e:
            logging.exception(e)
            raise e

        self.configure_logger(environment=environment)
        self.register_signals()
        logging.info("Kernel Ready")
        self.mediator.dispatch(KernelReadyEvent())
Example #27
0
class Kernel(object):
    def __init__(self,
                 environment,
                 bundles,
                 configuration_file="config/config.yml",
                 parameters_file="config/parameters.yml"):
        self.logger = None
        self.configuration_file = configuration_file
        self.parameters_file = parameters_file
        self.bundles = bundles
        self.environment = environment
        self.log_handlers = []
        self.is_shutdown = False
        max_workers = cpu_count() * 5
        self.thread_pool = ThreadPoolExecutor(max_workers=max_workers)
        self.running_services = []
        self.mediator = Mediator()

        try:
            self.configuration = self.load_configuration(environment)

            # Subscribe bundle events
            for bundle in self.bundles:
                if hasattr(bundle, 'event_listeners'):
                    for event_type, listener in bundle.event_listeners:
                        self.mediator.add_listener(event=event_type,
                                                   listener=listener)

                if hasattr(bundle, 'event_subscribers'):
                    for subscriber in bundle.event_subscribers:
                        self.mediator.add_subscriber(subscriber=subscriber)

            # Injection provided by the base system
            injection_bindings = {
                Kernel: self,
                Configuration: self.configuration,
                EventManager: self.mediator
            }
            self.mediator.dispatch(ConfigurationReadyEvent(self.configuration))
            # Injection from other bundles
            for bundle in self.bundles:
                if hasattr(bundle, 'injection_bindings'):
                    injection_bindings.update(bundle.injection_bindings)

            # Set this kernel and configuration available for injection
            def my_config(binder):
                for key, value in injection_bindings.items():
                    binder.bind(key, value)

            inject.configure(my_config)
            self.mediator.dispatch(InjectorReadyEvent())

            for bundle in self.bundles:
                if hasattr(bundle, 'log_handlers'):
                    self.log_handlers += bundle.log_handlers
        except Exception as e:
            logging.exception(e)
            raise e

        self.configure_logger(environment=environment)
        self.register_signals()
        logging.info("Kernel Ready")
        self.mediator.dispatch(KernelReadyEvent())

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.shutdown()

    def register_signals(self):
        signal.signal(signal.SIGINT, self._signal_handler)
        signal.signal(signal.SIGTERM, self._signal_handler)

    def _signal_handler(self, signal, frame):
        self.shutdown()

    def run_service(self, service_function, *args):
        self.running_services.append(
            self.thread_pool.submit(service_function, *args))

    def configure_logger(self, environment):
        log_level = logging.INFO
        if environment == Environments.DEVELOPMENT:
            # Console output
            ch = logging.StreamHandler()
            ch.setLevel(logging.DEBUG)
            formatter = logging.Formatter(
                '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
            ch.setFormatter(formatter)
            self.log_handlers.append(ch)
            log_level = logging.INFO

        logging.basicConfig(level=log_level, handlers=self.log_handlers)
        logging.info("Logger ready")

    def load_configuration(self, environment):
        config_mappings = [
            bundle.config_mapping for bundle in self.bundles
            if hasattr(bundle, "config_mapping")
        ]
        config = None
        if len(config_mappings) > 0:
            c = YmlLoader()
            config = c.load_config(self.configuration_file,
                                   self.parameters_file)
            try:
                config = c.build_config(config, config_mappings)
            except (NoValueException, NodeIsNotConfiguredException,
                    IgnoredFieldException) as ex:
                print("Configuration error: " + str(ex))
                exit()
        return config

    def shutdown(self):
        if not self.is_shutdown:
            self.is_shutdown = True
            logging.info("Kernel shutting down")
            self.mediator.dispatch(KernelShutdownEvent())
            self.thread_pool.shutdown()
            logging.info("Kernel shutdown")

    def wait(self):
        """Wait for all services to finish"""
        for i in self.running_services:
            i.result()
Example #28
0
 def __init__(self, maze_shape):
     self.mediator = Mediator(maze_shape)
     pygame.mixer.init()
Example #29
0
class GameController:

    playing_time = 0

    def __init__(self, maze_shape):
        self.mediator = Mediator(maze_shape)
        pygame.mixer.init()

    def quitGame(self):
        pygame.quit()

    def showInitialWindow(self):
        self.mediator.game_screen.initialWindow()
        action = Action.stand_by

        pygame.mixer.music.load("sounds/initial_track.mp3")
        pygame.mixer.music.play()

        while action == Action.stand_by:

            if not pygame.mixer.music.get_busy():
                pygame.mixer.music.play()

            mx, my = pygame.mouse.get_pos()

            for event in pygame.event.get():

                if event.type == pygame.QUIT:
                    action = Action.quit_game
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    if (self.mediator.game_screen.size[0] / 2 - 180) <= mx <= (self.mediator.game_screen.size[0] / 2 + 180) and \
                            450 <= my <= 555.5:
                        action = Action.change_screen

            if (self.mediator.game_screen.size[0] / 2 - 180) <= mx <= (self.mediator.game_screen.size[0] / 2 + 180) and \
                    450 <= my <= 555.5:
                self.mediator.game_screen.window.blit(
                    self.mediator.game_screen.pressed_start_button,
                    (self.mediator.game_screen.size[0] / 2 - 180, 450))

            else:
                self.mediator.game_screen.window.blit(
                    self.mediator.game_screen.start_button,
                    (self.mediator.game_screen.size[0] / 2 - 180, 450))

            pygame.display.flip()

        return action

    def playGame(self, maze_shape):
        self.mediator.createMaze(maze_shape)
        player = UserCharacter(3, self.mediator)
        player_list = pygame.sprite.Group()

        red_monster = MonsterCharacter('images/monster1.png',
                                       (1, self.mediator.maze.height - 2), 2,
                                       self.mediator)
        green_monster = MonsterCharacter('images/monster2.png',
                                         (self.mediator.maze.width - 2, 1), 1,
                                         self.mediator)
        ugly_monster = MonsterCharacter('images/monster3.png',
                                        (self.mediator.maze.width - 2, 1), 2,
                                        self.mediator)
        player_list.add(player)
        player_list.add(red_monster)
        player_list.add(green_monster)
        player_list.add(ugly_monster)

        pygame.mixer.music.load("sounds/gameplay_track.mp3")
        pygame.mixer.music.play()

        action_local = Action.stand_by

        start_time = time.time()

        while action_local == Action.stand_by:
            if not pygame.mixer.music.get_busy():
                pygame.mixer.music.play()
            # update maze:
            self.mediator.maze.updateMaze(player.getCharacterRectNode())
            self.mediator.game_screen.showMazeScreen(player_list,
                                                     self.mediator.maze,
                                                     player.lives)
            ParallelThreads.findMonstersNewPosition(red_monster, green_monster,
                                                    ugly_monster, player,
                                                    player_list, self.mediator)

            # Move characters
            keys = pygame.key.get_pressed()
            if keys[pygame.K_LEFT] or keys[ord('a')]:
                player.control(0, -1)
                red_monster.updatePosition()
                green_monster.updatePosition()
                ugly_monster.updatePosition()
                ParallelThreads.findMonstersNewPosition(
                    red_monster, green_monster, ugly_monster, player,
                    player_list, self.mediator)

            elif keys[pygame.K_RIGHT] or keys[ord('d')]:
                player.control(0, 1)
                red_monster.updatePosition()
                green_monster.updatePosition()
                ugly_monster.updatePosition()
                ParallelThreads.findMonstersNewPosition(
                    red_monster, green_monster, ugly_monster, player,
                    player_list, self.mediator)

            elif keys[pygame.K_UP] or keys[ord('w')]:
                player.control(-1, 0)
                red_monster.updatePosition()
                green_monster.updatePosition()
                ugly_monster.updatePosition()
                ParallelThreads.findMonstersNewPosition(
                    red_monster, green_monster, ugly_monster, player,
                    player_list, self.mediator)

            elif keys[pygame.K_DOWN] or keys[ord('s')]:
                player.control(1, 0)
                red_monster.updatePosition()
                green_monster.updatePosition()
                ugly_monster.updatePosition()
                ParallelThreads.findMonstersNewPosition(
                    red_monster, green_monster, ugly_monster, player,
                    player_list, self.mediator)

            red_monster.updatePosition()
            green_monster.updatePosition()
            ugly_monster.updatePosition()

            action_local = player.detectMonsterCollision(
                red_monster, green_monster, ugly_monster)
            action_local = player.detectWin(action_local)

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    action_local = Action.quit_game

        self.playing_time = time.time() - start_time

        return action_local

    def endScreen(self):
        pygame.mixer.music.load("sounds/defeat_track.mp3")
        pygame.mixer.music.play()

        exit_position = ((3 * self.mediator.game_screen.size[0] / 4 - 180),
                         500)
        restart_position = (self.mediator.game_screen.size[0] / 4 - 180, 500)

        self.mediator.game_screen.showEndScreen(self.playing_time)

        action = Action.local_loop

        while action == Action.local_loop:
            if not pygame.mixer.music.get_busy():
                pygame.mixer.music.play()

            mx, my = pygame.mouse.get_pos()

            for event in pygame.event.get():

                if event.type == pygame.QUIT:
                    action = Action.quit_game
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    if restart_position[0] <= mx <= restart_position[0]+360 and \
                            500 <= my <= 605.5:
                        action = Action.stand_by
                    elif exit_position[0] <= mx <= exit_position[0] + 360 and \
                            500 <= my <= 605.5:
                        action = Action.quit_game

            if restart_position[0] <= mx <= restart_position[0]+360 and \
                    500 <= my <= 605.5:
                self.mediator.game_screen.window.blit(
                    self.mediator.game_screen.pressed_restart_button,
                    (restart_position[0], 500))

            else:
                self.mediator.game_screen.window.blit(
                    self.mediator.game_screen.restart_button,
                    (restart_position[0], 500))

            if exit_position[0] <= mx <= exit_position[0] + 360 and \
                    500 <= my <= 605.5:
                self.mediator.game_screen.window.blit(
                    self.mediator.game_screen.pressed_exit_button,
                    (exit_position[0], 500))

            else:
                self.mediator.game_screen.window.blit(
                    self.mediator.game_screen.exit_button,
                    (exit_position[0], 500))

            pygame.display.flip()

        return action

    def winningScreen(self):
        pygame.mixer.music.load("sounds/victory_track.mp3")
        pygame.mixer.music.play()

        restart_position = (self.mediator.game_screen.size[0] / 4 - 235, 490)
        exit_position = ((3 * self.mediator.game_screen.size[0] / 4 - 140),
                         490)

        self.mediator.game_screen.showWinningScreen(self.playing_time)

        action = Action.local_loop

        while action == Action.local_loop:
            if not pygame.mixer.music.get_busy():
                pygame.mixer.music.play()

            mx, my = pygame.mouse.get_pos()

            for event in pygame.event.get():

                if event.type == pygame.QUIT:
                    action = Action.quit_game
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    if restart_position[0] <= mx <= restart_position[0]+360 and \
                            restart_position[1] <= my <= restart_position[1] + 105.5:
                        action = Action.stand_by
                    elif (3 * self.mediator.game_screen.size[0] / 4 - 180) <= mx \
                            <= (3 * self.mediator.game_screen.size[0] / 4 + 180) and \
                            exit_position[1] <= my <= exit_position[1] + 105.5:
                        action = Action.quit_game

            if restart_position[0] <= mx <= restart_position[0]+360 and \
                    restart_position[1] <= my <= restart_position[1] + 105.5:
                self.mediator.game_screen.window.blit(
                    self.mediator.game_screen.pressed_restart_button,
                    (restart_position[0], restart_position[1]))

            else:
                self.mediator.game_screen.window.blit(
                    self.mediator.game_screen.restart_button,
                    (restart_position[0], restart_position[1]))

            if (3 * self.mediator.game_screen.size[0] / 4 - 180) <= mx \
                    <= (3 * self.mediator.game_screen.size[0] / 4 + 180) and \
                    exit_position[1] <= my <= exit_position[1] + 105.5:
                self.mediator.game_screen.window.blit(
                    self.mediator.game_screen.pressed_exit_button,
                    exit_position)

            else:
                self.mediator.game_screen.window.blit(
                    self.mediator.game_screen.exit_button, exit_position)

            pygame.display.flip()

        return action
Example #30
0
 def test_add_listener_with_same_priority_failed(self):
     mediator = Mediator()
     mediator.add_listener(Event, lambda event: None, 0)
     with self.assertRaises(IndexError):
         mediator.add_listener(Event, lambda event: None, 0)
Example #31
0
 def test_remove_listener(self):
     mediator = Mediator()
     listener1 = Listener()
     listener2 = Listener()
     mediator.add_listener(Event, listener1)
     mediator.add_listener(Event, listener2)
     event = Event()
     mediator.dispatch(event)
     mediator.remove_listener(Event, listener1)
     mediator.dispatch(event)
     self.assertEqual(len(listener1.events), 1)
     self.assertEqual(len(listener2.events), 2)
     mediator.remove_listener(Event)
     mediator.dispatch(event)
     self.assertEqual(len(listener1.events), 1)
     self.assertEqual(len(listener2.events), 2)
Example #32
0
    def test_event__unload(self):
        Mediator._load = mock.MagicMock()

        Mediator.load()

        Mediator._load.assert_called_once_with()
Example #33
0
    def test_add_subscriber(self):
        mediator = Mediator()

        with self.assertRaises(TypeError) as context:
            mediator.add_subscriber(object())
        self.assertEqual(str(context.exception), 'Expects instance of SubscriberInterface')

        subscriber = Subscriber()
        mediator.add_subscriber(subscriber)

        event = EventOne()
        mediator.dispatch(event)
        self.assertEqual(len(subscriber.results), 3)
        events = [result[0] for result in subscriber.results.values()]
        self.assertIs(events[0], events[1])
        self.assertIs(events[1], events[2])
        first, middle, last = (
            subscriber.results['first'][1],
            subscriber.results['middle'][1],
            subscriber.results['last'][1]
        )
        self.assertTrue(first < middle < last, '%r %r %r' % (first, middle, last))

        event = EventTwo()
        mediator.dispatch(event)
        self.assertIs(subscriber.results['another'], event)

        event = EventThree()
        mediator.dispatch(event)
        self.assertIs(subscriber.results['even_more'], event)

        with self.assertRaises(ValueError):
            mediator.add_subscriber(BadSubscriberOne())

        with self.assertRaises(ValueError):
            mediator.add_subscriber(BadSubscriberTwo())
Example #34
0
# Main file of the app 'name_of_the_app'
# The application is for controlling display brightness
# depending on current time
# author: jogme

from time import sleep

import config
from mediator import Mediator

mediator = Mediator()

if __name__ == "__main__":
    while True:
        mediator.updater.update()
        sleep(config.sleep_time_s)
Example #35
0
 def test_dispatch_failed(self):
     meditator = Mediator()
     with self.assertRaises(TypeError) as context:
         meditator.dispatch('unexpected_event')
     self.assertEqual(str(context.exception), 'Expects instance of Event')
Example #36
0
"The Mediator Use Case Example"
from component import Component
from mediator import Mediator

MEDIATOR = Mediator()
COMPONENT1 = Component(MEDIATOR, "Component1")
COMPONENT2 = Component(MEDIATOR, "Component2")
COMPONENT3 = Component(MEDIATOR, "Component3")
MEDIATOR.add(COMPONENT1)
MEDIATOR.add(COMPONENT2)
MEDIATOR.add(COMPONENT3)

COMPONENT1.notify("data A")
COMPONENT2.notify("data B")
COMPONENT3.notify("data C")
Example #37
0
 def test_add_listener_with_same_priority_failed(self):
     mediator = Mediator()
     mediator.add_listener(Event, lambda event: None, 0)
     with self.assertRaises(IndexError):
         mediator.add_listener(Event, lambda event: None, 0)
            sys.exit()
        return False

    except Exception as e:
        print('General error', str(e))
        sys.exit()
        return False
    return False


client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((IP, PORT))
client_socket.setblocking(False)
my_username = ""
my_password = ""
mediator = Mediator()

while True:  #Login
    my_username = input("Username: "******"Password: "******" " + my_password).encode('utf-8')
    username_header = f"{len(username):<{HEADER_LENGTH}}".encode('utf-8')
    client_socket.send(username_header + username)
    response = tryGetMessage(client_socket)
    if response is not False:
        print(response)
Example #39
0
class MyForm(QtGui.QMainWindow):	


	def modeChanged(self, mode):
		self.showInfoMessage(mode)
		pass


	
	def setMeasuringMode(self):
		self.mode.setMode("MEASURING")
		for i in (4,5,6,7): self.ui.menu.deactivate(i)
		self.calibration=None
		self.reset()

	def showInfoMessage(self, mes):
		u"""shows info message"""
		QtGui.QMessageBox.information(self, programName, mes, QtGui.QMessageBox.Ok)

	def setDistance(self, distance):
		u"""called by the mediator when the distance changes
		updates fields distX and distY
		calls updateUi"""
		self.distX, self.distY = distance[0], distance[1]
		self.updateUi()


	def updateUi(self):		
		u"""updates UI based on distX and distY values"""
		
		# global distance ran over detector in both axis
		formatedDistanceX, formatedDistanceY = "%.3F" % self.distX, "%.3F" % self.distY
		
		# natively show the distance ran over the detector
		textX, textY = formatedDistanceX, formatedDistanceY


		# update info about
		if self.mode.getMode() == "MEASURING":
			pass	


		# the calibrated axis doesn't show the global way ran
		elif self.mode.getMode() == "CAL_X": textX = _("calibration")
		elif self.mode.getMode() == "CAL_Y": textY = _("calibration")

		self.textX = "<font color=red size=4><b>" + textX + "</b></font>"


		# fill the data into labels

		from PyQt4 import Qt
		# showing total distance ran over cursor
		self.ui.lbWayX.setText(textX)
		self.ui.lbWayY.setText(textY)
		

		# calculate the remaining distance and show it
		remainingX = self.wantedX - self.distX
		remainingY = self.wantedY - self.distY
		self.ui.lbRemainingX.setText("%3.3F"%remainingX)
		self.ui.lbRemainingY.setText("%3.3F"%remainingY)

		# show the distance user wants to measure
		self.ui.lbWantedX.setText("%3.3F"%self.wantedX)
		self.ui.lbWantedY.setText("%3.3F"%self.wantedY)

		# show info about current working mode
		self.ui.lbMode.setText(str(self.mode.getMode()))



	def __init__(self, parent=None):

		# 
		QtGui.QWidget.__init__(self, parent)


		
#		from PyQt4 import QString
		self.ui = Ui_Form()
		self.ui.setupUi(self, [  (_('Reset'), self.reset),
					 (_('Measuring mode'), self.setMeasuringMode),
					 (_('Distance to measure in X axis'), self.askWantedX),
					 (_('Distance to measure in Y axis'), self.askWantedY),
					 (_('Calibrate Y axis'), self.calY),
					 (_('Calibrate X axis'), self.calX),
					 (_('Finish calibration'), self.finishCal),
					 (_('Save calibration'), self.saveRatio),
					 (_('Allow calibration'), self.allowCalibrate),
					])


		
		print 'main widget: ',self.centralWidget()
		self.centralWidget().setMouseTracking(True)
		# mode of working
		self.mode = Mode(self, ("MEASURING", "CAL_X", "CAL_Y"))

		# storing programs options
		self.options = Options()

		# object that manages all other objects :)
		self.mediator = Mediator(self.cursor(), self)




		# distance the user wants the device to travel
		self.wantedX, self.wantedY = 0.0, 0.0

		self.setMeasuringMode()
		# set default values
		self.reset()

		



	def reset(self):
		u"""resets ui and mediator, loads ratio from file (if was calibrated at runtime and not saved, then calibration data will
		be lost, sets distance to 0"""

		# distance travelled by the input device in the measurements units (defalutly milimeters)
		self.distX, self.distY = 0.0, 0.0
		
		self.mediator.reset()
		self.mediator.storeRatio(self.options.getRatio())

		self.updateUi()
	
	def askWanted(self, axis):
		text, ok = QtGui.QInputDialog.getText(self, programName, _('Distance to measure in %s axis:')%axis)
		name = {'X':'wantedX', 'Y':'wantedY'}
		try:
			# force converting to float
			text="%s.0" % text
			distance = float(eval(text))
			setattr(self, name[axis], distance)
		except:
			self.showInfoMessage(_("You entered inproper number. You can use arithmetic operators (+, -, * /)"))
		self.updateUi()

	def askWantedX(self):self.askWanted('X')
	def askWantedY(self):self.askWanted('Y')
			
	def allowCalibrate(self):
		text, ok = QtGui.QInputDialog.getText(self, programName, _("""Enter below "SUWMIARKA". 
It is protection against accidentally changing of the calibration configuration.
Entering word other than "SUWMIARKA" or pressing Cancel key simply returns to the mail menu and doesn't allow calibration.
If you choose to allow calibrating, and then with any cause you would like to turn off allowing calibration, choose option """)
+_('Measuring mode'))
		if ok:
			if text == "SUWMIARKA":
				for i in (4,5):	self.ui.menu.activate(i)

	def saveRatio(self):
		u"""saves ratio to a file"""
		try:
			self.options.saveRatio()
		# if some problems occurred, show the info message
		except:
			self.showInfoMessage(_("Error during saving calibration data to file."))
		# else show message that everything was ok
		else:
			self.showInfoMessage(_("Calibration data saved correctly"))
			


	def finishCal(self):
		u"""calculates the ratio of the calibrated axis, saves it to the options object (not to a file!!)"""
		if self.calibration == None:
			self.showInfoMessage("Blad programu")
		elif self.calibration in ('X','Y'):


			#TODO: tu jest odwolanie do mediatora, koniecznie ustaw odpowiednie metody !!
			if self.calibration =='X': przesuniecie = self.mediator.coordCounter.ticksX
			else: przesuniecie = self.mediator.coordCounter.ticksY

			self.showInfoMessage(_("General distance in axis %s: %s"%(self.calibration, przesuniecie)))

			text, ok = QtGui.QInputDialog.getText(self, programName, _('Enter distance the input device has moved: '))
			if ok:
				calc = przesuniecie / float(text)
				if self.calibration == 'X':  self.options.xRatio = calc
				elif self.calibration =='Y': self.options.yRatio = calc
			self.showInfoMessage(_("Ticks on milimeter in axis ")+self.calibration+_(" is: %s")%calc)

		self.calibration = None
		self.setMeasuringMode()
		self.ui.menu.activate(7)

	def cal(self, axis):
		u"""starts calibrating an axis"""
		self.ui.menu.activate(6)
		reply = QtGui.QMessageBox.information(self, programName, _("Starting calibration of axis: %s")%axis, QtGui.QMessageBox.Ok)
		if reply == QtGui.QMessageBox.Ok:
			self.reset()
			self.calibration = axis
	
	def calX(self):
		self.mode.setMode("CAL_X")
		self.cal('X')

	def calY(self):
		self.mode.setMode("CAL_Y")
		self.cal('Y')

	def keyPressEvent(self, event):
		proc = {'A':self.ui.menu.keyUp,'Z':self.ui.menu.keyDown,'K':self.ui.menu.executeMethod}
		if type(event) == QtGui.QKeyEvent:
			char = chr(event.key())
			print 'char: ',char
			if char in proc: proc[char]()


	def mouseMoveEvent(self, event):
		try:
			self.mediator.coordChanged()
		except TickException, e:
			QtGui.QMessageBox.information(self, "", _("Inproper reading from input device."), QtGui.QMessageBox.Ok)
			self.reset()
Example #40
0
def main():
    print('program start')
    from utils.text_process import text_precess, text_to_code  # TODO: move?
    from utils.text_process import get_tokenlized, get_word_list, get_dict

    random.seed(SEED)
    np.random.seed(SEED)
    assert START_TOKEN == 0

    # JJ added
    SEQ_LENGTH, vocab_size = text_precess(true_file, val_file)

    gen_data_loader = Gen_Data_loader(BATCH_SIZE, SEQ_LENGTH)
    gan_data_loader = Gen_Data_loader(BATCH_SIZE, SEQ_LENGTH)
    val_data_loader = Gen_Data_loader(BATCH_SIZE, SEQ_LENGTH)
    likelihood_data_loader = Gen_Data_loader(BATCH_SIZE,
                                             SEQ_LENGTH)  # For testing
    #vocab_size = 5000

    # JJ added
    # Create training file and dicts
    tokens = get_tokenlized(true_file)
    val_tokens = get_tokenlized(val_file)
    word_set = get_word_list(tokens + val_tokens)
    [word_index_dict, index_word_dict] = get_dict(word_set)
    with open(oracle_file, 'w') as outfile:
        outfile.write(text_to_code(tokens, word_index_dict, SEQ_LENGTH))
    with open(val_oracle_file, 'w') as outfile:
        outfile.write(text_to_code(val_tokens, word_index_dict, SEQ_LENGTH))

    generator = Generator(vocab_size, BATCH_SIZE, EMB_DIM, HIDDEN_DIM,
                          SEQ_LENGTH, START_TOKEN)
    #target_params = pickle.load(open('save/target_params_py3.pkl', 'rb'))
    #target_lstm = TARGET_LSTM(vocab_size, BATCH_SIZE, 32, 32, SEQ_LENGTH, START_TOKEN, target_params) # The oracle model

    mediator = Mediator(vocab_size,
                        BATCH_SIZE,
                        EMB_DIM * 2,
                        HIDDEN_DIM * 2,
                        SEQ_LENGTH,
                        START_TOKEN,
                        name="mediator",
                        dropout_rate=M_DROPOUT_RATE,
                        learning_rate=3e-3,
                        with_professor_forcing=False)

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)
    sess.run(tf.global_variables_initializer())

    # First, use the oracle model to provide the positive examples, which are sampled from the oracle data distribution
    #generate_samples(sess, target_lstm, BATCH_SIZE, generated_num, positive_file)
    gen_data_loader.create_batches(oracle_file)  #positive_file)
    gan_data_loader.create_batches(oracle_file)  #positive_file)
    #generate_samples(sess, target_lstm, BATCH_SIZE, generated_num, eval_file)
    val_data_loader.create_batches(val_oracle_file)  #eval_file)

    log = open('save/experiment-log.txt', 'w')
    log_nll = open('save/experiment-log-nll.txt', 'w')
    #log_jsd = open('save/experiment-log-jsd.txt', 'w')

    #  pre-train generator (default 0 epochs)(not recommended)
    print('Start pre-training...')
    log.write('pre-training...\n')
    saver = tf.train.Saver(tf.global_variables())
    if RESTORE:
        saver.restore(sess, "saved_model/CoT")
    for epoch in range(PRE_EPOCH_NUM):
        loss = mle_epoch(sess, generator, gen_data_loader)
        if epoch % 1 == 0:
            generate_samples(sess, generator, BATCH_SIZE, generated_num,
                             negative_file)
            likelihood_data_loader.create_batches(negative_file)
            test_loss = target_loss(sess, target_lstm, likelihood_data_loader)
            print('pre-train epoch ', epoch, 'nll_oracle ', test_loss)
            buffer = 'epoch:\t' + str(epoch) + '\tnll_oracle:\t' + str(
                test_loss) + '\n'
            log_nll.write(buffer)
        if epoch % 1 == 0:
            test_loss = target_loss(sess, generator, val_data_loader)
            print('pre-train epoch ', epoch, 'nll_test ', test_loss)
            buffer = 'epoch:\t' + str(epoch) + '\tnll_test:\t' + str(
                test_loss) + '\n'
            log_nll.write(buffer)

    print(
        '#########################################################################'
    )
    toc = time.time()  # JJ
    print('Start Cooperative Training...')
    for iter_idx in range(TOTAL_BATCH):
        print('iteration: ' + str(iter_idx) + '\ntime: ' +
              str(time.time() - toc))
        toc = time.time()
        # Train the generator for one step
        for it in range(1):
            samples = generator.generate(sess)
            rewards = mediator.get_reward(sess, samples)
            feed = {generator.x: samples, generator.rewards: rewards}
            _ = sess.run(
                generator.g_updates, feed_dict=feed
            )  # JJ -> loss, _ = sess.run([generator.g_loss, generator.g_updates], feed_dict=feed)
        # Test
        # JJ delete
        '''
        if iter_idx % 100 == 0 or iter_idx == TOTAL_BATCH - 1:
            generate_samples(sess, generator, BATCH_SIZE, generated_num, negative_file)
            likelihood_data_loader.create_batches(negative_file)
            test_loss = target_loss(sess, target_lstm, likelihood_data_loader)
            buffer = 'batch:\t' + str(iter_idx) + '\tnll_oracle:\t' + str(test_loss) + '\n'
            print('batch: ', iter_idx, 'nll_oracle: ', test_loss)
            log_nll.write(buffer)
        '''
        if iter_idx % gen_data_loader.num_batch == 0:  # epochs instead of batches #if iter_idx % 100 == 0:
            test_loss = target_loss(sess, generator, val_data_loader)
            print('epoch:\t', iter_idx // gen_data_loader.num_batch,
                  'nll_test ', test_loss)
            buffer = 'epoch:\t' + str(
                iter_idx // gen_data_loader.num_batch) + '\tnll_test:\t' + str(
                    test_loss) + '\n'
            #print('batch:\t', iter_idx, 'nll_test ', test_loss)
            #buffer = 'batch:\t'+ str(iter_idx) + '\tnll_test:\t' + str(test_loss) + '\n'
            log_nll.write(buffer)
            saver.save(sess, "saved_model/CoT")
        # Train the mediator
        for _ in range(1):
            bnll_ = []
            """
            d_loss_ = []
            for it in range(3):
                feed = {
                    mediator.x0: gan_data_loader.next_batch(),
                    mediator.x1: generator.generate(sess)
                }
                d_loss, _ = sess.run([mediator.d_loss, mediator.d_update], feed)
                d_loss_.append(d_loss)
            """
            for it in range(1):
                feed = {
                    mediator.x0: gen_data_loader.next_batch(),
                    mediator.x1: generator.generate(sess)
                }
                bnll = sess.run(mediator.likelihood_loss, feed)
                bnll_.append(bnll)
                sess.run(mediator.dropout_on)
                _ = sess.run(mediator.likelihood_updates, feed)
                sess.run(mediator.dropout_off)
            if iter_idx % 10 == 0:
                bnll = np.mean(bnll_)
                print("mediator cooptrain iter#%d, balanced_nll %f" %
                      (iter_idx, bnll))
                log.write("%d\t%f\n" % (iter_idx, bnll))
        #if iter_idx % gen_data_loader.num_batch == 0:
        #jsd = jsd_calculate(sess, generator, target_lstm)
        #print('cooptrain epoch#', iter_idx // gen_data_loader.num_batch, 'jsd ', jsd)
        #log_jsd.write("%d\t%f\n" % (iter_idx // gen_data_loader.num_batch, jsd))
        #saver.save(sess, "saved_model/CoT")
    log.close()
    log_nll.close()

def listToString(s):

    # initialize an empty string
    str1 = ""

    # traverse in the string
    for ele in s:
        str1 += ele

    # return string
    return str1


mediator = Mediator()

while True:
    read_sockets, _, exception_sockets = select.select(sockets_list, [],
                                                       sockets_list)

    for notified_socket in read_sockets:
        if notified_socket == server_socket:  #If a new user tries to connect
            client_socket, client_address = server_socket.accept()

            user = receive_message(client_socket)

            if user is False:  #If the server gets something
                continue
            elif db.userExists(
                    user['data'].decode('utf-8').split()[0],
Example #42
0
 def test_remove_listener(self):
     mediator = Mediator()
     listener1 = Listener()
     listener2 = Listener()
     mediator.add_listener(Event, listener1)
     mediator.add_listener(Event, listener2)
     event = Event()
     mediator.dispatch(event)
     mediator.remove_listener(Event, listener1)
     mediator.dispatch(event)
     self.assertEqual(len(listener1.events), 1)
     self.assertEqual(len(listener2.events), 2)
     mediator.remove_listener(Event)
     mediator.dispatch(event)
     self.assertEqual(len(listener1.events), 1)
     self.assertEqual(len(listener2.events), 2)