Exemple #1
0
    def _command_callback(self, response):
        for packet_text in response.body.strip().split("\r\n\r\n"):
            packet = {}
            for packet_line in packet_text.strip().split("\r\n"):
                packet_line = packet_line.rstrip()
                if ":" not in packet_line:
                    print "Is weird for now", line

                key, val = packet_line.split(":", 1)
                val = val[1:]  # quick and dirty left trim value even if it's 0 length
                packet[key] = val

            if not "ActionID" in packet:
                print "OHNOES"
                continue

            req = self._request_from_any(packet["ActionID"])

            attr = "_handle_%s_reply" % str(req.action).lower()
            if hasattr(self, attr):
                callable = getattr(self, attr)
                if operator.isCallable(callable):
                    callable(response, req, packet)

            attr = "handle_%s_reply" % str(req.action).lower()
            if hasattr(self, attr):
                callable = getattr(self, attr)
                if operator.isCallable(callable):
                    callable(response, req, packet)

            self._requests.remove(req)
            del (self._requests_actionid_map[packet["ActionID"]])

        print req.action, packet
Exemple #2
0
def operator_examples():
    import operator
    operator.isCallable(foo)
    operator.sequenceIncludes(foo, bar)

    from operator import isCallable, sequenceIncludes
    # These should produce warnings.
    isCallable(foo)
    sequenceIncludes(foo, bar)
Exemple #3
0
    def connection_gone(self,why):
        if self.retry_timer: return

        self.stream.close()

        logging.debug("Redis connection is gone from " + why + " event.")

        self.connected = False
        self.ready = False
        self.subscriptions = False
        self.monitoring = False

        if not self.emitted_end:
            self.emit("end")
            self.emitted_end = True

        while self.command_queue:
            command = self.command_queue.popleft()
            if operator.isCallable(command.callback):
                command.callback(None,error="Server connection closed")

        if self.closing:
            self.retry_time = None
            return

        self.current_retry_delay = self.retry_delay * self.retry_backoff
        logging.debug("Retry connection in " + str(self.current_retry_delay) + " ms")
        self.attempts += 1
        self.emit("reconnecting",Jso({'delay': self.current_retry_delay,'attempt': self.attempts}))
        self.retry_time = IOLoop.instance().add_timeout(
            time.time()+self.current_retry_delay,self.reconnect)
Exemple #4
0
 def _getCallableRef(self, listener, cb=None):
     # return weakref.ref(listener, self._listenerDied)
     meth = getattr(listener, 'eventReceived', None)
     if meth is not None and operator.isCallable(meth):
         return weakref.ref(listener, cb)
     else:
         return CallableRef(listener, cb)
def httpdigest(*args, **kwargs):
    """

    :param args:
    :param kwargs:
    May be used in one of three ways:
    * as a decorator factory (with the arguments being parameters to an
      instance of HttpDigestAuthenticator used to protect the decorated view)
    * as a decorator (protecting the decorated view with a default-constructed
      instance of HttpDigestAuthenticator)
    * as a decorator factory (with the argument being a pre-constructed
      HttpDigestAuthenticator instance used to protect the decorated view)
    """
    if len(args) == 1 and not kwargs and isCallable(args[0]):
        authenticator = HttpDigestAuthenticator()
        return decorator(partial(_httpdigest, authenticator), args[0])
    if len(args) == 1 and not kwargs and isinstance(args[0],
                                                    HttpDigestAuthenticator):
        authenticator = args[0]
    else:
        authenticator = HttpDigestAuthenticator(*args, **kwargs)

    def decor(f):
        return decorator(partial(_httpdigest, authenticator), f)
    return decor
 def extract_data(self, doc, url):
     for pattern in self.extractDataFunc:
         if pattern.match(url):
             callback = self.extractDataFunc[pattern]
             if isCallable(callback):
                 logging.debug('extract_data(' + url + ', ' + callback.__name__ + ')')
                 callback(doc, url)
Exemple #7
0
 def filter( self, container, selection, op = 'and' ):
     if isCallable(selection):
         selection = map( selection, container )
     if   lower(op) == 'and' : op = 0 # IThinningSvc.Operator.And
     elif lower(op) == 'or'  : op = 1 # IThinningSvc.Operator.Or
     else                    : op = 0 # IThinningSvc default
     return py_thinning( self, container, selection, op )
Exemple #8
0
    def urls(self):
        import operator

        urls_or_func = self.settings.get('source_urls') or getattr(self.rule, 'source_urls', None)
        rval = urls_or_func
        if operator.isCallable(urls_or_func):
            rval = urls_or_func(self)
        return rval or []
Exemple #9
0
 def _worker(self):
     #Processing queue items
     while not self._stop.is_set() and not self._queue.empty():
         item = self._queue.get()
         try:
             if item is not None and isCallable(item): 
                 item()
             elif isSequence(item): 
                 if self._action: self._action(*item)
                 elif isCallable(item[0]): item[0](*item[1:])
             elif self._action: 
                 self._action(item)
         except:
             import traceback
             print('objects.Pool.worker(%s) failed: %s'%(str(item),traceback.format_exc()))
         self._remove_task(item)
     return
def grouped_by(it, key):
    assert isCallable(key)
    grouped = {}
    for x in it:
        k = key(x)
        grp = grouped.get(k, [])
        grp.append(x)
        grouped[k] = grp
    return grouped
Exemple #11
0
 def prepare_value(self, value):
     ret = self.message
     if isCallable(ret):
         obj = getattr(self.parent_form, 'instance', None)
         if obj:
             ret = ret(obj)
         else:
             ret = ''
     return ret
Exemple #12
0
    def return_error(self,err):
        command_obj = self.command_queue.popleft()

        if not self.subscriptions and len(self.command_queue) == 0:
            self.emit("idle")

        if command_obj and operator.isCallable(command_obj.callback):
            command_obj.call_callback(None,err)
        else:
            logging.debug("tornado-redis: no callback to send error: %s" % str(err))
Exemple #13
0
 def add_event(self, eventname, func):
     if not isCallable(func):
         raise RuntimeError("func argument must be a function!")
     elif not isinstance(eventname, (basestring, int)):
         raise RuntimeError("Event name must be a string!")
     elif eventname in self._events:
         raise RuntimeError("Event name already exists!")
     
     else:
         self._events[eventname] = func
