Python operator 模块,mul() 实例源码

我们从Python开源项目中,提取了以下49个代码示例,用于说明如何使用operator.mul()

项目:wurst    作者:IndEcol    | 项目源码 | 文件源码
def get_comma_separated_data(raw):
    # Convert to long string
    header, data = "".join(raw).strip().split(" = ")

    # Remove trailing comma
    assert data[-1] == ';'
    data = data[:-1]

    # Remove newline characters and convert to list
    data = eval(data.replace("\n", ''))

    shape = tuple(eval(header[header.index("["):header.index("]") + 1]))
    step_size = functools.reduce(operator.mul, shape) + 1
    years = np.array(data[::step_size], dtype=int)

    data = np.stack([
        np.array(data[1 + index * step_size:(index + 1) * step_size]).reshape(shape)
        for index in range(len(years))
    ], axis=-1)

    return header, years, data
项目:wurst    作者:IndEcol    | 项目源码 | 文件源码
def get_space_separated_data(raw):
    assert raw[0].strip().endswith("= [")
    assert raw[-1].strip().endswith("];")

    header = raw[0].replace("= [", "").strip()
    shape = tuple(eval(header[header.index("["):header.index("]") + 1]))
    data = [eval(line.strip().replace("  ", ",")) for line in raw[1:-1]]

    if len(shape) == 1:
        step_size = 1
    else:
        step_size = functools.reduce(operator.mul, shape[:-1])

    years = np.array(data[::step_size + 1], dtype=int)

    subarrays = [
        np.array(data[index * (step_size + 1) + 1:(index + 1) * (step_size + 1)]).reshape(shape)
        for index in range(len(years))
    ]
    return header, years, np.stack(subarrays, axis=-1)
项目:pykit    作者:baishancloud    | 项目源码 | 文件源码
def test_combine(self):
        cases = (
            ({'a': 2},
             {'a': 3},
             operator.mul,
             None,
             {'a': 6}),

            ({'a': 2},
             {'a': 3},
             operator.mul,
             {'a': True},
             {'a': 2}),
        )

        for a, b, op, exclude, expected in cases:
            result = dictutil.combine(a, b, op, exclude=exclude)
            self.assertIsNot(a, result)
            self.assertDictEqual(expected, result,
                                 repr([a, b, op, exclude, expected, result]))

            result = dictutil.combineto(a, b, op, exclude=exclude)
            self.assertIs(a, result)
            self.assertDictEqual(expected, result,
                                 repr([a, b, op, exclude, expected, result]))
项目:Software-Architecture-with-Python    作者:PacktPublishing    | 项目源码 | 文件源码
def factorial(n):
    """ Factorial of a number.

    >>> factorial(0)
    1
    >>> factorial(1)
    1
    >>> factorial(5)
    120
    >>> factorial(10)
    3628800
    """

    # Handle 0 as a special case
    if n == 0:
        return 1

    return functools.reduce(operator.mul, range(1,n+1))
项目:radar    作者:amoose136    | 项目源码 | 文件源码
def test_safe_binop():
    # Test checked arithmetic routines

    ops = [
        (operator.add, 1),
        (operator.sub, 2),
        (operator.mul, 3)
    ]

    with exc_iter(ops, INT64_VALUES, INT64_VALUES) as it:
        for xop, a, b in it:
            pyop, op = xop
            c = pyop(a, b)

            if not (INT64_MIN <= c <= INT64_MAX):
                assert_raises(OverflowError, mt.extint_safe_binop, a, b, op)
            else:
                d = mt.extint_safe_binop(a, b, op)
                if c != d:
                    # assert_equal is slow
                    assert_equal(d, c)
项目:radar    作者:amoose136    | 项目源码 | 文件源码
def test_datafriendly_mul(self):
        # Test keeping data w/ (inplace) multiplication
        # Test mul w/ scalar
        x = array([1, 2, 3], mask=[0, 0, 1])
        xx = x * 2
        assert_equal(xx.data, [2, 4, 3])
        assert_equal(xx.mask, [0, 0, 1])
        # Test imul w/ scalar
        x = array([1, 2, 3], mask=[0, 0, 1])
        x *= 2
        assert_equal(x.data, [2, 4, 3])
        assert_equal(x.mask, [0, 0, 1])
        # Test mul w/ array
        x = array([1, 2, 3], mask=[0, 0, 1])
        xx = x * array([10, 20, 30], mask=[1, 0, 0])
        assert_equal(xx.data, [1, 40, 3])
        assert_equal(xx.mask, [1, 0, 1])
        # Test imul w/ array
        x = array([1, 2, 3], mask=[0, 0, 1])
        x *= array([10, 20, 30], mask=[1, 0, 0])
        assert_equal(x.data, [1, 40, 3])
        assert_equal(x.mask, [1, 0, 1])
