我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用operator.floordiv()。
def test_modulus_basic(self): dt = np.typecodes['AllInteger'] + np.typecodes['Float'] for dt1, dt2 in itertools.product(dt, dt): for sg1, sg2 in itertools.product((+1, -1), (+1, -1)): if sg1 == -1 and dt1 in np.typecodes['UnsignedInteger']: continue if sg2 == -1 and dt2 in np.typecodes['UnsignedInteger']: continue fmt = 'dt1: %s, dt2: %s, sg1: %s, sg2: %s' msg = fmt % (dt1, dt2, sg1, sg2) a = np.array(sg1*71, dtype=dt1)[()] b = np.array(sg2*19, dtype=dt2)[()] div = self.floordiv(a, b) rem = self.mod(a, b) assert_equal(div*b + rem, a, err_msg=msg) if sg2 == -1: assert_(b < rem <= 0, msg) else: assert_(b > rem >= 0, msg)
def test_float_modulus_roundoff(self): # gh-6127 dt = np.typecodes['Float'] for dt1, dt2 in itertools.product(dt, dt): for sg1, sg2 in itertools.product((+1, -1), (+1, -1)): fmt = 'dt1: %s, dt2: %s, sg1: %s, sg2: %s' msg = fmt % (dt1, dt2, sg1, sg2) a = np.array(sg1*78*6e-8, dtype=dt1)[()] b = np.array(sg2*6e-8, dtype=dt2)[()] div = self.floordiv(a, b) rem = self.mod(a, b) # Equal assertion should hold when fmod is used assert_equal(div*b + rem, a, err_msg=msg) if sg2 == -1: assert_(b < rem <= 0, msg) else: assert_(b > rem >= 0, msg)
def test_division(self): t = timedelta(hours=1, minutes=24, seconds=19) second = timedelta(seconds=1) self.assertEqual(t / second, 5059.0) self.assertEqual(t // second, 5059) t = timedelta(minutes=2, seconds=30) minute = timedelta(minutes=1) self.assertEqual(t / minute, 2.5) self.assertEqual(t // minute, 2) zerotd = timedelta(0) self.assertRaises(ZeroDivisionError, truediv, t, zerotd) self.assertRaises(ZeroDivisionError, floordiv, t, zerotd) # self.assertRaises(TypeError, truediv, t, 2) # note: floor division of a timedelta by an integer *is* # currently permitted.
def __init__(self, *generators): super().__init__(*generators, reduce_lambda=operator.floordiv)
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 test_floordiv_scalar(self): with testing.NumpyError(divide='ignore'): self.check_array_scalar_op(operator.floordiv)
def test_broadcasted_floordiv(self): with testing.NumpyError(divide='ignore'): self.check_array_broadcasted_op(operator.floordiv)
def test_binops(self): ops = [operator.add, operator.sub, operator.mul, operator.floordiv, operator.truediv, pow] scalars = [-1, 1, 2] idxs = [RangeIndex(0, 10, 1), RangeIndex(0, 20, 2), RangeIndex(-10, 10, 2), RangeIndex(5, -5, -1)] for op in ops: for a, b in combinations(idxs, 2): result = op(a, b) expected = op(Int64Index(a), Int64Index(b)) tm.assert_index_equal(result, expected) for idx in idxs: for scalar in scalars: result = op(idx, scalar) expected = op(Int64Index(idx), scalar) tm.assert_index_equal(result, expected)
def test_arith(self): self._test_op(self.panel, operator.add) self._test_op(self.panel, operator.sub) self._test_op(self.panel, operator.mul) self._test_op(self.panel, operator.truediv) self._test_op(self.panel, operator.floordiv) self._test_op(self.panel, operator.pow) self._test_op(self.panel, lambda x, y: y + x) self._test_op(self.panel, lambda x, y: y - x) self._test_op(self.panel, lambda x, y: y * x) self._test_op(self.panel, lambda x, y: y / x) self._test_op(self.panel, lambda x, y: y ** x) self._test_op(self.panel, lambda x, y: x + y) # panel + 1 self._test_op(self.panel, lambda x, y: x - y) # panel - 1 self._test_op(self.panel, lambda x, y: x * y) # panel * 1 self._test_op(self.panel, lambda x, y: x / y) # panel / 1 self._test_op(self.panel, lambda x, y: x ** y) # panel ** 1 self.assertRaises(Exception, self.panel.__add__, self.panel['ItemA'])
def test_arith_flex_panel(self): ops = ['add', 'sub', 'mul', 'div', 'truediv', 'pow', 'floordiv', 'mod'] if not compat.PY3: aliases = {} else: aliases = {'div': 'truediv'} self.panel = self.panel.to_panel() for n in [np.random.randint(-50, -1), np.random.randint(1, 50), 0]: for op in ops: alias = aliases.get(op, op) f = getattr(operator, alias) exp = f(self.panel, n) result = getattr(self.panel, op)(n) assert_panel_equal(result, exp, check_panel_type=True) # rops r_f = lambda x, y: f(y, x) exp = r_f(self.panel, n) result = getattr(self.panel, 'r' + op)(n) assert_panel_equal(result, exp)
def test_binary_operators(self): # skipping for now ##### raise nose.SkipTest("skipping sparse binary operators test") def _check_inplace_op(iop, op): tmp = self.bseries.copy() expected = op(tmp, self.bseries) iop(tmp, self.bseries) assert_sp_series_equal(tmp, expected) inplace_ops = ['add', 'sub', 'mul', 'truediv', 'floordiv', 'pow'] for op in inplace_ops: _check_inplace_op(getattr(operator, "i%s" % op), getattr(operator, op))
def __floordiv__(self, trc): return self.apply_op2(trc, operator.floordiv)
def test_div (self): c1 = pygame.Color (0x80808080) self.assertEquals (c1.r, 128) self.assertEquals (c1.g, 128) self.assertEquals (c1.b, 128) self.assertEquals (c1.a, 128) c2 = pygame.Color (2, 4, 8, 16) self.assertEquals (c2.r, 2) self.assertEquals (c2.g, 4) self.assertEquals (c2.b, 8) self.assertEquals (c2.a, 16) c3 = c1 // c2 self.assertEquals (c3.r, 64) self.assertEquals (c3.g, 32) self.assertEquals (c3.b, 16) self.assertEquals (c3.a, 8) c3 = c3 // c2 self.assertEquals (c3.r, 32) self.assertEquals (c3.g, 8) self.assertEquals (c3.b, 2) self.assertEquals (c3.a, 0) # Issue #286: Is type checking done for Python 3.x? self.assertRaises (TypeError, operator.floordiv, c1, None) self.assertRaises (TypeError, operator.floordiv, None, c1) # Division by zero check dividend = pygame.Color (255, 255, 255, 255) for i in range (4): divisor = pygame.Color (64, 64, 64, 64) divisor[i] = 0 quotient = pygame.Color (3, 3, 3, 3) quotient[i] = 0 self.assertEqual (dividend // divisor, quotient)
def test_issue52(self): u1 = UnitRegistry() u2 = UnitRegistry() q1 = 1*u1.meter q2 = 1*u2.meter import operator as op for fun in (op.add, op.iadd, op.sub, op.isub, op.mul, op.imul, op.floordiv, op.ifloordiv, op.truediv, op.itruediv): self.assertRaises(ValueError, fun, q1, q2)
def _test_quantity_floordiv(self, unit, func): a = self.Q_('10*meter') b = self.Q_('3*second') self.assertRaises(DimensionalityError, op.floordiv, a, b) self.assertRaises(DimensionalityError, op.floordiv, 3, b) self.assertRaises(DimensionalityError, op.floordiv, a, 3) self.assertRaises(DimensionalityError, op.ifloordiv, a, b) self.assertRaises(DimensionalityError, op.ifloordiv, 3, b) self.assertRaises(DimensionalityError, op.ifloordiv, a, 3) func(op.floordiv, unit * 10.0, '4.2*meter/meter', 2, unit) func(op.floordiv, '10*meter', '4.2*inch', 93, unit)
def test_float_modulus_exact(self): # test that float results are exact for small integers. This also # holds for the same integers scaled by powers of two. nlst = list(range(-127, 0)) plst = list(range(1, 128)) dividend = nlst + [0] + plst divisor = nlst + plst arg = list(itertools.product(dividend, divisor)) tgt = list(divmod(*t) for t in arg) a, b = np.array(arg, dtype=int).T # convert exact integer results from Python to float so that # signed zero can be used, it is checked. tgtdiv, tgtrem = np.array(tgt, dtype=float).T tgtdiv = np.where((tgtdiv == 0.0) & ((b < 0) ^ (a < 0)), -0.0, tgtdiv) tgtrem = np.where((tgtrem == 0.0) & (b < 0), -0.0, tgtrem) for dt in np.typecodes['Float']: msg = 'dtype: %s' % (dt,) fa = a.astype(dt) fb = b.astype(dt) # use list comprehension so a_ and b_ are scalars div = [self.floordiv(a_, b_) for a_, b_ in zip(fa, fb)] rem = [self.mod(a_, b_) for a_, b_ in zip(fa, fb)] assert_equal(div, tgtdiv, err_msg=msg) assert_equal(rem, tgtrem, err_msg=msg)
def check_floordiv(Poly): c1 = list(random((4,)) + .5) c2 = list(random((3,)) + .5) c3 = list(random((2,)) + .5) p1 = Poly(c1) p2 = Poly(c2) p3 = Poly(c3) p4 = p1 * p2 + p3 c4 = list(p4.coef) assert_poly_almost_equal(p4 // p2, p1) assert_poly_almost_equal(p4 // c2, p1) assert_poly_almost_equal(c4 // p2, p1) assert_poly_almost_equal(p4 // tuple(c2), p1) assert_poly_almost_equal(tuple(c4) // p2, p1) assert_poly_almost_equal(p4 // np.array(c2), p1) assert_poly_almost_equal(np.array(c4) // p2, p1) assert_poly_almost_equal(2 // p2, Poly([0])) assert_poly_almost_equal(p2 // 2, 0.5*p2) assert_raises( TypeError, op.floordiv, p1, Poly([0], domain=Poly.domain + 1)) assert_raises( TypeError, op.floordiv, p1, Poly([0], window=Poly.window + 1)) if Poly is Polynomial: assert_raises(TypeError, op.floordiv, p1, Chebyshev([0])) else: assert_raises(TypeError, op.floordiv, p1, Polynomial([0]))
def test_floordiv_scalar(self): with testing.NumpyError(divide='ignore'): self.check_array_scalar_op(operator.floordiv, no_complex=True)
def test_rfloordiv_scalar(self): with testing.NumpyError(divide='ignore'): self.check_array_scalar_op(operator.floordiv, swap=True, no_complex=True)
def test_floordiv_array(self): with testing.NumpyError(divide='ignore'): self.check_array_array_op(operator.floordiv, no_complex=True)
def test_broadcasted_floordiv(self): with testing.NumpyError(divide='ignore'): self.check_array_broadcasted_op(operator.floordiv, no_complex=True)
def test_doubly_broadcasted_floordiv(self): with testing.NumpyError(divide='ignore'): self.check_array_doubly_broadcasted_op(operator.floordiv, no_complex=True)
def test_floordiv(self): self.assertRaises(TypeError, operator.floordiv, 5) self.assertRaises(TypeError, operator.floordiv, None, None) self.assertTrue(operator.floordiv(5, 2) == 2)
def test_complex(self): self.assertFalse(issubclass(complex, Real)) self.assertTrue(issubclass(complex, Complex)) c1, c2 = complex(3, 2), complex(4,1) # XXX: This is not ideal, but see the comment in math_trunc(). self.assertRaises(TypeError, math.trunc, c1) self.assertRaises(TypeError, operator.mod, c1, c2) self.assertRaises(TypeError, divmod, c1, c2) self.assertRaises(TypeError, operator.floordiv, c1, c2) self.assertRaises(TypeError, float, c1) self.assertRaises(TypeError, int, c1)
def __floordiv__(self, y): return NonStandardInteger(operator.floordiv(self.val, y))
def __rfloordiv__(self, y): return NonStandardInteger(operator.floordiv(y, self.val))
def __floordiv__(self, other): assert type(other) in scalar_types return Vector2(operator.floordiv(self.x, other), operator.floordiv(self.y, other))
def __rfloordiv__(self, other): assert type(other) in scalar_types return Vector2(operator.floordiv(other, self.x), operator.floordiv(other, self.y))
def __floordiv__(self, other): assert type(other) in scalar_types return Vector3(operator.floordiv(self.x, other), operator.floordiv(self.y, other), operator.floordiv(self.z, other))
def __rfloordiv__(self, other): assert type(other) in scalar_types return Vector3(operator.floordiv(other, self.x), operator.floordiv(other, self.y), operator.floordiv(other, self.z))
def apply_floordiv(left: Column, right): if type(right) == Column: result, index = apply_fast_floordiv(left.values, right.values, left.index, right.index) return Column(result, index) else: return Column(operator.floordiv(left.values, right), left.index)
def test_div(self): self.assertRaises(TypeError, operator.div, 5) self.assertRaises(TypeError, operator.div, None, None) self.assertTrue(operator.floordiv(5, 2) == 2)
def test_rfloordiv_scalar(self): with testing.NumpyError(divide='ignore'): self.check_array_scalar_op(operator.floordiv, swap=True)
def test_floordiv_array(self): with testing.NumpyError(divide='ignore'): self.check_array_array_op(operator.floordiv)