Exemple #14
0
 def append_if(self, line, path, i):
     line = line.split('#', 1)[0].strip()
     if line: 
         obj = colonize(line, path, i)
         if not isCallable(obj):
             raise ConfFileError( "'%s' is not callable." % line
                                , i
                                , path
                                 )
         self.append(obj)
 def __init__(self, function, kargs={}):
     if isinstance(function, types.StringTypes):
         ## Lookup in globals() filtered to remove non-callables
         try:
             function = dict([(name, func) for name, func in globals().items() if operator.isCallable(func)])[function]
         except KeyError:
             raise KeyError("Unknown function named '%s'" % function)
     
     self.function = function
     self.kargs    = kargs
Exemple #16
0
def dump(data):
    print type(data), "=>",
    if operator.isCallable(data):
        print "CALLABLE",
    if operator.isMappingType(data):
        print "MAPPING",
    if operator.isNumberType(data):
        print "NUMBER",
    if operator.isSequenceType(data):
        print "SEQUENCE",
    print
Exemple #17
0
    def test_operator(self):
        from operator import isCallable, sequenceIncludes

        callable_warn = ("operator.isCallable() is not supported in 3.x. "
                         "Use hasattr(obj, '__call__').")
        seq_warn = ("operator.sequenceIncludes() is not supported "
                    "in 3.x. Use operator.contains().")
        with check_py3k_warnings() as w:
            self.assertWarning(isCallable(self), w, callable_warn)
            w.reset()
            self.assertWarning(sequenceIncludes(range(3), 2), w, seq_warn)
	def __init__(self, delay, function, *params):
		try:
			if isCallable(function):
				global instanceTab
				instanceTab.append(self)
				self.function = function
				self.params = params
				self.timer = None
				self.timer = eTimer()
				self.timer.timeout.get().append(self.timerLaunch)
				self.timer.start(delay, False)
		except Exception, e:
			emcDebugOut("[spDF] __init__ exception:\n%s:%s" %(str(self.function),str(e)))
Exemple #19
0
 def GetDockerRunCommand(self):
   command = ["/usr/bin/docker", "run", "--rm"]
   command.extend(["--name", self.name])
   for name, value in self.environment.items():
     if operator.isCallable(value):
       value = value()
     command.extend(["-e", "{}={}".format(name, value)])
   for value in self.ports:
     if operator.isCallable(value):
       value = value()
     command.extend(["-p", str(value)])
   for value in self.volumes:
     if operator.isCallable(value):
       value = value()
     if isinstance(value, tuple):
       host_path, container_path = value
       value = host_path + ":" + container_path
     command.extend(["-v", str(value)])
   command.append(self.container)
   if self.command:
     command.extend(self.command)
   return " ".join(map(pipes.quote, command))
Exemple #20
0
 def test_operator(self):
     import operator
     self.assertIs(operator.truth(0), False)
     self.assertIs(operator.truth(1), True)
     self.assertIs(operator.isCallable(0), False)
     self.assertIs(operator.isCallable(len), True)
     self.assertIs(operator.isNumberType(None), False)
     self.assertIs(operator.isNumberType(0), True)
     self.assertIs(operator.not_(1), False)
     self.assertIs(operator.not_(0), True)
     self.assertIs(operator.isSequenceType(0), False)
     self.assertIs(operator.isSequenceType([]), True)
     self.assertIs(operator.contains([], 1), False)
     self.assertIs(operator.contains([1], 1), True)
     self.assertIs(operator.isMappingType(1), False)
     self.assertIs(operator.isMappingType({}), True)
     self.assertIs(operator.lt(0, 0), False)
     self.assertIs(operator.lt(0, 1), True)
     self.assertIs(operator.is_(True, True), True)
     self.assertIs(operator.is_(True, False), False)
     self.assertIs(operator.is_not(True, True), False)
     self.assertIs(operator.is_not(True, False), True)
Exemple #21
0
 def sort(self,key):
     """
     This method modifies the sorting of the dictionary overriding the existing sort key.
     :param key: it can be a sequence containing all the keys already existing in the dictionary 
                 or a callable providing a sorting key algorithm.
     """
     import operator
     if operator.isCallable(key):
         self._keys = sorted(self._keys,key=key)
     else:
         for k in self._keys:
             if k not in self._keys: raise KeyError(k)
         self._keys = list(key)
     return self._keys[:]
Exemple #22
0
    def ctcpQuery_FINGER(self, user, channel, data):
        if data is not None:
            self.quirkyMessage("Why did %s send '%s' with a FINGER query?" %
                               (user, data))
        if not self.fingerReply:
            return

        if operator.isCallable(self.fingerReply):
            reply = self.fingerReply()
        else:
            reply = str(self.fingerReply)

        nick = string.split(user, "!")[0]
        self.ctcpMakeReply(nick, [('FINGER', reply)])
Exemple #23
0
 def showSettings(self):
     for x in dir(self):
         if x.startswith("_") or x in ["pe", "elf", "buf"] or operator.isCallable(eval("self." + x)) \
            or x in ["plugins", "names", "imports", "exports", "functions_address", \
                     "names", "functions", "xrefs_from", "xrefs_to", "antidebug", \
                     "function_stats", "basic_blocks"]:
             continue
         else:
             self.log("pyew." + x.ljust(16) + ":", eval("self." + x))
     self.log()
     self.log("Pyew Plugins:")
     self.log()
     for x in self.plugins:
         self.log(x.ljust(8), self.plugins[x].__doc__)
Exemple #24
0
 def test_operator(self):
     import operator
     self.assertIs(operator.truth(0), False)
     self.assertIs(operator.truth(1), True)
     with test_support.check_py3k_warnings():
         self.assertIs(operator.isCallable(0), False)
         self.assertIs(operator.isCallable(len), True)
     self.assertIs(operator.isNumberType(None), False)
     self.assertIs(operator.isNumberType(0), True)
     self.assertIs(operator.not_(1), False)
     self.assertIs(operator.not_(0), True)
     self.assertIs(operator.isSequenceType(0), False)
     self.assertIs(operator.isSequenceType([]), True)
     self.assertIs(operator.contains([], 1), False)
     self.assertIs(operator.contains([1], 1), True)
     self.assertIs(operator.isMappingType(1), False)
     self.assertIs(operator.isMappingType({}), True)
     self.assertIs(operator.lt(0, 0), False)
     self.assertIs(operator.lt(0, 1), True)
     self.assertIs(operator.is_(True, True), True)
     self.assertIs(operator.is_(True, False), False)
     self.assertIs(operator.is_not(True, True), False)
     self.assertIs(operator.is_not(True, False), True)