项目:taskcv-2017-public    作者:VisionLearningGroup    | 项目源码 | 文件源码
def _read_datafile(self, path, expected_dims):
        """Helper function to read a file in IDX format."""
        base_magic_num = 2048
        with gzip.GzipFile(path) as f:
            magic_num = struct.unpack('>I', f.read(4))[0]
            expected_magic_num = base_magic_num + expected_dims
            if magic_num != expected_magic_num:
                raise ValueError('Incorrect MNIST magic number (expected '
                                 '{}, got {})'
                                 .format(expected_magic_num, magic_num))
            dims = struct.unpack('>' + 'I' * expected_dims,
                                 f.read(4 * expected_dims))
            buf = f.read(reduce(operator.mul, dims))
            data = np.frombuffer(buf, dtype=np.uint8)
            data = data.reshape(*dims)
            return data
项目:qrn    作者:uwnlp    | 项目源码 | 文件源码
def variable_with_weight_decay(name, shape, stddev, wd):
    """Helper to create an initialized Variable with weight decay.

    Note that the Variable is initialized with a truncated normal distribution.
    A weight decay is added only if one is specified.

    Args:
      name: name of the variable
      shape: list of ints
      stddev: standard deviation of a truncated Gaussian
      wd: add L2Loss weight decay multiplied by this float. If None, weight
          decay is not added for this Variable.

    Returns:
      Variable Tensor
    """
    var = variable_on_cpu(name, shape,
                           tf.truncated_normal_initializer(stddev=stddev))
    if wd:
        weight_decay = tf.mul(tf.nn.l2_loss(var), wd, name='weight_loss')
        tf.add_to_collection('losses', weight_decay)
    return var
项目:cupy    作者:cupy    | 项目源码 | 文件源码
def _generate_normal(self, func, size, dtype, *args):
        # curand functions below don't support odd size.
        # * curand.generateNormal
        # * curand.generateNormalDouble
        # * curand.generateLogNormal
        # * curand.generateLogNormalDouble
        size = core.get_size(size)
        element_size = six.moves.reduce(operator.mul, size, 1)
        if element_size % 2 == 0:
            out = cupy.empty(size, dtype=dtype)
            func(self._generator, out.data.ptr, out.size, *args)
            return out
        else:
            out = cupy.empty((element_size + 1,), dtype=dtype)
            func(self._generator, out.data.ptr, out.size, *args)
            return out[:element_size].reshape(size)

    # NumPy compatible functions
项目:TF_MemN2N-tableQA    作者:vendi12    | 项目源码 | 文件源码
def categorical_accuracy(y_true, y_pred, mask=True):
    '''
    categorical_accuracy adjusted for padding mask
    '''
    # if mask is not None:
    print y_true
    print y_pred
    eval_shape = (reduce(mul, y_true.shape[:-1]), y_true.shape[-1])
    print eval_shape
    y_true_ = np.reshape(y_true, eval_shape)
    y_pred_ = np.reshape(y_pred, eval_shape)
    flat_mask = np.flatten(mask)
    comped = np.equal(np.argmax(y_true_, axis=-1),
                      np.argmax(y_pred_, axis=-1))
    ## not sure how to do this in tensor flow
    good_entries = flat_mask.nonzero()[0]
    return np.mean(np.gather(comped, good_entries))

    # else:
    #     return K.mean(K.equal(K.argmax(y_true, axis=-1),
    #                           K.argmax(y_pred, axis=-1)))
