Beispiel #1
0
def add_call_event(reactor, call):
    """Add a call event to the reactor."""
    
    if lowlevel.cc_version < 6:
        global _call_event_thread
        if not _call_event_thread:
            log.debug("starting V5 call event helper thread")
            _call_event_thread = CallEventThread()
            _call_event_thread.setDaemon(1)
            _call_event_thread.start()

            atexit.register(shutdown_call_event_thread)

        _call_event_thread.add(reactor, call)
    else:
        chwo = lowlevel.CALL_HANDLE_WAIT_OBJECT_PARMS()
        chwo.handle = call.handle

        rc = lowlevel.call_get_handle_event_wait_object(chwo)
        if rc:
            raise AculabError(rc, 'call_get_handle_event_wait_object')

        if os.name == 'nt':
            # This is a bit nasty - we set an attribute on call
            call.event = pywintypes.HANDLE(chwo.wait_object)

            # Note the curry
            reactor.add(call.event, curry(call_on_event, call))
        else:
            # This is a bit nasty - we set an attribute on call
            call.event = chwo.wait_object.fileno()

            # Note the curry
            reactor.add(call.event, chwo.wait_object.mode(),
                        curry(call_on_event, call))
Beispiel #2
0
 def OnRightClickConnection(self, event, c):
     m = wxMenu()
     port = self.cm.get_connection_port(c)[0]
     self.AddMenuItem(m, "View", curry(self.ViewPort, port=port))
     self.AddMenuItem(m, "Remove", curry(self.RemoveConnection,
                                         connection=c))
     self.PopupMenu(m, event.GetPosition())
def extract_features(Xtr_text, Xte_text, stop_words=None, ngram_range=(1, 1), min_df=0.0, max_df=1.0, max_features=None):
    # Extract features (Yes, this is based on The One programming style haha)
    print("Extract features...")

    print("Obtaining classic data...")
    # Connect the preprocessing functions
    extractor = Extractor(Xtr_text, Xte_text)\
        .bind(curry(emoji_tagging))\
        .bind(curry(remove_stop_words))\
        .bind(curry(lemmatizing))\
        .bind(curry(negation_handling))\
        .bind(curry(clean_text))
        

    # Add the feature extractor functions
    extractor\
        .bind(extract_tf(ngram_range=ngram_range, min_df=min_df, max_df=max_df))\
        .bind(extract_tf_idf)\
        .bind(k_best(Ytr, Yte, max_features))

    # Extract the features
    Xtr, Xte = extractor.get_features()

    print("Xtr shape: {}".format(Xtr.shape))
    print("Xte shape: {}".format(Xte.shape))
    print("Done extracting features!")
    return Xtr, Xte
Beispiel #4
0
    def _test(self, b_immediate, b_set_error):
        agg = future.FutureAggregator()

        exc_string = 'EXC_STRING'        
        n_total = 10
        if b_immediate:
            n_unset = 0 
        else:
            n_unset = 5

        unset_futures = [future.Future() for i in range(n_unset)]
        for f in unset_futures:
            agg.add_future(f)

        # add some more futures that are already set
        for i in range(n_unset,n_total):
            f = future.Future()
            if b_set_error and b_immediate and (i%2==0):
                f.set_error(TestException(exc_string))
            else:
                f.set(i)
            agg.add_future(f)

        agg_f = agg.activate()

        def observer(the_future, **kw):
            self.failUnless(the_future.is_set())
            thread_id = id(threading.current_thread())
            if not b_set_error:
                res = sum(the_future.get())
            else:
                self.failUnless(the_future.is_error())
                res = the_future.get_error()
            self.result.val = (res,thread_id)
        agg_f.attach_observer(observer)

        threads = []
        for i,f in enumerate(unset_futures):
            if b_set_error and (i%2 == 0):
                target = curry(f.set_error,TestException(exc_string))
            else:
                target = curry(f.set,i)
            t = threading.Thread(target=target)
            threads.append(t)
            t.start()

        for t in threads: # wait for all threads. this also guarantees that self.result.val will already have been set
            t.join()
        res,tid = self.result.val
        if b_set_error:
            self.assertEqual(str(res),exc_string)
        else:
            self.assertEqual(res,sum(range(n_total)))
        main_thread_id = id(threading.current_thread())
        if b_immediate:
            self.failUnless(tid == main_thread_id)
        else:
            self.failIf(tid == main_thread_id)
            self.failUnless(tid in [id(t) for t in threads])
