我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用operator.inv()。
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 __inv__(self): return self.apply_op1(operator.inv)
def test_invert(self): self.assertRaises(TypeError, operator.invert) self.assertRaises(TypeError, operator.invert, None) self.assertEqual(operator.inv(4), -5)
def test_invert(self): self.assertRaises(TypeError, operator.invert) self.assertRaises(TypeError, operator.invert, None) self.assertTrue(operator.inv(4) == -5)
def __neg__(self): values = _values_from_object(self) if values.dtype == np.bool_: arr = operator.inv(values) else: arr = operator.neg(values) return self.__array_wrap__(arr)
def __invert__(self): try: arr = operator.inv(_values_from_object(self)) return self.__array_wrap__(arr) except: # inv fails with 0 len if not np.prod(self.shape): return self raise
def qs_filter(cls, qs, flt, value, process_value=True): """ Private method to set WHERE part of query. If required, Django-style filter is available via qs.filter() and peewee.DQ - this method provides joins. Filter relational operators are: * NOT - '-', not operator, should be user as prefix * < - 'lt', less than * > - 'gt', greater than * <= - 'lte', less than or equal * >= - 'gte', greater than or equal * != - 'ne', not equal * LIKE - 'like', classic like operator * ILIKE - 'ilike', case-insensitive like operator * IN - 'in', classic in. Values should be separated by comma * ISNULL - 'isnull', operator to know if smth is equal to null. Use -<fieldname>__isnull for IS NOT NULL """ neg = False if flt[0] in '-': # Register NOT filter clause neg = True flt = flt[1:] fld_name, _, k = flt.rpartition('__') if not fld_name: # No underscore, simple filter fld_name, k = k, '' # Get filter op = FILTER_MAP.get(k, operator.eq) if neg: _op = op op = lambda f, x: operator.inv(_op(f, x)) # Get field from model # raised AttributeError should be handled on higher level fld = getattr(cls.model_cls, fld_name) # Additional value processing if process_value: _v = value.decode() if isinstance(fld, peewee.BooleanField) and _v in ('0', 'f'): # Assume that '0' and 'f' are FALSE for boolean field _v = False elif k == 'in': # Force set parameter to list _v = _v.split(',') elif k == 'isnull': # ISNULL. Force set parameter to None _v = None else: _v = value # Send parameter to ORM return qs.where(op(fld, _v))
def test_logical_operators(self): def _check_bin_op(op): result = op(df1, df2) expected = DataFrame(op(df1.values, df2.values), index=df1.index, columns=df1.columns) self.assertEqual(result.values.dtype, np.bool_) assert_frame_equal(result, expected) def _check_unary_op(op): result = op(df1) expected = DataFrame(op(df1.values), index=df1.index, columns=df1.columns) self.assertEqual(result.values.dtype, np.bool_) assert_frame_equal(result, expected) df1 = {'a': {'a': True, 'b': False, 'c': False, 'd': True, 'e': True}, 'b': {'a': False, 'b': True, 'c': False, 'd': False, 'e': False}, 'c': {'a': False, 'b': False, 'c': True, 'd': False, 'e': False}, 'd': {'a': True, 'b': False, 'c': False, 'd': True, 'e': True}, 'e': {'a': True, 'b': False, 'c': False, 'd': True, 'e': True}} df2 = {'a': {'a': True, 'b': False, 'c': True, 'd': False, 'e': False}, 'b': {'a': False, 'b': True, 'c': False, 'd': False, 'e': False}, 'c': {'a': True, 'b': False, 'c': True, 'd': False, 'e': False}, 'd': {'a': False, 'b': False, 'c': False, 'd': True, 'e': False}, 'e': {'a': False, 'b': False, 'c': False, 'd': False, 'e': True}} df1 = DataFrame(df1) df2 = DataFrame(df2) _check_bin_op(operator.and_) _check_bin_op(operator.or_) _check_bin_op(operator.xor) # operator.neg is deprecated in numpy >= 1.9 _check_unary_op(operator.inv)