项目:veros    作者:dionhaefner    | 项目源码 | 文件源码
def tdma(a, b, c, d, workgrp_size=None):
    assert a.shape == b.shape == c.shape == d.shape
    assert a.dtype == b.dtype == c.dtype == d.dtype

    # Check that PyOpenCL is installed and that the Bohrium runtime uses the OpenCL backend
    if not bh.interop_pyopencl.available():
        raise NotImplementedError("OpenCL not available")

    # Get the OpenCL context from Bohrium
    ctx = bh.interop_pyopencl.get_context()
    queue = cl.CommandQueue(ctx)

    ret = bh.empty(a.shape, dtype=a.dtype)
    a_buf, b_buf, c_buf, d_buf, ret_buf = map(bh.interop_pyopencl.get_buffer, (a, b, c, d, ret))

    prg = compile_tdma(ret.shape[-1], bh.interop_pyopencl.type_np2opencl_str(a.dtype))
    global_size = functools.reduce(operator.mul, ret.shape[:-1])
    prg.tdma(queue, [global_size], workgrp_size, a_buf, b_buf, c_buf, d_buf, ret_buf)
    return ret
项目:sidekick    作者:fabiommendes    | 项目源码 | 文件源码
def test_reduceby(self):
        data = [1, 2, 3, 4, 5]

        def iseven(x): return x % 2 == 0
        assert reduceby(iseven, add, data, 0) == {False: 9, True: 6}
        assert reduceby(iseven, mul, data, 1) == {False: 15, True: 8}

        projects = [{'name': 'build roads', 'state': 'CA', 'cost': 1000000},
                    {'name': 'fight crime', 'state': 'IL', 'cost': 100000},
                    {'name': 'help farmers', 'state': 'IL', 'cost': 2000000},
                    {'name': 'help farmers', 'state': 'CA', 'cost': 200000}]
        assert reduceby(lambda x: x['state'],
                        lambda acc, x: acc + x['cost'],
                        projects, 0) == {'CA': 1200000, 'IL': 2100000}

        assert reduceby('state',
                        lambda acc, x: acc + x['cost'],
                        projects, 0) == {'CA': 1200000, 'IL': 2100000}
项目:deb-python-lesscpy    作者:openstack    | 项目源码 | 文件源码
def operate(self, left, right, operation):
        """ Do operation on colors
        args:
            left (str): left side
            right (str): right side
            operation (str): Operation
        returns:
            str
        """
        operation = {
            '+': operator.add,
            '-': operator.sub,
            '*': operator.mul,
            '/': operator.truediv
        }.get(operation)
        return operation(left, right)
项目:bi-att-flow    作者:allenai    | 项目源码 | 文件源码
def variable_with_weight_decay(name, shape, stddev, wd):
    """Helper to create an initialized Variable with weight decay.

    Note that the Variable is initialized with a truncated normal distribution.
    A weight decay is added only if one is specified.

    Args:
      name: name of the variable
      shape: list of ints
      stddev: standard deviation of a truncated Gaussian
      wd: add L2Loss weight decay multiplied by this float. If None, weight
          decay is not added for this Variable.

    Returns:
      Variable Tensor
    """
    var = variable_on_cpu(name, shape,
                           tf.truncated_normal_initializer(stddev=stddev))
    if wd:
        weight_decay = tf.mul(tf.nn.l2_loss(var), wd, name='weight_loss')
        tf.add_to_collection('losses', weight_decay)
    return var
项目:QGL    作者:BBN-Q    | 项目源码 | 文件源码
def create_tomo_blocks(qubits, numPulses, alignment='parallel'):
    '''
    Helper function to create the tomography pulse block in either parallel or serial.
    '''
    #Tomography pulse sets
    if numPulses == 4:
        tomoSet = [Id, X90, Y90, X]
    elif numPulses == 6:
        tomoSet = [Id, X90, X90m, Y90, Y90m, X]
    else:
        raise ValueError("Only able to handle numPulses=4 or 6")

    #Create all combinations of pulses for the number of qubits
    if alignment == 'parallel':
        return [reduce(operator.mul, [p(q) for p, q in zip(pulseSet, qubits)])
                for pulseSet in product(tomoSet, repeat=len(qubits))]
    elif alignment == 'serial':
        return [[p(q) for p, q in zip(pulseSet, qubits)]
                for pulseSet in product(tomoSet, repeat=len(qubits))]
    else:
        raise ValueError("Alignment must be either serial or parallel")
项目:QGL    作者:BBN-Q    | 项目源码 | 文件源码
def state_tomo(seq, qubits, numPulses=4, measChans=None):
    '''
    Apply state tomography readout pulses and measurement.

    Parameters
    -----------
    seq : a single entry list sequence to perform tomography on
    qubits : which qubits to act on
    numPulses : number of readout pulses
    measChans : tuple of measurement channels to readout (defaults to individual qubit channels)
    '''
    if measChans is None:
        measChans = qubits
    measBlock = reduce(operator.mul, [MEAS(q) for q in measChans])

    return [seq + [tomoBlock, measBlock]
            for tomoBlock in create_tomo_blocks(qubits, numPulses)]