Exemple #25
0
 def getCachedObject(obj,methods=[],depth=10.,expire=3.,catched=False):
     """ @RISKY
     This method will try to apply Cached decorator to all methods 
     of an object. USE IT AT YOUR OWN RISK!!
     """
     klass = obj if isinstance(obj,type) else type(obj)
     if not methods:
         methods = [k for k,f in klass.__dict__.items() if isCallable(f)]
     for k in methods:
         try:
             m = Cached(getattr(klass,k),depth,expire,catched=catched)
             setattr(obj,k,m)
         except:pass
     return obj
Exemple #26
0
 def showSettings(self):
     for x in dir(self):
         if x.startswith("_") or x in ["pe", "elf", "buf"] or operator.isCallable(eval("self." + x)) \
            or x in ["plugins", "names", "imports", "exports", "functions_address", \
                     "names", "functions", "xrefs_from", "xrefs_to", "antidebug", \
                     "function_stats", "basic_blocks"]:
             continue
         else:
             self.log("pyew." + x.ljust(16) + ":", eval("self." + x))
     self.log()
     self.log("Pyew Plugins:")
     self.log()
     for x in self.plugins:
         self.log(x.ljust(8), self.plugins[x].__doc__)
Exemple #27
0
 def sort(self, key):
     """
     This method modifies the sorting of the dictionary overriding the existing sort key.
     :param key: it can be a sequence containing all the keys already existing in the dictionary 
                 or a callable providing a sorting key algorithm.
     """
     import operator
     if operator.isCallable(key):
         self._keys = sorted(self._keys, key=key)
     else:
         for k in self._keys:
             if k not in self._keys: raise KeyError(k)
         self._keys = list(key)
     return self._keys[:]
Exemple #28
0
    def ctcpQuery_FINGER(self, user, channel, data):
        if data is not None:
            self.quirkyMessage("Why did %s send '%s' with a FINGER query?"
                               % (user, data))
        if not self.fingerReply:
            return

        if operator.isCallable(self.fingerReply):
            reply = self.fingerReply()
        else:
            reply = str(self.fingerReply)

        nick = string.split(user,"!")[0]
        self.ctcpMakeReply(nick, [('FINGER', reply)])
Exemple #29
0
    def __load_sub_game_init_funs__(self):
        gamefuns = []
        binpath = TyContext.TYGlobal.path_bin()
        subdirs = os.listdir(binpath)
        for subdir in subdirs:
            if not os.path.isdir(binpath + '/' + subdir):
                continue
            if subdir[0] != '.' and not subdir in ('tyframework', ''):
                _game_pack_ = None
                try:
                    exec 'import %s as _game_pack_' % (subdir)
                except:
                    _game_pack_ = None
                if _game_pack_ != None:

                    initfun = getattr(_game_pack_, 'tygame_init', None)
                    if not isCallable(initfun):
                        initfun = None

                    startfun = getattr(_game_pack_, 'tygame_start', None)
                    if not isCallable(startfun):
                        startfun = None

                    if initfun != None:
                        gamefuns.append({
                            'typath': subdir,
                            'tygame_init': initfun,
                            'tygame_start': startfun
                        })
                        TyContext.ftlog.info('find game package of :',
                                             gamefuns[-1])
                    else:
                        TyContext.ftlog.info('find no game package of :',
                                             subdir)
                else:
                    TyContext.ftlog.info('find unknow 2 package of :', subdir)
        return gamefuns
Exemple #30
0
 def get_callable(key,executor=None):
     try:
         x = []
         if isinstance(key,basestring):
             trial(lambda:x.append(evalX(key)),lambda:x.append(None))
         return first(a for a in (
             key,
             x and x[0],
             isinstance(key,basestring) and getattr(executor,key,None), #key is a member of executor
             getattr(executor,'process',None), #executor is a process object
             executor, #executor is a callable
             #isinstance(key,basestring) and evalX(key), # key may be name of function
             ) if a and isCallable(a))
     except StopIteration,e:
         return None
Exemple #31
0
    def __add_handler__(self, router, api_style_ver):
        '''
        @deprecate
        此方法已经不提倡使用, 请使用 @http_request_entry 和 add_handler 方法
        '''
        self.__ctx__.ftlog.info('RunHttp Add Handler ->', router)

        if isCallable(getattr(router, 'getJsonPaths', None)):
            jsons = router.getJsonPaths()
            if jsons:
                for httppath, httpfun in jsons.items():
                    entry = {
                        'registed': 0,
                        'jsonp': 0,
                        'need_ip_filter': 0,
                        'extend_tag': None,
                        'response': 'json',
                        'httppath': httppath
                    }
                    self.__register_http_entry__(router, httpfun, entry,
                                                 api_style_ver)

        if isCallable(getattr(router, 'getHtmlPaths', None)):
            htmls = router.getHtmlPaths()
            if htmls:
                for httppath, httpfun in htmls.items():
                    entry = {
                        'registed': 0,
                        'jsonp': 0,
                        'need_ip_filter': 0,
                        'extend_tag': None,
                        'response': 'html',
                        'httppath': httppath
                    }
                    self.__register_http_entry__(router, httpfun, entry,
                                                 api_style_ver)
Exemple #32
0
    def fireEvent(self, event_type, event_value, listeners=None):
        """sends an event to all listeners or a specific one"""

        if listeners is None:
            listeners = self._listeners

        if listeners is None:
            return

        if not operator.isSequenceType(listeners):
            listeners = listeners,

        for listener in listeners:
            if isinstance(listener, weakref.ref) or isinstance(listener, BoundMethodWeakref):
                l = listener()
            else:
                l = listener
            if l is None:
                continue
            meth = getattr(l, 'eventReceived', None)
            if meth is not None and operator.isCallable(meth):
                l.eventReceived(self, event_type, event_value)
            elif operator.isCallable(l):
                l(self, event_type, event_value)