Beispiel #5
0
 def create_pipe(self, reactor):
     if os.name == 'nt':
         pipe = win32event.CreateEvent(None, 0, 0, None)
         self.pipes[reactor] = pipe
         reactor.add(pipe, curry(self.on_event, pipe))
     else:
         # Create a nonblocking pipe (if drained completely on reading,
         # this will behave like a Windows Event Semaphore)
         pipe = create_pipe(True)
         self.pipes[reactor] = pipe
         reactor.add(pipe[0].fileno(), select.POLLIN,
                     curry(self.on_event, pipe[0]))
Beispiel #6
0
    def test_wait(self):
        f = future.Future()
        self.failIf(f.is_set())
        threading.Thread(target=curry(f.set, 3)).run()
        f.wait()
        self.failUnless(f.is_set())
        self.assertEqual(f.get(),3)

        f = future.Future()
        self.failIf(f.is_set())
        threading.Thread(target=curry(f.set_error, Exception())).run()
        f.wait()
        self.failUnless(f.is_set())
        self.assertEqual(f.is_error(),True)
Beispiel #7
0
 def test_set_error(self):
     f = future.Future()
     threading.Thread(target=curry(f.set_error, TestException('BAD'))).run()
     self.assertRaises(TestException,f.get)
     self.failUnless(f.is_set())
     self.failUnless(f.is_error())
     self.assertEqual(str(f.get_error()),'BAD')
Beispiel #8
0
 def __getattribute__(self, name):
     initialized = Motor.__getattribute__(self, '_initialized')
     if not initialized:
         return Motor.__getattribute__(self, name)
     if name in Motor.__getattribute__(self, '__dict__'):
         return Motor.__getattribute__(self, name)
     elif name in ['adapt']:
         return Motor.__getattribute__(self, name)
     else:
         method = getattr(self._current, name)
         if hasattr(method, 'im_func'):
             value = curry(method.im_func, self)
         elif hasattr(method, 'fget'):
             value = curry(method.fget, self)()
         else:
             value = method
         return value
Beispiel #9
0
    def test_normal(self):
        f = future.Future()
        self.failIf(f.is_set())

        threading.Thread(target=curry(f.set, 3)).run()
        val = f.get()
        self.failUnless(f.is_set())
        self.assertEqual(val,3)
Beispiel #10
0
 def __getattribute__(self, name):
     initialized = Robot.__getattribute__(self, '_initialized')
     if not initialized:
         return Robot.__getattribute__(self, name)
     if name in Robot.__getattribute__(self, '__dict__'):
         return Robot.__getattribute__(self, name)
     elif name in ['adapt', 'initBrain', '_testGoalReached']:
         return Robot.__getattribute__(self, name)
     else:
         method = getattr(self._current, name)
         if hasattr(method, 'im_func'):
             value = curry(method.im_func, self)
         elif hasattr(method, 'fget'):
             value = curry(method.fget, self)()
         else:
             value = method
         return value