项目:QGL    作者:BBN-Q    | 项目源码 | 文件源码
def process_tomo(seq, qubits, numPulses=4, measChans=None):
    '''
    Apply process tomography state prep. and readout pulses and measurement.

    Parameters
    -----------
    seq : a single entry list sequence to perform tomography on
    qubits : which qubits to act on
    numPulses : number of prep/readout pulses
    measChans : tuple of measurement channels to readout (defaults to individual qubit channels)
    '''
    if measChans is None:
        measChans = qubits
    measBlock = reduce(operator.mul, [MEAS(q) for q in measChans])

    seqs = []
    for k in range(numPulses**len(qubits)):
        for readoutBlock in create_tomo_blocks(qubits, numPulses):
            prepBlock = create_tomo_blocks(qubits, numPulses)[k]
            tomoseq = [prepBlock] + seq + [readoutBlock, measBlock]
            seqs.append(tomoseq)
    return seqs
项目:ESPEI    作者:PhasesResearchLab    | 项目源码 | 文件源码
def get_samples(desired_data):
    all_samples = []
    for data in desired_data:
        temperatures = np.atleast_1d(data['conditions']['T'])
        num_configs = np.array(data['solver'].get('sublattice_configurations'), dtype=np.object).shape[0]
        site_fractions = data['solver'].get('sublattice_occupancies', [[1]] * num_configs)
        site_fraction_product = [reduce(operator.mul, list(itertools.chain(*[np.atleast_1d(f) for f in fracs])), 1)
                                 for fracs in site_fractions]
        # TODO: Subtle sorting bug here, if the interactions aren't already in sorted order...
        interaction_product = []
        for fracs in site_fractions:
            interaction_product.append(float(reduce(operator.mul,
                                                    [f[0] - f[1] for f in fracs if isinstance(f, list) and len(f) == 2],
                                                    1)))
        if len(interaction_product) == 0:
            interaction_product = [0]
        comp_features = zip(site_fraction_product, interaction_product)
        all_samples.extend(list(itertools.product(temperatures, comp_features)))
    return all_samples