def dump(data):
    print type(data), "=>",
    # deprecated since version 2.0: User isinstance(x, collections.Callable) instead
    if operator.isCallable(data):
        print "CALLABLE",
    # deprecated since version 2.7: Use isinstance(x, collections.Mapping) instead
    if operator.isMappingType(data):
        print "MAPPING",
    # deprecated since version 2.7: Use isinstance(x, numbers.Number) instead
    if operator.isNumberType(data):
        print "NUMBER",
    # deprecated since version 2.7: Use isinstance(x, collections.Sequence) instead
    if operator.isSequenceType(data):
        print "SEQUENCE",
    print
Exemple #34
0
 def get_callable(key,executor=None):
     try:
         x = []
         if isinstance(key,basestring):
             trial(lambda:x.append(evalX(key)),lambda:x.append(None))
         return first(a for a in (
             key,
             x and x[0],
             isinstance(key,basestring) and getattr(executor,key,None), #key is a member of executor
             getattr(executor,'process',None), #executor is a process object
             executor, #executor is a callable
             #isinstance(key,basestring) and evalX(key), # key may be name of function
             ) if a and isCallable(a))
     except StopIteration,e:
         return None
Exemple #35
0
    def fireEvent(self, event_type, event_value, listeners=None):
        """sends an event to all listeners or a specific one"""

        if listeners is None:
            listeners = self._listeners

        if listeners is None:
            return

        if not operator.isSequenceType(listeners):
            listeners = listeners,

        for listener in listeners:
            if isinstance(listener, weakref.ref) or isinstance(listener, BoundMethodWeakref):
                l = listener()
            else:
                l = listener
            if l is None:
                continue
            meth = getattr(l, 'eventReceived', None)
            if meth is not None and operator.isCallable(meth):
                l.eventReceived(self, event_type, event_value)
            elif operator.isCallable(l):
                l(self, event_type, event_value)
Exemple #36
0
        def wrapper(chain):
            assert isCallable(chain)

            if fast:
                if not is_value:
                    chain_dict = cls._fast_coerce_chain
                else:
                    chain_dict = cls._fast_value_coerce_chain
            else:
                chain_dict = cls._coerce_chain

            for arg in args:
                if arg not in chain_dict:
                    chain_dict[arg] = []
                chain_dict[arg].append(chain)
            return chain
Exemple #37
0
    def return_reply(self,reply):
        command_obj = self.command_queue.popleft() if len(self.command_queue) > 0 else None

        if not self.subscriptions and len(self.command_queue) == 0:
            self.emit("idle")

        if command_obj and not command_obj.sub_command:
            if operator.isCallable(command_obj.callback):
                if reply and command_obj.command.lower() == 'hgetall':
                    i = 0
                    obj = Jso()
                    while i < len(reply):
                        key = str(reply[i])
                        val = reply[i+1]
                        obj[key] = val
                    reply = obj

                command_obj.call_callback(reply,None)
            else:
                logging.debug("no callback for reply: %s" % reply)

        elif self.subscriptions or (command_obj and command_obj.sub_command):
            if isinstance(reply,list):
                if reply[0] in ["subscribe","unsubscribe","psubscribe"] and reply[2] == 0:
                    self.subscriptions = False
                    logging.debug("All subscriptions removed, exiting pub/sub mode")
                if reply[0] not in ["message","pmessage","subscribe","unsubscribe","psubscribe"]:
                    raise TypeError("subscriptions are active but unknow reply type %s" % reply[0])

                try:
                    self.emit(*reply)
                except Exception:
                     logging.error("Uncaught exceptions in subscriptions.",
                                   exc_info=True)
            elif not self.closing:
                raise ValueError("subscriptions are active but got an invalid reply: %s" % str(reply))
        elif self.monitoring:
            l = reply.index(" ")
            timestamp = replice[:l]
            finds = re.finditer('[^"]+',reply[l:])
            args = []
            for find in finds:
                args.append(find.replace('"',''))
            self.emit("monitor",timestamp,args)
        else:
            raise ValueError("tornado-redis command queue state error. If you can reproduce this, please report it")
Exemple #38
0
	def __init__(self, delay, function, *params):
		try:
			if isCallable(function):
				global instanceTab
				instanceTab.append(self)
				self.function = function
				self.params = params
				self.timer = None
				self.timer = eTimer()
				self.timer_conn = None
				try:
					self.timer_conn = self.timer.timeout.connect(self.timerLaunch)
				except:
					self.timer.timeout.get().append(self.timerLaunch)
				self.timer.start(delay, False)
		except Exception, e:
			emcDebugOut("[spDF] __init__ exception:\n%s:%s" %(str(self.function),str(e)))
	def __init__(self, delay, function, *params):
		try:
			if isCallable(function):
				global instanceTab
				instanceTab.append(self)
				self.function = function
				self.params = params
				self.timer = None
				self.timer = eTimer()
				self.timer_conn = None
				try:
					self.timer_conn = self.timer.timeout.connect(self.timerLaunch)
				except:
					self.timer.timeout.get().append(self.timerLaunch)
				self.timer.start(delay, False)
		except Exception, e:
			pass
Exemple #40
0
    def __init__(self, parent=None):
        Qt.QWidget.__init__(self, parent)
        self._groups = {}

        layout = Qt.QGridLayout()
        self.setLayout(layout)

        wf = taurus.qt.qtgui.util.TaurusWidgetFactory()

        taurus_widgets = wf.getWidgets()

        demos = {}
        for widget_name in taurus_widgets:
            widget_module_name, widget_class = taurus_widgets[widget_name]
            internal_widget_module_name = widget_class.__module__
            if internal_widget_module_name in demos:
                continue
            internal_widget_module = sys.modules[internal_widget_module_name]
            if hasattr(internal_widget_module, "demo"):
                if operator.isCallable(internal_widget_module.demo):
                    demos[
                        internal_widget_module_name] = internal_widget_module.demo

        groups = set()

        for demo_name in demos.keys():
            parts = demo_name.split(".")
            group = parts[-2]
            groups.add(group)

        for group in groups:
            self.addGroup(group)

        for demo_name in sorted(demos.keys()):
            demo_func = demos[demo_name]
            parts = demo_name.split(".")
            group = parts[-2]
            if not demo_func.__doc__:
                continue
            try:
                self.addDemo(demo_func.__doc__, demo_func, group)
            except Exception, e:
                print 80 * "-"
                print "Problems adding demo", demo_name
                print e
