我们从Python开源项目中,提取了以下21个代码示例,用于说明如何使用operator.isCallable()。
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)
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 #@staticmethod
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 iif(condition,truepart,falsepart=None,forward=False): """ if condition is boolean return (falsepart,truepart)[condition] if condition is callable returns truepart if condition(tp) else falsepart if forward is True condition(truepart) is returned instead of truepart if forward is callable, forward(truepart) is returned instead """ if isCallable(condition): v = condition(truepart) if not v: return falsepart elif not condition: return falsepart if isCallable(forward): return forward(truepart) elif forward: return v else: return truepart
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)
def _log(self,msg): if isCallable(self.log): self.log(msg) elif self.log: print(msg)
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
def decorate(self,target): if isCallable(target): self._log('decorate(%s)'%str(target)) self.f = target #self.call = wraps(self.f)(self.__call__) #Not for methods!! functools.update_wrapper(self,self.f) else: self.f = None
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[:]
def test_isCallable(self): self.assertRaises(TypeError, operator.isCallable) class C: pass def check(self, o, v): with test_support.check_py3k_warnings(): self.assertEqual(operator.isCallable(o), v) self.assertEqual(callable(o), v) check(self, 4, 0) check(self, operator.isCallable, 1) check(self, C, 1) check(self, C(), 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)