项目:Chinese-QA    作者:distantJing    | 项目源码 | 文件源码
def variable_with_weight_decay(name, shape, stddev, wd):
    """Helper to create an initialized Variable with weight decay.

    Note that the Variable is initialized with a truncated normal distribution.
    A weight decay is added only if one is specified.

    Args:
      name: name of the variable
      shape: list of ints
      stddev: standard deviation of a truncated Gaussian
      wd: add L2Loss weight decay multiplied by this float. If None, weight
          decay is not added for this Variable.

    Returns:
      Variable Tensor
    """
    var = variable_on_cpu(name, shape,
                           tf.truncated_normal_initializer(stddev=stddev))
    if wd:
        weight_decay = tf.mul(tf.nn.l2_loss(var), wd, name='weight_loss')
        tf.add_to_collection('losses', weight_decay)
    return var
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def prod(a, start=1):
    """Return product of elements of a. Start with int 1 so if only
       ints are included then an int result is returned.

    Examples
    ========

    >>> from sympy import prod, S
    >>> prod(range(3))
    0
    >>> type(_) is int
    True
    >>> prod([S(2), 3])
    6
    >>> _.is_Integer
    True

    You can start the product at something other than 1:
    >>> prod([1, 2], 3)
    6

    """
    return reduce(operator.mul, a, start)
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def pde_separate_mul(eq, fun, sep):
    """
    Helper function for searching multiplicative separable solutions.

    Consider an equation of two independent variables x, y and a dependent
    variable w, we look for the product of two functions depending on different
    arguments:

    `w(x, y, z) = X(x)*u(y, z)`

    Examples
    ========

    >>> from sympy import Function, Eq, pde_separate_mul, Derivative as D
    >>> from sympy.abc import x, y
    >>> u, X, Y = map(Function, 'uXY')

    >>> eq = Eq(D(u(x, y), x, 2), D(u(x, y), y, 2))
    >>> pde_separate_mul(eq, u(x, y), [X(x), Y(y)])
    [Derivative(X(x), x, x)/X(x), Derivative(Y(y), y, y)/Y(y)]

    """
    return pde_separate(eq, fun, sep, strategy='mul')
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def _rebuild_expr(self, expr, mapping):
        domain = self.domain

        def _rebuild(expr):
            generator = mapping.get(expr)

            if generator is not None:
                return generator
            elif expr.is_Add:
                return reduce(add, list(map(_rebuild, expr.args)))
            elif expr.is_Mul:
                return reduce(mul, list(map(_rebuild, expr.args)))
            elif expr.is_Pow and expr.exp.is_Integer:
                return _rebuild(expr.base)**int(expr.exp)
            else:
                try:
                    return domain.convert(expr)
                except CoercionFailed:
                    if not domain.has_Field and domain.has_assoc_Field:
                        return domain.get_field().convert(expr)
                    else:
                        raise

        return _rebuild(sympify(expr))
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def _rebuild_expr(self, expr, mapping):
        domain = self.domain

        def _rebuild(expr):
            generator = mapping.get(expr)

            if generator is not None:
                return generator
            elif expr.is_Add:
                return reduce(add, list(map(_rebuild, expr.args)))
            elif expr.is_Mul:
                return reduce(mul, list(map(_rebuild, expr.args)))
            elif expr.is_Pow and expr.exp.is_Integer and expr.exp >= 0:
                return _rebuild(expr.base)**int(expr.exp)
            else:
                return domain.convert(expr)

        return _rebuild(sympify(expr))
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def mul(self, *objs):
        """
        Multiply a sequence of polynomials or containers of polynomials.

        Example
        -------
        >>> from sympy.polys.rings import ring
        >>> from sympy.polys.domains import ZZ

        >>> R, x = ring("x", ZZ)
        >>> R.mul([ x**2 + 2*i + 3 for i in range(4) ])
        x**8 + 24*x**6 + 206*x**4 + 744*x**2 + 945
        >>> _.factor_list()
        (1, [(x**2 + 3, 1), (x**2 + 5, 1), (x**2 + 7, 1), (x**2 + 9, 1)])

        """
        p = self.one

        for obj in objs:
            if is_sequence(obj, include=GeneratorType):
                p *= self.mul(*obj)
            else:
                p *= obj

        return p
项目:krpcScripts    作者:jwvanderbeck    | 项目源码 | 文件源码
def test_safe_binop():
    # Test checked arithmetic routines

    ops = [
        (operator.add, 1),
        (operator.sub, 2),
        (operator.mul, 3)
    ]

    with exc_iter(ops, INT64_VALUES, INT64_VALUES) as it:
        for xop, a, b in it:
            pyop, op = xop
            c = pyop(a, b)

            if not (INT64_MIN <= c <= INT64_MAX):
                assert_raises(OverflowError, mt.extint_safe_binop, a, b, op)
            else:
                d = mt.extint_safe_binop(a, b, op)
                if c != d:
                    # assert_equal is slow
                    assert_equal(d, c)
项目:krpcScripts    作者:jwvanderbeck    | 项目源码 | 文件源码
def check_mul(Poly):
    c1 = list(random((4,)) + .5)
    c2 = list(random((3,)) + .5)
    p1 = Poly(c1)
    p2 = Poly(c2)
    p3 = p1 * p2
    assert_poly_almost_equal(p2 * p1, p3)
    assert_poly_almost_equal(p1 * c2, p3)
    assert_poly_almost_equal(c2 * p1, p3)
    assert_poly_almost_equal(p1 * tuple(c2), p3)
    assert_poly_almost_equal(tuple(c2) * p1, p3)
    assert_poly_almost_equal(p1 * np.array(c2), p3)
    assert_poly_almost_equal(np.array(c2) * p1, p3)
    assert_poly_almost_equal(p1 * 2, p1 * Poly([2]))
    assert_poly_almost_equal(2 * p1, p1 * Poly([2]))
    assert_raises(TypeError, op.mul, p1, Poly([0], domain=Poly.domain + 1))
    assert_raises(TypeError, op.mul, p1, Poly([0], window=Poly.window + 1))
    if Poly is Polynomial:
        assert_raises(TypeError, op.mul, p1, Chebyshev([0]))
    else:
        assert_raises(TypeError, op.mul, p1, Polynomial([0]))
