Пример #1
0
class CrossTabbingSource(object):
    "A source that produces a crosstab from another source"

    def __init__(self,
                 source,
                 rowvaluesatt,
                 colvaluesatt,
                 values,
                 aggregator=None,
                 nonevalue=0,
                 sortrows=False):
        """Arguments:
        - source: the data source to pull data from
        - rowvaluesatt: the name of the attribute that holds the values that
          appear as rows in the result
        - colvaluesatt: the name of the attribute that holds the values that
          appear as columns in the result
        - values: the name of the attribute that holds the values to aggregate
        - aggregator: the aggregator to use (see pyauetl.aggregators). If not
          given, pyauetl.aggregators.Sum is used to sum the values
        - nonevalue: the value to return when there is no data to aggregate.
          Default: 0
        - sortrows: A boolean deciding if the rows should be sorted.
          Default: False
        """
        self.__source = source
        self.__rowvaluesatt = rowvaluesatt
        self.__colvaluesatt = colvaluesatt
        self.__values = values
        if aggregator is None:
            from pyauetl.aggregators import Sum
            self.__aggregator = Sum()
        else:
            self.__aggregator = aggregator
        self.__nonevalue = nonevalue
        self.__sortrows = sortrows
        self.__allcolumns = set()
        self.__allrows = set()

    def __iter__(self):
        for data in self.__source:  # first we iterate over all source data ...
            row = data[self.__rowvaluesatt]
            col = data[self.__colvaluesatt]
            self.__allrows.add(row)
            self.__allcolumns.add(col)
            self.__aggregator.process((row, col), data[self.__values])

        # ... and then we build result rows
        for row in (self.__sortrows and sorted(self.__allrows) \
                        or self.__allrows):
            res = {self.__rowvaluesatt: row}
            for col in self.__allcolumns:
                res[col] = \
                    self.__aggregator.finish((row, col), self.__nonevalue)
            yield res
Пример #2
0
 def __init__(self,
              source,
              rowvaluesatt,
              colvaluesatt,
              values,
              aggregator=None,
              nonevalue=0,
              sortrows=False):
     """Arguments:
     - source: the data source to pull data from
     - rowvaluesatt: the name of the attribute that holds the values that
       appear as rows in the result
     - colvaluesatt: the name of the attribute that holds the values that
       appear as columns in the result
     - values: the name of the attribute that holds the values to aggregate
     - aggregator: the aggregator to use (see pyauetl.aggregators). If not
       given, pyauetl.aggregators.Sum is used to sum the values
     - nonevalue: the value to return when there is no data to aggregate.
       Default: 0
     - sortrows: A boolean deciding if the rows should be sorted.
       Default: False
     """
     self.__source = source
     self.__rowvaluesatt = rowvaluesatt
     self.__colvaluesatt = colvaluesatt
     self.__values = values
     if aggregator is None:
         from pyauetl.aggregators import Sum
         self.__aggregator = Sum()
     else:
         self.__aggregator = aggregator
     self.__nonevalue = nonevalue
     self.__sortrows = sortrows
     self.__allcolumns = set()
     self.__allrows = set()