Exemple #41
0
def wait(seconds,event=True,hook=None):
    """
    :param seconds: seconds to wait for
    :param event: if True (default) it uses a dummy Event, if False it uses time.sleep, if Event is passed then it calls event.wait(seconds)
    """
    r = 0
    try:
      if hook and isCallable(hook):
          Catched(hook)()
      r+=1
      if not event:
          time.sleep(seconds)
      elif hasattr(event,'wait'):
        try:
          event.wait(seconds)
        except Exception,e:
          raise e
      else:
Exemple #42
0
 def do_task(self,task=None,trace=False):
     """
     Executes an string or callable
     """
     trace = trace or self.trace
     task = task or self.task
     if trace: print 'In CronTab(%s).do_task(%s)'%(self.line,task)
     if isCallable(task):
         ret = task()
     elif isString(task):
         from fandango.linos import shell_command
         ret = shell_command(self.task)
     else:
         raise Exception('NotCallable/String')
     if self.keep:
         if self._queue.full(): self.get()
         self._queue.put(ret,False)
     if trace: 
         print 'CronTab(%s).do_task() => %s'%(self.line,ret)
Exemple #43
0
 def do_task(self,task=None,trace=False):
     """
     Executes an string or callable
     """
     trace = trace or self.trace
     task = task or self.task
     if trace: print 'In CronTab(%s).do_task(%s)'%(self.line,task)
     if isCallable(task):
         ret = task()
     elif isString(task):
         from fandango.linos import shell_command
         ret = shell_command(self.task)
     else:
         raise Exception('NotCallable/String')
     if self.keep:
         if self._queue.full(): self.get()
         self._queue.put(ret,False)
     if trace: 
         print 'CronTab(%s).do_task() => %s'%(self.line,ret)
Exemple #44
0
def getValuesByTagName(xml_node, tagName, default_type=str, default_value=''):
    _type_callable = isCallable(default_type)
    valuelist = []
    taglist = xml_node.getElementsByTagName(tagName)
    if 0 != len(taglist):
        for tagNode in taglist:
            if None != tagNode.firstChild:
                if _type_callable:
                    valuelist.append(default_type(
                        tagNode.firstChild.nodeValue))
                else:
                    valuelist.append(tagNode.firstChild.nodeValue)
    else:
        if _type_callable:
            valuelist.append(default_type(default_value))
        else:
            valuelist.append(default_value)

    return valuelist
Exemple #45
0
def ParseXDoc(UrlOrFNameOrDoc, xPaths, flNodes=0):
    if IsEmpty(UrlOrFNameOrDoc):
        return None

    if isinstance(UrlOrFNameOrDoc, (tuple, list)):
        pars = map(lambda doc: [doc, xPaths, flNodes], UrlOrFNameOrDoc)
        return map(lambda x: ParseXDoc(*x), pars)

    if isinstance(UrlOrFNameOrDoc, (basestring)):
        doc = xDoc(UrlOrFNameOrDoc)
    else:
        doc = UrlOrFNameOrDoc

    if IsEmpty(doc):
        return None

    if IsEmpty(xPaths):
        return None

    if isinstance(xPaths, (basestring)):
        if flNodes == 0:
            ret = xValues(doc, xPaths)
            return ret
        else:
            return xNodes(doc, xPaths)

    if op.isCallable(xPaths):
        return xPaths(doc)

    # check for dict
    if op.isMappingType(xPaths):
        pars = [[k, v] for k, v in xPaths.items()]
        ret = []
        for k, v in pars:
            dPar = ParseXDoc(doc, k, 1)
            ret1 = ParseXDoc(dPar, v, flNodes)
            ret.append(ret1)
        return ret

    #check for list (return just values)
    if isinstance(xPaths, (tuple, list)):
        return [ParseXDoc(doc, xPath, 0) for xPath in xPaths]
def httpdigest(*args, **kwargs):
    '''
    May be used in one of three ways:
    * as a decorator factory (with the arguments being parameters to an instance of
    HttpDigestAuthenticator used to protect the decorated view)
    * as a decorator (protecting the decorated view with a default-constructed instance of
    HttpDigestAuthenticator)
    * as a decorator factory (with the argument being a pre-constructed HttpDigestAuthenticator
    instance used to protect the decorated view)
    '''
    if len(args) == 1 and not kwargs and isCallable(args[0]):
        authenticator = HttpDigestAuthenticator()
        return decorator(partial(_httpdigest, authenticator), args[0])

    if len(args) == 1 and not kwargs and isinstance(args[0],
                                                    HttpDigestAuthenticator):
        authenticator = args[0]
    else:
        authenticator = HttpDigestAuthenticator(*args, **kwargs)

    return lambda (f): decorator(partial(_httpdigest, authenticator), f)
Exemple #47
0
    def _get_configure_(self,
                        redisfullkey,
                        defaultvalue=None,
                        datatype=DATA_TYPE_STR,
                        clientid=None,
                        decodeutf8=False):
        isclientid = 0
        if clientid == None or len(clientid) <= 0:
            rkey = redisfullkey
        else:
            isclientid = 1
            rkey = redisfullkey + ':' + clientid
        value = self._cache_._get_cache_data_(rkey, datatype, decodeutf8)
        if value == None and isclientid == 1:
            rkey = redisfullkey + ':default'
            value = self._cache_._get_cache_data_(rkey, datatype, decodeutf8)
        if value == None:
            value = defaultvalue

        if value != None:
            if datatype == DATA_TYPE_JSON:
                assert (isinstance(value, (list, dict)))
            elif datatype == DATA_TYPE_INT:
                assert (isinstance(value, int))
            elif datatype == DATA_TYPE_FLOAT:
                assert (isinstance(value, float))
            elif datatype == DATA_TYPE_STR:
                assert (isinstance(value, (unicode, str)))
            elif datatype == DATA_TYPE_UNICODE:
                assert (isinstance(value, (unicode, str)))
            elif datatype == DATA_TYPE_HASH_SET:
                assert (isinstance(value, set))
            elif datatype == DATA_TYPE_FUNCTION:
                assert (isCallable(value))
            else:
                assert (isinstance(value, (str, unicode, int, float)))
            #         self.__ctx__.ftlog.debug('_get_configure_->', rkey, '=', value)
        return value