项目:idascripts    作者:ctfhacker    | 项目源码 | 文件源码
def list(**type):
    """List all the enumerations within the database.

    Search type can be identified by providing a named argument.
    like = glob match
    regex = regular expression
    index = particular index
    identifier = particular id number
    pred = function predicate
    """
    res = __builtin__.list(iterate(**type))

    maxindex = max(__builtin__.map(idaapi.get_enum_idx, res))
    maxname = max(__builtin__.map(utils.compose(idaapi.get_enum_name, len), res))
    maxsize = max(__builtin__.map(size, res))
    cindex = math.ceil(math.log(maxindex or 1)/math.log(10))
    cmask = max(__builtin__.map(utils.compose(mask, math.log, functools.partial(operator.mul, 1.0/math.log(16)), math.ceil), res) or [database.config.bits()/4.0])

    for n in res:
        print("[{:{:d}d}] {:>{:d}s} & {:#<{:d}x} ({:d} members){:s}".format(idaapi.get_enum_idx(n), int(cindex), idaapi.get_enum_name(n), maxname, mask(n), int(cmask), len(__builtin__.list(members(n))), " // {:s}".format(comment(n)) if comment(n) else ''))
    return

## members
项目:oil    作者:oilshell    | 项目源码 | 文件源码
def test_optimized_concat(self):
        x = 'x' * MAX_Py_ssize_t
        try:
            x = x + '?'     # this statement uses a fast path in ceval.c
        except OverflowError:
            pass
        else:
            self.fail("should have raised OverflowError")
        try:
            x += '?'        # this statement uses a fast path in ceval.c
        except OverflowError:
            pass
        else:
            self.fail("should have raised OverflowError")
        self.assertEqual(len(x), MAX_Py_ssize_t)

    ### the following test is pending a patch
    #   (http://mail.python.org/pipermail/python-dev/2006-July/067774.html)
    #@bigaddrspacetest
    #def test_repeat(self):
    #    self.assertRaises(OverflowError, operator.mul, 'x', MAX_Py_ssize_t + 1)
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
def test_optimized_concat(self):
        x = 'x' * MAX_Py_ssize_t
        try:
            x = x + '?'     # this statement uses a fast path in ceval.c
        except OverflowError:
            pass
        else:
            self.fail("should have raised OverflowError")
        try:
            x += '?'        # this statement uses a fast path in ceval.c
        except OverflowError:
            pass
        else:
            self.fail("should have raised OverflowError")
        self.assertEqual(len(x), MAX_Py_ssize_t)

    ### the following test is pending a patch
    #   (http://mail.python.org/pipermail/python-dev/2006-July/067774.html)
    #@bigaddrspacetest
    #def test_repeat(self):
    #    self.assertRaises(OverflowError, operator.mul, 'x', MAX_Py_ssize_t + 1)
项目:exercism-python    作者:unfo    | 项目源码 | 文件源码
def largest_product(txt, n):
    if len(txt) < n or n < 0:
        raise ValueError("Input length is less than n")

    if any(not c.isdigit() for c in txt):
        raise ValueError("Input must be numeric")

    if n == 0:
        return 1

    products = [reduce(mul, grp) for grp in slices(txt, n)]
    largest = reduce(max, products)

    return largest


# from series.py
项目:download-manager    作者:thispc    | 项目源码 | 文件源码
def __init__(self, code, objects=None):
        self._OPERATORS = [
            ('|', operator.or_),
            ('^', operator.xor),
            ('&', operator.and_),
            ('>>', operator.rshift),
            ('<<', operator.lshift),
            ('-', operator.sub),
            ('+', operator.add),
            ('%', operator.mod),
            ('/', operator.truediv),
            ('*', operator.mul),
        ]
        self._ASSIGN_OPERATORS = [(op + '=', opfunc)
                                  for op, opfunc in self._OPERATORS]
        self._ASSIGN_OPERATORS.append(('=', lambda cur, right: right))
        self._VARNAME_PATTERN = r'[a-zA-Z_$][a-zA-Z_$0-9]*'

        if objects is None:
            objects = {}
        self.code = code
        self._functions = {}
        self._objects = objects
