# vim:ft=doctest

Aggregating
===========



Checking Pivot Table
--------------------

Let's create our class to add in pivot table ::

    >>> class Purchase(object):
    ...     def __init__(self, cost=0.0, price=0.0, month='', ou=''):
    ...         self.cost = cost
    ...         self.price  = price
    ...         self.month = month
    ...         self.ou = ou
    ...     def gain(self):
    ...         return (self.price - self.cost) / self.cost
    ...     def __repr__(self):
    ...         return 'Purchase(cost=%f, price=%f, month=%s, ou=%s)' % (self.cost,
    ...                                     self.price, self.month, self.ou) 


Let's create some purchases, for NY::

    >>> purschases = [Purchase(cost=5, price=7, month='jan', ou='NY'), 
    ...         Purchase(cost=5, price=7, month='jan', ou='NY'),
    ...         Purchase(cost=14, price=4900, month='feb', ou='NY'),
    ...         Purchase(cost=7, price=7000, month='mar', ou='NY'), Purchase(cost=7, price=7834, month='apr', ou='NY'),
    ...         Purchase(cost=73, price=8692, month='may', ou='NY'), Purchase(cost=128, price=9552, month='jun', ou='NY'), 
    ...         Purchase(cost=58, price=8828, month='jul', ou='NY'), Purchase(cost=128, price=9652, month='aug', ou='NY'), ]

Let's create some purchases, for RJ::

    >>> purschases += [Purchase(cost=14, price=463, month='jan', ou='RJ'), Purchase(cost=14, price=4946, month='feb', ou='RJ'),
    ...         Purchase(cost=7, price=7184,  month='mar', ou='RJ'), Purchase(cost=7, price=7834, month='apr', ou='RJ'),
    ...         Purchase(cost=73, price=8692, month='may', ou='RJ'), Purchase(cost=128, price=9552, month='jun', ou='RJ'), 
    ...         Purchase(cost=58, price=8828, month='jul', ou='RJ'), Purchase(cost=128, price=9652, month='aug', ou='RJ'), ]


Generating a simple Pivot Table::

    >>> from pivot_table import *


    >>> fmt = PivotTable()
    >>> fmt.attr_to_name_col = 'month'
    >>> fmt.attrs_to_fill_row = [{'attr': 'cost',  'label': 'Cost Total',   'aggr_func': Sum}, 
    ...                          {'attr': 'price', 'label': "Sell's Price", 'aggr_func': Sum}, 
    ...                          {'attr': 'gain',  'label': 'AVG Gain %',   'aggr_func': Avg},
    ...                          {'attr': 'ou',    'label': 'OU',           'aggr_func': GroupBy}]
    >>> fmt.objects = purschases
    >>> fmt.first_col_title = 'Purchases'

Checking the titles ::

    >>> fmt.getHeader()
    ['Purchases', 'OU', 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug']

Checking the rows ::

    >>> rows = fmt.getRows()

    >>> rows[0]
    ['Cost Total', 'RJ', 14, 14, 7, 7, 73, 128, 58, 128]
    >>> rows[1]
    ["Sell's Price", 'RJ', 463, 4946, 7184, 7834, 8692, 9552, 8828, 9652]
    >>> rows[2]
    ['AVG Gain %', 'RJ', 32.0, 352.0, 1025.0, 1118.0, 118.0, 73.0, 151.0, 74.0]

    >>> rows[3]
    ['Cost Total', 'NY', 10, 14, 7, 7, 73, 128, 58, 128]
    >>> rows[4]
    ["Sell's Price", 'NY', 14, 4900, 7000, 7834, 8692, 9552, 8828, 9652]
    >>> rows[5]
    ['AVG Gain %', 'NY', 0.0, 349.0, 999.0, 1118.0, 118.0, 73.0, 151.0, 74.0]

Checking the footer ::

    >>> fmt.getFooter()
    []

Now, new purchases
------------------

NY has purchases in jan. and feb. :: 

    >>> purschases = [Purchase(cost=5, price=10, month='jan', ou='NY'), 
    ...         Purchase(cost=5, price=10, month='jan', ou='NY'),
    ...         Purchase(cost=14, price=28, month='feb', ou='NY'),
    ...         Purchase(cost=14, price=28, month='feb', ou='NY'),
    ...         ]

RJ has purchases only in feb. ::

    >>> purschases += [
    ...         Purchase(cost=14, price=28, month='feb', ou='RJ'),
    ...         Purchase(cost=14, price=28, month='feb', ou='RJ'),
    ...         ]

Using the same params to Pivot Table ::

    >>> fmt = PivotTable()
    >>> fmt.attr_to_name_col = 'month'
    >>> fmt.attrs_to_fill_row = [{'attr': 'cost',  'label': 'Cost Total',   'aggr_func': Sum}, 
    ...                          {'attr': 'price', 'label': "Sell's Price", 'aggr_func': Sum}, 
    ...                          {'attr': 'gain',  'label': 'AVG Gain %',   'aggr_func': Avg},
    ...                          {'attr': 'ou',    'label': 'OU',           'aggr_func': GroupBy}]
    >>> fmt.objects = purschases
    >>> fmt.first_col_title = 'Purchases'

RJ need the col jan. to be empty (None) ::

    >>> fmt.getHeader()
    ['Purchases', 'OU', 'jan', 'feb']
    >>> rows = fmt.getRows()
    >>> rows[0]
    ['Cost Total', 'RJ', None, 28]
    >>> rows[1]
    ["Sell's Price", 'RJ', None, 56]
    >>> rows[2]
    ['AVG Gain %', 'RJ', None, 1.0]

    >>> rows[3]
    ['Cost Total', 'NY', 10, 28]
    >>> rows[4]
    ["Sell's Price", 'NY', 20, 56]
    >>> rows[5]
    ['AVG Gain %', 'NY', 1.0, 1.0]