Exemple #48
0
def selectElements(datasets, axis, testFct=None, master=None, linplace=False, lall=False):
  ''' Extract common points that meet a specific criterion from a list of datasets. 
      The test function has to accept the following input: index, dataset, axis'''
  if linplace: raise NotImplementedError, "Option 'linplace' does not work currently."
  # check input
  if not isinstance(datasets, (list,tuple,Ensemble)): raise TypeError
  if not all(isinstance(dataset,Dataset) for dataset in datasets): raise TypeError 
  if not isCallable(testFct) and testFct is not None: raise TypeError
  if isinstance(axis, Axis): axis = axis.name
  if not isinstance(axis, basestring): raise TypeError
  if lall and master is not None: raise ArgumentError, "The options 'lall' and 'imaster' are mutually exclusive!"
  # save some ensemble parameters for later  
  lnotest = testFct is None
  lens = isinstance(datasets,Ensemble)
  if lens:
    enskwargs = dict(basetype=datasets.basetype, idkey=datasets.idkey, 
                     name=datasets.name, title=datasets.title) 
  # use dataset with shortest axis as master sample (more efficient)
  axes = [dataset.getAxis(axis) for dataset in datasets]
  if master is None: imaster = np.argmin([len(ax) for ax in axes]) # find shortest axis
  elif isinstance(master,basestring): 
    # translate name of dataset into index
    imaster = None
    for i,dataset in enumerate(datasets): 
      if dataset.name == master: 
        imaster = i; break
    if imaster is None: raise ArgumentError, "Master '{:s}' not found in datasets".format(master)
  else: imaster = master
  if not imaster is None and not isinstance(imaster,(int,np.integer)): raise TypeError, imaster
  elif imaster >= len(datasets) or imaster < 0: raise ValueError 
  maxis = axes.pop(imaster) # extraxt shortest axis for loop
  if lall: 
    tmpds = tuple(datasets)
    if imaster != 0: tmpds = (tmpds[imaster],)+tmpds[:imaster]+tmpds[imaster+1:]
    test_fct = lambda i,ds: testFct(i, ds, axis) # prepare test function arguments
  else: 
    test_fct = lambda i: testFct(i, datasets[imaster], axis) 
  # loop over coordinate axis
  itpls = [] # list of valid index tuple
  for i,x in enumerate(maxis.coord):
    # check other axes
    if all([x in ax.coord for ax in axes]): # only the other axes
      # no condition
      if lnotest:
        # just find and add indices
        itpls.append((i,)+tuple(ax.coord.searchsorted(x) for ax in axes))
      # check condition using shortest dataset
      elif lall: 
        # check test condition on all datasets (slower)
        tmpidx = (i,)+tuple(ax.coord.searchsorted(x) for ax in axes)
        if all(test_fct(ii,ds) for ii,ds in zip(tmpidx,tmpds)):
          # add corresponding indices in each dataset to list
          itpls.append(tmpidx)
      else:
        # check test condition on only one dataset (faster, default)
        if test_fct(i):
          # add corresponding indices in each dataset to list
          itpls.append((i,)+tuple(ax.coord.searchsorted(x) for ax in axes))
          # N.B.: since we can expect exact matches, plain searchsorted is fastest (side='left') 
  # check if there is anything left...
  if len(itpls) == 0: raise DatasetError, "Aborting: no data points match all criteria!"
  # construct axis indices for each dataset (need to remember to move shortest axis back in line)
  idxs = [[] for ds in datasets] # create unique empty lists
  for itpl in itpls:
    for i,idx in enumerate(itpl): idxs[i].append(idx)
  idxs.insert(imaster,idxs.pop(0)) # move first element back in line (where shortest axis was)
  idxs = [np.asarray(idxlst, dtype='int') for idxlst in idxs]      
  # slice datasets using only positive results  
  datasets = [ds(lidx=True, linplace=linplace, **{axis:idx}) for ds,idx in zip(datasets,idxs)]
  if lens: datasets = Ensemble(*datasets, **enskwargs)
  # return datasets
  return datasets
Exemple #49
0
 def check(self, o, v):
     self.assertEqual(operator.isCallable(o), v)
     self.assertEqual(callable(o), v)
 def check(self, o, v):
     with test_support.check_py3k_warnings():
         self.assertEqual(operator.isCallable(o), v)
         self.assertEqual(callable(o), v)