项目:orizonhub    作者:gumblex    | 项目源码 | 文件源码
def selectname(self, name, num=10):
        if not name:
            return []
        evalnum = int(num ** (1/len(name))) + 1
        namechars = [sorted(filter(ig1, ((n, self.firstchar.get(n, 1e-10 if 0x4E00 <= ord(n) < 0x9FCD else 0)) for n in self.lookupchar(name[0]))), key=ig1, reverse=1)]
        namechars.extend(sorted(filter(ig1, ((n, self.secondchar.get(n, 1e-10 if 0x4E00 <= ord(n) < 0x9FCD else 0)) for n in self.lookupchar(l))), key=ig1, reverse=1)[:evalnum] for l in name[1:])
        namechars = list(filter(None, namechars))[:10]
        if not namechars:
            return []
        candidates = []
        for group in itertools.product(*namechars):
            gz = tuple(zip(*group))
            gname = ''.join(gz[0])
            gfreq = functools.reduce(operator.mul, gz[1])
            candidates.append((gname, gfreq))
        candidates.sort(key=ig1, reverse=1)
        return [x[0] for x in candidates][:num]
项目:pudzu    作者:Udzu    | 项目源码 | 文件源码
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))
项目:pwndemo    作者:zh-explorer    | 项目源码 | 文件源码
def dotproduct(x, y):
    """dotproduct(x, y) -> int

    Computes the dot product of `x` and `y`.

    Arguments:
      x(iterable):  An iterable.
      x(iterable):  An iterable.

    Returns:
      The dot product of `x` and `y`, i.e.: ``x[0] * y[0] + x[1] * y[1] + ...``.

    Example:
      >>> dotproduct([1, 2, 3], [4, 5, 6])
      ... # 1 * 4 + 2 * 5 + 3 * 6 == 32
      32
    """
    return sum(imap(operator.mul, x, y))
项目:chebpy    作者:chebpy    | 项目源码 | 文件源码
def binaryOpTester(f, g, binop, nf, ng):
    ff = Chebtech2.initfun_fixedlen(f, nf)
    gg = Chebtech2.initfun_fixedlen(g, ng)
    FG = lambda x: binop(f(x),g(x)) 
    fg = binop(ff, gg)
    def tester(self):
        vscl = max([ff.vscale, gg.vscale])
        lscl = max([ff.size, gg.size])
        self.assertLessEqual(infnorm(fg(self.xx)-FG(self.xx)), 3*vscl*lscl*eps)
        if binop is operator.mul:
            # check simplify is not being called in __mul__
            self.assertEqual(fg.size, ff.size+gg.size-1)
    return tester

# note: defining __radd__(a,b) = operator.add(b,a) and feeding this into the
# test will not in fact test the __radd__ functionality of the class. These
# test need to be added manually to the class.
项目:chainer-deconv    作者:germanRos    | 项目源码 | 文件源码
def _generate_normal(self, func, size, dtype, *args):
        # curand functions below don't support odd size.
        # * curand.generateNormal
        # * curand.generateNormalDouble
        # * curand.generateLogNormal
        # * curand.generateLogNormalDouble
        size = core.get_size(size)
        element_size = six.moves.reduce(operator.mul, size, 1)
        if element_size % 2 == 0:
            out = cupy.empty(size, dtype=dtype)
            func(self._generator, out.data.ptr, out.size, *args)
            return out
        else:
            out = cupy.empty((element_size + 1,), dtype=dtype)
            func(self._generator, out.data.ptr, out.size, *args)
            return out[:element_size].reshape(size)

    # NumPy compatible functions
项目:myhdlpeek    作者:xesscorp    | 项目源码 | 文件源码
def __mul__(self, trc):
        return self.apply_op2(trc, operator.mul)
项目:zipline-chinese    作者:zhanghan1990    | 项目源码 | 文件源码
def test_vwap(context, data):
        """
        Tests the vwap transform by manually keeping track of the prices
        and volumes in a naiive way and asserting that our hand-rolled vwap is
        the same
        """
        mins = sum(context.mins_for_days[-context.days:])
        for sid in data:
            prices = context.price_bars[sid][-mins:]
            vols = context.vol_bars[sid][-mins:]
            manual_vwap = sum(
                map(operator.mul, np.nan_to_num(np.array(prices)), vols),
            ) / sum(vols)

            assert_allclose(
                data[sid].vwap(context.days),
                manual_vwap,
            )
项目:cloud-volume    作者:seung-lab    | 项目源码 | 文件源码
def rectVolume(self):
        return reduce(operator.mul, self)
项目:otRebuilder    作者:Pal3love    | 项目源码 | 文件源码
def __mul__(self, other):
        return Vector(self._scalarOp(other, operator.mul), keep=True)
项目:otRebuilder    作者:Pal3love    | 项目源码 | 文件源码
def __imul__(self, other):
        self.values = self._scalarOp(other, operator.mul)
        return self