Beispiel #11
0
 def OnRightClickComponent(self, event, c):
     m = wxMenu()
     for port in self.cm.get_component_output_ports(c):
         m2 = wxMenu()
         for input_c, input_p in self.cm.get_available_input(port):
             text = '[ ' + input_c.name + ':' + input_p.name + ' ]'
             func = curry(self.CreateConnection,
                          from_component=c,
                          to_component=input_c,
                          from_port=port,
                          to_port=input_p)
             self.AddMenuItem(m2, text, func)
         m.AppendMenu(wxNewId(), "Connect [ %s ] to..." % port.name, m2)
     for port in self.cm.get_component_output_ports(c):
         self.AddMenuItem(m, "View [ %s ]" % port.name,
                          curry(self.ViewPort, port=port))
     m.AppendSeparator()
     self.AddMenuItem(m, "Configure",
                      curry(self.OnLeftDClickComponent, component=c))
     self.AddMenuItem(m, "Rename", curry(self.RenameComponent, component=c))
     self.AddMenuItem(m, "Run", curry(self.RunOne, component=c))
     self.AddMenuItem(m, "Reset", curry(self.ResetComponent, component=c))
     self.AddMenuItem(m, "Remove", curry(self.RemoveComponent, component=c))
     self.PopupMenu(m, event.GetPosition())
     m.Destroy()
Beispiel #12
0
 def __init__(self,parent,log):
     wxShapeCanvas.__init__(self,parent,style=wxSUNKEN_BORDER)
     self.SetBackgroundColour(wxWHITE)
     self.parent = parent
     self.log = log
     self.diagram = wxDiagram()
     self.SetDiagram(self.diagram)
     self.diagram.SetCanvas(self)
     EVT_RIGHT_UP(self,curry(self.OnMouseClick,button='RIGHT'))
     EVT_LEFT_DCLICK(self,curry(self.OnMouseClick,button='LEFT'))
     EVT_COMPONENT_MANAGER(self,self.OnComponentManagerEvent)
     self.shape2component = {}
     self.component2shape = {}
     self.line2connection = {}
     self.connection2line = {}
     self.cm = ComponentManager.ComponentManager(self,
                 self.ComponentManagerCallback(self))
     self.CreatePluginMenu()
Beispiel #13
0
 def __init__(self, parent, log):
     wxShapeCanvas.__init__(self, parent, style=wxSUNKEN_BORDER)
     self.SetBackgroundColour(wxWHITE)
     self.parent = parent
     self.log = log
     self.diagram = wxDiagram()
     self.SetDiagram(self.diagram)
     self.diagram.SetCanvas(self)
     EVT_RIGHT_UP(self, curry(self.OnMouseClick, button='RIGHT'))
     EVT_LEFT_DCLICK(self, curry(self.OnMouseClick, button='LEFT'))
     EVT_COMPONENT_MANAGER(self, self.OnComponentManagerEvent)
     self.shape2component = {}
     self.component2shape = {}
     self.line2connection = {}
     self.connection2line = {}
     self.cm = ComponentManager.ComponentManager(
         self, self.ComponentManagerCallback(self))
     self.CreatePluginMenu()
Beispiel #14
0
def getDestroyNetworkTasks(endpoints, mode=Mode.SWITCH):
	assert _areEndpoints(endpoints)
	reverse = util.curry(_tryDestroyNetwork, [endpoints, mode])
	taskset = tasks.TaskSet()
	for ep in endpoints:
		id = ep.getId()
		assert id
		taskset.add(tasks.Task("delete-files-%s" % id, _deleteFiles, reverseFn=reverse, args=(ep,)))
	return taskset
Beispiel #15
0
 def CreatePluginMenu(self):
     plugins = self.cm.get_plugins()
     categories = dict([(p.category,1) for p in plugins]).keys()
     self.plugin_menu = wxMenu("-- Create Components --")
     for c in categories:
         m = wxMenu()
         for p in [p for p in plugins if p.category == c]:
             self.AddMenuItem(m, p.name, curry(self.CreateComponent,plugin=p))
         self.plugin_menu.AppendMenu(wxNewId(),c,m)