def addTupleTool(self, tool, name=None):
    """Correctly adds a TupleTool to a DecayTreeTuple or Branch instance, so that the user doesn't need to do the logic themselves
  tool can be any TupleTool, either the bare class, instance, string with '::' or string with '__'
  name must be a string
  examples:
  mytuple.addTupleTool('LoKi::Hybrid::TupleTool/LoKi_J')
    mytuple.addTupleTool('LoKi::Hybrid::TupleTool')
    mytuple.addTupleTool(LoKi__Hybrid__TupleTool)
    mytuple.addTupleTool('LoKi__Hybrid__TupleTool')
    mytuple.addTupleTool(LoKi__Hybrid__TupleTool,'LoKi_J')
    mytuple.addTupleTool(LoKi__Hybrid__TupleTool('Shared_J'))
  """

    ############## Step 1: check you're not doing something stupid ######################
    import operator
    if name is not None:
        if type(name) is not str:
            raise TypeError, ('expected string for name, got ' +
                              str(type(name)) + ' instead')

    if type(tool) is not str and not operator.isCallable(tool):
        if 'getFullName' not in dir(tool):
            raise TypeError, (
                'tool instance must be a string or configurable, got ' +
                str(type(tool)) + ' instead')
        mother, atype, aname = tool.splitName()
        if aname == atype and tool.__class__.__name__ == aname and tool.isPublic(
        ):
            raise TypeError, (
                'You are trying to add a default public tool-configurable to your ntuple: '
                + tool.getFullName() +
                ' This is dangerous so not allowed with addTTool.' +
                ' Either add it yourself manually, or, better, supply an instance name'
            )
    if type(tool) is not str and operator.isCallable(tool):
        import GaudiKernel
        if type(tool) is not GaudiKernel.ConfigurableMeta.ConfigurableMeta:
            raise TypeError, ("Expected a bare configurable, got a " +
                              str(type(tool)) + " instead")
    #will fail here if you haven't supplied an ntuple!
    if 'ToolList' not in dir(self):
        raise TypeError, (
            "You are calling addTupleTool to something which hasn't got the ability to own TupleTools "
            + str(type(self)))

    if type(tool) is not str and not operator.isCallable(
            tool) and name is not None:
        mother, atype, aname = tool.splitName()
        if aname != name:
            raise NameError, (
                'You have supplied an instance, but also specified a different name '
                + atype + ' ' + name + ' ' + aname +
                '. Supply the bare class or strings instead if an instance.')
        name = None
    if type(tool) is str and name is not None and '/' in tool:
        tool, name2 = self.__splitname__(tool)
        if name2 != name:
            raise NameError, ('You have supplied two different names for ' +
                              tool + ' ' + name + ' ' + name2)
        name = None

    config = None
    if self.ToolList is None:
        self.ToolList = []

    tooltype = ''
    toolname = ''
    toolinstance = ''

    ################# Step 2: retrieve the configurable for the tool in question ############
    if type(tool) is str:
        tool, name2 = self.__splitname__(tool)
        if name2 is not None:
            name = name2
        #will fail here if the configurable doesn't exist
        try:
            exec('from Configurables import ' + tool)
            exec('config = ' + tool)
        except ImportError:
            raise ImportError, (
                'The TupleTool ' + tool +
                ' does not exist, check the name and try again')
    else:
        config = tool

    ################# Step 3: add to Self ###################################################
    if name is None: self.addTool(config)
    else: self.addTool(config, name)

    ################# Step 4: add to ToolList and return the instance of the configurable ###
    instance = None

    if type(tool) is not str and not operator.isCallable(config):
        #if a configurable was supplied I need to find its name twice ...
        mother, tool, name = config.splitName()
        if (tool == name): tool = config.__class__.__name__

    if type(tool) is not str and operator.isCallable(config):
        #if a bare configurable was supplied I need to change the type to a string.. not easy to do that!
        import GaudiKernel
        tool = GaudiKernel.ConfigurableMeta.ConfigurableMeta.__repr__(tool)
        tool = tool.split("'")[-2]
        tool = tool.split(".")[-1]

    if name is None:
        exec('instance=self.' + tool)
    else:
        exec('instance=self.' + name)

    #mother,tool,name=instance.splitName()

    if instance.getFullName() in self.ToolList:
        raise AttributeError, (
            'The tool ' + instance.getFullName() +
            ' was already added to the ToolList, remove and try again')
    elif instance.getFullName().split('/')[0] == instance.getFullName().split(
            '/')[-1] and instance.getFullName().split('/')[0] in self.ToolList:
        raise AttributeError, (
            'The tool ' + instance.getFullName().split('/')[-1] +
            ' was already added to the ToolList, remove and try again')
    #elif (tool==name and name in self.ToolList):
    #  raise AttributeError, ('The tool '+tool+' was already added to the ToolList, remove and try again')

    if instance.getFullName().split('/')[0] == instance.getFullName().split(
            '/')[-1]:
        self.ToolList.append(instance.getFullName().split('/')[0])
    else:
        self.ToolList.append(instance.getFullName())

    return instance
def test_tuple_new():
    # TypeError: tuple.__new__(str): str is not a subtype of tuple
    #!!!AssertError(TypeError, tuple.__new__, str)
    #!!!AssertError(TypeError, tuple.__new__, str, 'abc')
    pass

########################################################################
# stand-alone tests

# override pow, delete it, and it should be gone
import operator
pow = 7
AreEqual(pow, 7)
del pow
AreEqual(operator.isCallable(pow), True)

try:
    del pow
    AreEqual(True,False)
except NameError:
    pass

@skip("silverlight", "Merlin bug #404247: this test doesn't work when the file is executed from non-Python host (thost)" )
def test_file():
    AreEqual(file_var_present, 1)

file_var_present = vars().keys().count('__file__')

run_test(__name__)
Exemple #53
0
 def all(cls):
     query = Session.query(cls).filter()
     if hasattr(cls, 'order_by') and isCallable(cls.order_by):
         query = cls.order_by(query)
     return query
Exemple #54
0
 def filter_active(cls):
     query = Session.query(cls).filter(cls.active == True)
     if hasattr(cls, 'order_by') and isCallable(cls.order_by):
         query = cls.order_by(query)
     return query.all()
 def check(self, o, v):
     self.assert_(operator.isCallable(o) == callable(o) == v)
Exemple #56
0
 def _log(self, msg, force=False):
     if isCallable(self.log):
         self.log(msg)
     elif self.log or force:
         print(self.func, msg)
Exemple #57
0
 def _log(self, msg):
     if isCallable(self.log):
         self.log(msg)
     elif self.log:
         print(msg)
def wrap2callable(f, **kwargs):
    """
    Allow constants, string formulas, discrete data points,
    user-defined functions and (callable) classes to be wrapped
    in a new callable function. That is, all the mentioned data
    structures can be used as a function, usually of space and/or
    time.
    (kwargs is used for string formulas)

    >>> from py4cs.numpytools import *
    >>> f1 = wrap2callable(2.0)
    >>> f1(0.5)
    2.0
    >>> f2 = wrap2callable('1+2*x')
    >>> f2(0.5)
    2.0
    >>> f3 = wrap2callable('1+2*t', independent_variables='t')
    >>> f3(0.5)
    2.0
    >>> f4 = wrap2callable('a+b*t')
    >>> f4(0.5)
    Traceback (most recent call last):
    ...
    NameError: name 'a' is not defined
    >>> f4 = wrap2callable('a+b*t', independent_variables='t', \
                           a=1, b=2)
    >>> f4(0.5)
    2.0

    >>> x = seq(0,1,0.5); y=1+2*x
    >>> f5 = wrap2callable((x,y))
    >>> f5(0.5)
    2.0
    >>> def myfunc(x):  return 1+2*x
    >>> f6 = wrap2callable(myfunc)
    >>> f6(0.5)
    2.0
    >>> f7 = wrap2callable(lambda x: 1+2*x)
    >>> f7(0.5)
    2.0
    >>> class MyClass:
            def __call__(self, x):
                return 1+2*x

    >>> myclass = MyClass()
    >>> f8 = wrap2callable(myclass)
    >>> f8(0.5)
    2.0
    >>> # 3D functions:
    >>> f9 = wrap2callable('1+2*x+3*y+4*z', \
                           independent_variables=('x','y','z'))
    >>> f9(0.5,1/3.,0.25)
    4.0
    >>> # discrete 3D data:
    >>> y = seq(0,1,0.5); z = seq(-1,0.5,0.1)
    >>> xc = x[:,NewAxis,NewAxis]; yc = y[NewAxis,:,NewAxis]
    >>> zc = z[NewAxis,NewAxis,:]
    >>> def myfunc3(x,y,z):  return 1+2*x+3*y+4*z

    >>> values = myfunc3(xc,yc,zc)
    >>> f10 = wrap2callable((x,y,z,values))
    >>> f10(0.5,1/3.,0.25)
    4.0
    >>> 
    """
    if isinstance(f, str):
        return StringFunction(f, **kwargs)
    elif isinstance(f, (float, int)):
        return WrapNo2Callable(f)
    elif isinstance(f, (list, tuple)):
        return WrapDiscreteData2Callable(f)
    elif operator.isCallable(f):
        return f
    else:
        raise TypeError, 'f of type %s is not callable' % type(f)