项目:robocup-soccer    作者:kengz    | 项目源码 | 文件源码
def product(numbers):
    """Return the product of the numbers.
    >>> product([1,2,3,4])
    24
    """
    return reduce(operator.mul, numbers, 1)
项目:Projects    作者:it2school    | 项目源码 | 文件源码
def test_mul (self):
        c1 = pygame.Color (0x01010101)
        self.assertEquals (c1.r, 1)
        self.assertEquals (c1.g, 1)
        self.assertEquals (c1.b, 1)
        self.assertEquals (c1.a, 1)

        c2 = pygame.Color (2, 5, 3, 22)
        self.assertEquals (c2.r, 2)
        self.assertEquals (c2.g, 5)
        self.assertEquals (c2.b, 3)
        self.assertEquals (c2.a, 22)

        c3 = c1 * c2
        self.assertEquals (c3.r, 2)
        self.assertEquals (c3.g, 5)
        self.assertEquals (c3.b, 3)
        self.assertEquals (c3.a, 22)

        c3 = c3 * c2
        self.assertEquals (c3.r, 4)
        self.assertEquals (c3.g, 25)
        self.assertEquals (c3.b, 9)
        self.assertEquals (c3.a, 255)

        # Issue #286: Is type checking done for Python 3.x?
        self.assertRaises (TypeError, operator.mul, c1, None)
        self.assertRaises (TypeError, operator.mul, None, c1)
项目:code    作者:ActiveState    | 项目源码 | 文件源码
def vector_inner_product(self, a, b):
        """Takes the inner product of vectors a and b

        a and b are lists.
        This is a private function called by matrix_multiply.
        """
        assert(isinstance(a, types.ListType))
        assert(isinstance(b, types.ListType))
        return reduce(operator.add, map(operator.mul, a, b))
项目:turtle    作者:icse-turtle    | 项目源码 | 文件源码
def multiplication(self, *args):
        if not len(args) > 1:
            raise EvaluateException('* requires at least 2 parameters!' + ' (' + str(len(args)) + ' given).')

        elif False in [isinstance(x, NumberType) for x in args]:
            raise EvaluateException('* requires all parameters to be numbers!')

        return reduce(op.mul, args, IntegerType(1))
项目:deb-python-pint    作者:openstack    | 项目源码 | 文件源码
def __imul__(self, other):
        if not isinstance(self._magnitude, ndarray):
            return self._mul_div(other, operator.mul)
        else:
            return self._imul_div(other, operator.imul)
项目:deb-python-pint    作者:openstack    | 项目源码 | 文件源码
def __mul__(self, other):
        return self._mul_div(other, operator.mul)
项目:deb-python-pint    作者:openstack    | 项目源码 | 文件源码
def test_unitcontainer_arithmetic(self):
        x = UnitsContainer(meter=1)
        y = UnitsContainer(second=1)
        z = UnitsContainer(meter=1, second=-2)

        self._test_not_inplace(op.mul, x, y, UnitsContainer(meter=1, second=1))
        self._test_not_inplace(op.truediv, x, y, UnitsContainer(meter=1, second=-1))
        self._test_not_inplace(op.pow, z, 2, UnitsContainer(meter=2, second=-4))
        self._test_not_inplace(op.pow, z, -2, UnitsContainer(meter=-2, second=4))

        self._test_inplace(op.imul, x, y, UnitsContainer(meter=1, second=1))
        self._test_inplace(op.itruediv, x, y, UnitsContainer(meter=1, second=-1))
        self._test_inplace(op.ipow, z, 2, UnitsContainer(meter=2, second=-4))
        self._test_inplace(op.ipow, z, -2, UnitsContainer(meter=-2, second=4))
项目:deb-python-pint    作者:openstack    | 项目源码 | 文件源码
def _test_quantity_mul_div(self, unit, func):
        func(op.mul, unit * 10.0, '4.2*meter', '42*meter', unit)
        func(op.mul, '4.2*meter', unit * 10.0, '42*meter', unit)
        func(op.mul, '4.2*meter', '10*inch', '42*meter*inch', unit)
        func(op.truediv, unit * 42, '4.2*meter', '10/meter', unit)
        func(op.truediv, '4.2*meter', unit * 10.0, '0.42*meter', unit)
        func(op.truediv, '4.2*meter', '10*inch', '0.42*meter/inch', unit)