我们从Python开源项目中,提取了以下42个代码示例,用于说明如何使用operator.is_()。
def _validate_query_state(self): for attr, methname, notset, op in ( ('_limit', 'limit()', None, operator.is_), ('_offset', 'offset()', None, operator.is_), ('_order_by', 'order_by()', False, operator.is_), ('_group_by', 'group_by()', False, operator.is_), ('_distinct', 'distinct()', False, operator.is_), ( '_from_obj', 'join(), outerjoin(), select_from(), or from_self()', (), operator.eq) ): if not op(getattr(self.query, attr), notset): raise sa_exc.InvalidRequestError( "Can't call Query.update() or Query.delete() " "when %s has been called" % (methname, ) )
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 number_of_args(fn): """Return the number of positional arguments for a function, or None if the number is variable. Looks inside any decorated functions.""" try: if hasattr(fn, '__wrapped__'): return number_of_args(fn.__wrapped__) if any(p.kind == p.VAR_POSITIONAL for p in signature(fn).parameters.values()): return None else: return sum(p.kind in (p.POSITIONAL_ONLY, p.POSITIONAL_OR_KEYWORD) for p in signature(fn).parameters.values()) except ValueError: # signatures don't work for built-in operators, so check for a few explicitly UNARY_OPS = [len, op.not_, op.truth, op.abs, op.index, op.inv, op.invert, op.neg, op.pos] BINARY_OPS = [op.lt, op.le, op.gt, op.ge, op.eq, op.ne, op.is_, op.is_not, op.add, op.and_, op.floordiv, op.lshift, op.mod, op.mul, op.or_, op.pow, op.rshift, op.sub, op.truediv, op.xor, op.concat, op.contains, op.countOf, op.delitem, op.getitem, op.indexOf] TERNARY_OPS = [op.setitem] if fn in UNARY_OPS: return 1 elif fn in BINARY_OPS: return 2 elif fn in TERNARY_OPS: return 3 else: raise NotImplementedError("Bult-in operator {} not supported".format(fn))
def add_globals(self): "Add some Scheme standard procedures." import math, cmath, operator as op from functools import reduce self.update(vars(math)) self.update(vars(cmath)) self.update({ '+':op.add, '-':op.sub, '*':op.mul, '/':op.itruediv, 'níl':op.not_, 'agus':op.and_, '>':op.gt, '<':op.lt, '>=':op.ge, '<=':op.le, '=':op.eq, 'mod':op.mod, 'frmh':cmath.sqrt, 'dearbhluach':abs, 'uas':max, 'íos':min, 'cothrom_le?':op.eq, 'ionann?':op.is_, 'fad':len, 'cons':cons, 'ceann':lambda x:x[0], 'tóin':lambda x:x[1:], 'iarcheangail':op.add, 'liosta':lambda *x:list(x), 'liosta?': lambda x:isa(x,list), 'folamh?':lambda x: x == [], 'adamh?':lambda x: not((isa(x, list)) or (x == None)), 'boole?':lambda x: isa(x, bool), 'scag':lambda f, x: list(filter(f, x)), 'cuir_le':lambda proc,l: proc(*l), 'mapáil':lambda p, x: list(map(p, x)), 'lódáil':lambda fn: load(fn), 'léigh':lambda f: f.read(), 'oscail_comhad_ionchuir':open,'dún_comhad_ionchuir':lambda p: p.file.close(), 'oscail_comhad_aschur':lambda f:open(f,'w'), 'dún_comhad_aschur':lambda p: p.close(), 'dac?':lambda x:x is eof_object, 'luacháil':lambda x: evaluate(x), 'scríobh':lambda x,port=sys.stdout:port.write(to_string(x) + '\n')}) return self
def test_is(self): a = b = 'xyzpdq' c = a[:3] + b[3:] self.assertRaises(TypeError, operator.is_) self.assertTrue(operator.is_(a, b)) self.assertFalse(operator.is_(a,c))
def test_operator(self): import operator self.assertIs(operator.truth(0), False) self.assertIs(operator.truth(1), True) self.assertIs(operator.not_(1), False) self.assertIs(operator.not_(0), True) self.assertIs(operator.contains([], 1), False) self.assertIs(operator.contains([1], 1), 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 exclude_tags(tags, excluded, relation=operator.is_): """Filter out tags that are related to at least one of the excluded set.""" return [t for t in make_bs4_iterable(tags) if not any(relation(t, s) for s in make_bs4_iterable(excluded))]
def restrict_tags(tags, included, relation=operator.is_): """Restrict to tags that are related to at least one of the included set.""" return [t for t in make_bs4_iterable(tags) if any(relation(t, s) for s in make_bs4_iterable(included))] # finding tags by chaining
def test_is(self): a = b = 'xyzpdq' c = a[:3] + b[3:] self.assertRaises(TypeError, operator.is_) self.assertTrue(operator.is_(a, b)) self.assertFalse(operator.is_(a,c)) #brython fix me (syntax error) #def test_is_not(self): # a = b = 'xyzpdq' # c = a[:3] + b[3:] # self.assertRaises(TypeError, operator.is_not) # self.assertFalse(operator.is_not(a, b)) # self.assertTrue(operator.is_not(a,c))
def compare(value1, value2, comparision): import operator if comparision == 'is': return operator.is_(value1, value2) elif comparision == 'or': return operator.or_(value1, value2) elif comparision == 'and': return operator.and_(value1, value2) # Use Euler's Formula