コード例 #1
0
def aggregate_properties(objects, aggregators):
    """Combine the properties of a list of objects.

    Constructs a lazy dictionary (see class lazy_dict in utils.js) of
    properties.

    Arguments:

    objects -- a list of Node or Edge objects

    aggregators -- dictionary, specifies how to combine the
    properties. For each item in aggregators, a property with the same
    key is created. The aggregator value should be either a function,
    in which case the property value is created by calling that
    function on the list of objects; or a pair (function, prop), in
    which case the property value is created by calling the function
    on a list containing each object's value for property prop.

    """
    def apply_aggregator(aggr):
        """Run aggregator with objects as arguments"""
        if isinstance(aggr, tuple):
            fun = aggr[0]
            prop = aggr[1]
            lst = [x[prop] for x in objects]
        else:
            fun = aggr
            lst = objects
        return fun(lst)

    return map_dict_lazy(apply_aggregator, aggregators)
コード例 #2
0
ファイル: graph.py プロジェクト: alexanderfefelov/nav
def aggregate_properties(objects, aggregators):
    """Combine the properties of a list of objects.

    Constructs a lazy dictionary (see class lazy_dict in utils.js) of
    properties.

    Arguments:

    objects -- a list of Node or Edge objects

    aggregators -- dictionary, specifies how to combine the
    properties. For each item in aggregators, a property with the same
    key is created. The aggregator value should be either a function,
    in which case the property value is created by calling that
    function on the list of objects; or a pair (function, prop), in
    which case the property value is created by calling the function
    on a list containing each object's value for property prop.

    """
    def apply_aggregator(aggr):
        """Run aggregator with objects as arguments"""
        if isinstance(aggr, tuple):
            fun = aggr[0]
            prop = aggr[1]
            lst = [x[prop] for x in objects]
        else:
            fun = aggr
            lst = objects
        return fun(lst)
    return map_dict_lazy(apply_aggregator, aggregators)