Beispiel #16
0
    def test_set_by_other_future(self):
        f1 = future.Future()              
        f2 = future.Future()
        threading.Thread(target=curry(f1.set, 3)).run()
        f1.get()
        self.failUnless(f1.is_set())
        f2.set_by_other_future_value(f1)
        self.failUnless(f2.is_set())
        self.assertEqual(f2.get(),3)        

        #set error:
        f1 = future.Future()
        f2 = future.Future()
        threading.Thread(target=curry(f1.set_error,TestException('BAD'))).run()
        self.assertRaises(TestException,f1.get)
        f2.set_by_other_future_value(f1)
        self.assertRaises(TestException,f2.get)
        self.assertEqual(str(f2.get_error()),'BAD')
Beispiel #17
0
 def CreatePluginMenu(self):
     plugins = self.cm.get_plugins()
     categories = dict([(p.category, 1) for p in plugins]).keys()
     self.plugin_menu = wxMenu("-- Create Components --")
     for c in categories:
         m = wxMenu()
         for p in [p for p in plugins if p.category == c]:
             self.AddMenuItem(m, p.name,
                              curry(self.CreateComponent, plugin=p))
         self.plugin_menu.AppendMenu(wxNewId(), c, m)
Beispiel #18
0
    def test_progress_observer(self):
        f = future.Future()
        main_thread_id = id(threading.current_thread())

        result_many = Result()        
        def observe_many(the_future, **kw):
            result_many.val = (the_future.get_progress().percent, id(threading.current_thread()))

        result_once = Result()        
        def observe_once(the_future, **kw):
            self.assertEqual(result_once.val,None)
            result_once.val = (the_future.get_progress().percent, id(threading.current_thread()))

        f.attach_observer(observe_many, b_notify_on_progress=True)
        f.attach_observer(observe_once, b_notify_on_progress=True,b_notify_once=True)
        t = threading.Thread(target=curry(f.set_progress, 50))
        t.start()
        t.join()
        self.assertEqual(f.get_progress().percent, 50)
        self.assertEqual(result_many.val, (50, id(t)))
        self.assertEqual(result_once.val, (50, id(t)))

        result_immediate = Result()
        def observe_immediate(the_future, **kw):
            result_immediate.val = (the_future.get_progress().percent, id(threading.current_thread()))
        f.attach_observer(observe_immediate,b_notify_on_progress=True)
        self.assertEqual(result_immediate.val,(50,main_thread_id))

        result_not_immediate = Result()
        def observe_not_immediate(the_future, **kw):
            result_not_immediate.val = (the_future.get_progress().percent, id(threading.current_thread()))
        f.attach_observer(observe_not_immediate,b_notify_on_progress=True,b_notify_immediate_progress=False)
        self.assertEqual(result_not_immediate.val,None)
        
        t2 = threading.Thread(target=curry(f.set_progress, 75))
        t2.start()
        t2.join()
        self.assertEqual(f.get_progress().percent,75)
        self.assertEqual(result_many.val, (75, id(t2)))
        self.assertEqual(result_once.val, (50, id(t)))
        self.assertEqual(result_immediate.val,(75, id(t2)))
        self.assertEqual(result_not_immediate.val,(75, id(t2)))
Beispiel #19
0
def getStopNetworkTasks(endpoints, mode=Mode.SWITCH):
	assert _areEndpoints(endpoints)
	taskset = tasks.TaskSet()
	reverse = util.curry(_tryStopNetwork, [endpoints, mode])
	for ep in endpoints:
		id = ep.getId()
		assert id
		teardown_ep = tasks.Task("teardown-routing-%s" % id, _teardownRouting, args=(ep,mode,), reverseFn=reverse)
		stop_ep = tasks.Task("stop-endpoint-%s" % id, _stopEndpoint, args=(ep,), reverseFn=reverse)
		taskset.add([teardown_ep, stop_ep])
	return taskset
Beispiel #20
0
def getStartNetworkTasks(endpoints, mode=Mode.SWITCH):
	assert _areEndpoints(endpoints)
	taskset = tasks.TaskSet()
	reverse = util.curry(_tryStopNetwork, [endpoints, mode])
	for ep in endpoints:
		id = ep.getId()
		assert id
		start_ep = tasks.Task("start-endpoint-%s" % id, _startEndpoint, args=(ep,), reverseFn=reverse)
		connect_ep = tasks.Task("connect-endpoint-%s" % id, _connectEndpoint, args=(ep,mode), reverseFn=reverse, after=start_ep)
		taskset.add([start_ep, connect_ep])
	return taskset
