def weightedAverage(requestContext, seriesListAvg, seriesListWeight, *nodes):
    """
    Takes a series of average values and a series of weights and
    produces a weighted average for all values.

    The corresponding values should share one or more zero-indexed nodes.

    Example::

        &target=weightedAverage(*.transactions.mean,*.transactions.count,0)
        &target=weightedAverage(*.transactions.mean,*.transactions.count,1,3,4)

    """

    if isinstance(nodes, int):
        nodes = [nodes]

    sortedSeries = {}

    for seriesAvg, seriesWeight in zip_longest(
            seriesListAvg, seriesListWeight):
        key = ''
        for node in nodes:
            key += seriesAvg.name.split(".")[node]

        sortedSeries.setdefault(key, {})
        sortedSeries[key]['avg'] = seriesAvg

        key = ''
        for node in nodes:
            key += seriesWeight.name.split(".")[node]

        sortedSeries.setdefault(key, {})
        sortedSeries[key]['weight'] = seriesWeight

    productList = []

    for key in sortedSeries:
        if 'weight' not in sortedSeries[key]:
            continue
        if 'avg' not in sortedSeries[key]:
            continue

        seriesWeight = sortedSeries[key]['weight']
        seriesAvg = sortedSeries[key]['avg']

        productValues = [safeMul(val1, val2)
                         for val1, val2
                         in zip_longest(seriesAvg, seriesWeight)]
        name = 'product(%s,%s)' % (seriesWeight.name, seriesAvg.name)
        productSeries = TimeSeries(name, seriesAvg.start, seriesAvg.end,
                                   seriesAvg.step, productValues)
        productSeries.pathExpression = name
        productList.append(productSeries)

    if not productList:
        return []

    [sumProducts] = sumSeries(requestContext, productList)
    [sumWeights] = sumSeries(requestContext, seriesListWeight)

    resultValues = [safeDiv(val1, val2)
                    for val1, val2 in zip_longest(sumProducts, sumWeights)]
    name = "weightedAverage(%s, %s, %s)" % (
        ','.join(sorted(set(s.pathExpression for s in seriesListAvg))),
        ','.join(sorted(set(s.pathExpression for s in seriesListWeight))),
        ','.join(map(str, nodes)))
    resultSeries = TimeSeries(name, sumProducts.start, sumProducts.end,
                              sumProducts.step, resultValues)
    resultSeries.pathExpression = name
    return resultSeries
def weightedAverage ( requestContext , seriesListAvg , seriesListWeight , * nodes ) : if isinstance ( nodes , int ) : nodes = [ nodes ] sortedSeries = { } for seriesAvg , seriesWeight in zip_longest ( seriesListAvg , seriesListWeight ) : key = '' for node in nodes : key += seriesAvg . name . split ( "." ) [ node ] sortedSeries . setdefault ( key , { } ) sortedSeries [ key ] [ 'avg' ] = seriesAvg key = '' for node in nodes : key += seriesWeight . name . split ( "." ) [ node ] sortedSeries . setdefault ( key , { } ) sortedSeries [ key ] [ 'weight' ] = seriesWeight productList = [ ] for key in sortedSeries : if 'weight' not in sortedSeries [ key ] : continue if 'avg' not in sortedSeries [ key ] : continue seriesWeight = sortedSeries [ key ] [ 'weight' ] seriesAvg = sortedSeries [ key ] [ 'avg' ] productValues = [ safeMul ( val1 , val2 ) for val1 , val2 in zip_longest ( seriesAvg , seriesWeight ) ] name = 'product(%s,%s)' % ( seriesWeight . name , seriesAvg . name ) productSeries = TimeSeries ( name , seriesAvg . start , seriesAvg . end , seriesAvg . step , productValues ) productSeries . pathExpression = name productList . append ( productSeries ) if not productList : return [ ] [ sumProducts ] = sumSeries ( requestContext , productList ) [ sumWeights ] = sumSeries ( requestContext , seriesListWeight ) resultValues = [ safeDiv ( val1 , val2 ) for val1 , val2 in zip_longest ( sumProducts , sumWeights ) ] name = "weightedAverage(%s, %s, %s)" % ( ',' . join ( sorted ( set ( s . pathExpression for s in seriesListAvg ) ) ) , ',' . join ( sorted ( set ( s . pathExpression for s in seriesListWeight ) ) ) , ',' . join ( map ( str , nodes ) ) ) resultSeries = TimeSeries ( name , sumProducts . start , sumProducts . end , sumProducts . step , resultValues ) resultSeries . pathExpression = name return resultSeries
Takes a series of average values and a series of weights and
    produces a weighted average for all values.

    The corresponding values should share one or more zero-indexed nodes.

    Example::

        &target=weightedAverage(*.transactions.mean,*.transactions.count,0)
        &target=weightedAverage(*.transactions.mean,*.transactions.count,1,3,4)