Exemple #59
0
def evalX(target,
          _locals=None,
          modules=None,
          instances=None,
          _trace=False,
          _exception=Exception):
    """
    evalX is an enhanced eval function capable of evaluating multiple types 
    and import modules if needed.
    
    The _locals/modules/instances dictionaries WILL BE UPDATED with the result
    of the code! (if '=' or import are used)
    
    It is used by some fandango classes to send python code to remote threads;
    that will evaluate and return the values as pickle objects.
    
    target may be:
         - dictionary of built-in types (pickable): 
                {'__target__':callable or method_name,
                '__args__':[],'__class_':'',
                '__module':'','__class_args__':[]}
         - string to eval: eval('import $MODULE' or '$VAR=code()' or 'code()')
         - list if list[0] is callable: value = list[0](*list[1:]) 
         - callable: value = callable()
    """
    import imp, __builtin__

    # Only if immutable types are passed as arguments these dictionaries will
    # be preserved.
    _locals = notNone(_locals, {})
    modules = notNone(modules, {})
    instances = notNone(instances, {})

    def import_module(module, reload=False):
        # This method is re-implemented in objects module for avoiding
        # inter-dependency between modules
        module = module.split('import ', 1)[-1].strip().split()[0]
        alias = module.split(' as ', 1)[-1] if ' as ' in module else module
        if reload or alias not in modules:
            if '.' not in module:
                modules[module] = imp.load_module(module,
                                                  *imp.find_module(module))
            else:
                parent, child = module.rsplit('.', 1)
                mparent = import_module(parent)
                setattr(
                    mparent, child,
                    imp.load_module(module,
                                    *imp.find_module(child, mparent.__path__)))
                modules[module] = getattr(mparent, child)
            if alias:
                modules[alias] = modules[module]
                _locals[alias] = modules[alias]
        if _trace:
            print('%s(%s) : %s' % (alias, module, modules[alias]))

        return modules[module]

    def get_instance(_module, _klass, _klass_args):
        if (_module, _klass, _klass_args) not in instances:
            instances[(_module,_klass,_klass_args)] = \
                getattr(import_module(_module),klass)(*klass_args)
        return instances[(_module, _klass, _klass_args)]

    if 'import_module' not in _locals:
        _locals['import_module'] = lambda m: import_module(m, reload=True)

    if isDictionary(target):
        model = target
        keywords = [
            '__args__', '__target__', '__class__', '__module__',
            '__class_args__'
        ]
        args = model['__args__'] if '__args__' in model \
            else dict((k,v) for k,v in model.items() if k not in keywords)
        target = model.get('__target__', None)
        module = model.get('__module__', None)
        klass = model.get('__class__', None)
        klass_args = model.get('__class_args__', tuple())

        if isCallable(target):
            target = model['__target__']

        elif isString(target):
            if module:
                #module,subs = module.split('.',1)
                if klass:
                    if _trace:
                        print('evalX: %s.%s(%s).%s(%s)' %
                              (module, klass, klass_args, target, args))
                    target = getattr(get_instance(module, klass, klass_args),
                                     target)
                else:
                    if _trace:
                        print('evalX: %s.%s(%s)' % (module, target, args))
                    target = getattr(import_module(module), target)

            elif klass and klass in dir(__builtin__):
                if _trace:
                    print('evalX: %s(%s).%s(%s)' %
                          (klass, klass_args, target, args))
                instance = getattr(__builtin__, klass)(*klass_args)
                target = getattr(instance, target)

            elif target in dir(__builtin__):
                if _trace: print('evalX: %s(%s)' % (target, args))
                target = getattr(__builtin__, target)
            else:
                raise _exception('%s()_MethodNotFound' % target)
        else:
            raise _exception('%s()_NotCallable' % target)

        value = target(**args) if isDictionary(args) else target(*args)
        if _trace: print('%s: %s' % (model, value))

        return value
    else:
        #Parse: method[0](*method[1:])
        if isIterable(target) and isCallable(target[0]):
            value = target[0](*target[1:])

        #Parse: method()
        elif isCallable(target):
            value = target()

        elif isString(target):
            if _trace: print('evalX("%s")' % target)

            #Parse: import $MODULE
            if target.startswith('import ') or ' import ' in target:
                #Modules dictionary is updated here
                value = import_module(target)
                #value = target

            #Parse: $VAR = #code
            elif ('=' in target and '=' != target.split('=', 1)[1][0]
                  and re.match('[A-Za-z\._]+[A-Za-z0-9\._]*$',
                               target.split('=', 1)[0].strip())):
                var = target.split('=', 1)[0].strip()
                _locals[var] = eval(
                    target.split('=', 1)[1].strip(), modules, _locals)
                value = var
            #Parse: #code
            else:
                value = eval(target, modules, _locals)
        else:
            raise _exception('targetMustBeCallable, not %s(%s)' %
                             (type(target), target))

        if _trace: print('Out of evalX(%s): %s' % (target, value))
    return value