def make_product_func(factors):
    '''
    Return a single callable from
    a list of factors which correctly
    applies the arguments to each
    individual factor.

    The challenge here is to return a function
    whose argument list we know and ensure that
    when this function is called, its always
    called with the correct arguments.
    Since the correct argspec is attached
    to the built function it seems that
    it should be up to the caller to
    get the argument list correct.
    So we need to determine when and where its called...

    '''
    args_map = {}
    all_args = []
    domains = {}
    for factor in factors:
        #if factor == 1:
        #    continue
        args_map[factor] = get_args(factor)
        all_args += args_map[factor]
        if hasattr(factor, 'domains'):
            domains.update(factor.domains)
    args = list(set(all_args))
    # Perhaps if we sort the


    def product_func(*product_func_args):
        #import pytest; pytest.set_trace()
        #arg_dict = dict([(a.name, a) for a in product_func_args])
        arg_dict = dict(zip(args, product_func_args))
        #import pytest; pytest.set_trace()
        result = 1
        for factor in factors:
            #domains.update(factor.domains)
            # We need to build the correct argument
            # list to call this factor with.
            factor_args = []
            for arg in get_args(factor):
                if arg in arg_dict:
                    factor_args.append(arg_dict[arg])
            if not factor_args:
                # Since we always require
                # at least one argument we
                # insert a dummy argument
                # so that the unity function works.
                factor_args.append('dummy')
            result *= factor(*factor_args)

        return result

    product_func.argspec = args
    product_func.factors = factors
    product_func.domains = domains
    return memoize(product_func)
dataset/ETHPy150Open eBay/bayesian-belief-networks/bayesian/factor_graph.py/make_product_func/original