Beispiel #21
0
    def test_observers(self):
        f = future.Future()

        def observer(the_future, **kw):
            self.assert_(the_future.is_set())
            self.assertEqual(self.result.val,None)
            self.result.val = (the_future.get(), id(threading.current_thread()))

        f.attach_observer(observer)
        f.set_progress(50) # verify observer not called (it would assert)
        t = threading.Thread(target=curry(f.set, 'a name'))            
        t.start()
        t.join()
        self.failUnless(f.is_set())
        self.assertEqual(self.result.val, ('a name', id(t)))

        self.result.val = None
        f.attach_observer(observer) # attaching observer after future is set calls observer immediately (from our thread)
        self.assertEqual(self.result.val, ('a name', id(threading.current_thread())))
Beispiel #22
0
def getPrepareNetworkTasks(endpoints, mode=Mode.SWITCH):
	assert _areEndpoints(endpoints)
	reverse = util.curry(_tryDestroyNetwork, [endpoints, mode])
	taskset = tasks.TaskSet()
	determine_connections = tasks.Task("determine-connections", _determineConnections, args=(endpoints,))
	create_config_all = []
	for ep in endpoints:
		id = ep.getId()
		assert id
		create_config_all.append(tasks.Task("create-config-%s" % id, _createConfigTask, args=(ep, mode,), callWithTask=True, reverseFn=reverse, after=determine_connections))
	collect_config_all = []
	for ep in endpoints:		
		id = ep.getId()
		assert id
		collect_config_all.append(tasks.Task("collect-config-%s" % id, _collectConfigTask, args=(ep,), callWithTask=True, reverseFn=reverse, after=[create_config_all, determine_connections]))
	for ep in endpoints:		
		id = ep.getId()
		assert id
		taskset.add(tasks.Task("use-config-%s" % id, _useConfigTask, args=(ep,), reverseFn=reverse, after=collect_config_all))
	taskset.add([determine_connections, create_config_all, collect_config_all])
	return taskset
Beispiel #23
0
 def OnRightClickComponent(self,event,c):
     m = wxMenu()
     for port in self.cm.get_component_output_ports(c):
         m2 = wxMenu()
         for input_c, input_p in self.cm.get_available_input(port):
             text = '[ ' + input_c.name + ':' + input_p.name + ' ]'
             func = curry(self.CreateConnection,
                          from_component = c, to_component = input_c,
                          from_port = port, to_port = input_p)
             self.AddMenuItem(m2,text,func)
         m.AppendMenu(wxNewId(),
             "Connect [ %s ] to..." % port.name, m2)
     for port in self.cm.get_component_output_ports(c):
         self.AddMenuItem(m, "View [ %s ]" % port.name,
                     curry(self.ViewPort,port=port))
     m.AppendSeparator()
     self.AddMenuItem(m, "Configure", curry(self.OnLeftDClickComponent,component=c))
     self.AddMenuItem(m, "Rename", curry(self.RenameComponent,component=c))
     self.AddMenuItem(m, "Run", curry(self.RunOne,component=c))
     self.AddMenuItem(m, "Reset",  curry(self.ResetComponent,component=c))
     self.AddMenuItem(m, "Remove", curry(self.RemoveComponent,component=c))
     self.PopupMenu(m, event.GetPosition())
     m.Destroy()
Beispiel #24
0
 def OnRightClickConnection(self,event,c):
     m = wxMenu()
     port = self.cm.get_connection_port(c)[0]
     self.AddMenuItem(m,"View",curry(self.ViewPort,port=port))
     self.AddMenuItem(m,"Remove",curry(self.RemoveConnection,connection=c))
     self.PopupMenu(m, event.GetPosition())