Package s3 :: Module s3grouped :: Class S3GroupedItems
[frames] | no frames]

Class S3GroupedItems

source code

object --+
         |
        S3GroupedItems

Helper class representing dict-like items grouped by attribute values, used by S3GroupedItemsReport

Instance Methods
 
__init__(self, items, groupby=None, aggregate=None, values=None)
Constructor
source code
 
groups(self)
Generator for iteration over subgroups
source code
 
__getitem__(self, key)
Getter for the grouping values dict
source code
 
add(self, item)
Add a new item, either to this group or to a subgroup
source code
 
add_to_group(self, key, value, item)
Add an item to a subgroup.
source code
 
get_values(self, key)
Get a list of attribute values for the items in this group
source code
 
aggregate(self, method, key)
Aggregate item attribute values (recursively over subgroups)
source code
 
__repr__(self)
Represent this group and all its subgroups as string
source code
 
json(self, fields=None, labels=None, represent=None, as_dict=False, master=True)
Serialize this group as JSON
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties

Inherited from object: __class__

Method Details

__init__(self, items, groupby=None, aggregate=None, values=None)
(Constructor)

source code 

Constructor

Parameters:
  • items - ordered iterable of items (e.g. list, tuple, iterator, Rows), grouping tries to maintain the original item order
  • groupby - attribute key or ordered iterable of attribute keys (e.g. list, tuple, iterator) for the items to be grouped by; grouping happens in order of appearance of the keys
  • aggregate - aggregates to compute, list of tuples (method, key)
  • value - the grouping values for this group (internal)
Overrides: object.__init__

groups(self)

source code 

Generator for iteration over subgroups

Decorators:
  • @property

__getitem__(self, key)
(Indexing operator)

source code 

Getter for the grouping values dict

Parameters:
  • key - the grouping key

add(self, item)

source code 

Add a new item, either to this group or to a subgroup

Parameters:
  • item - the item

add_to_group(self, key, value, item)

source code 

Add an item to a subgroup. Create that subgroup if it does not yet exist.

Parameters:
  • key - the grouping key
  • value - the grouping value for the subgroup
  • item - the item to add to the subgroup

get_values(self, key)

source code 

Get a list of attribute values for the items in this group

Parameters:
  • key - the attribute key
Returns:
the list of values

aggregate(self, method, key)

source code 

Aggregate item attribute values (recursively over subgroups)

Parameters:
  • method - the aggregation method
  • key - the attribute key
Returns:
an S3GroupAggregate instance

__repr__(self)
(Representation operator)

source code 

Represent this group and all its subgroups as string

Overrides: object.__repr__

json(self, fields=None, labels=None, represent=None, as_dict=False, master=True)

source code 

Serialize this group as JSON

@param fields: the columns to include for each item
@param labels: columns labels as dict {key: label},
               including the labels for grouping axes
@param represent: dict of representation methods for grouping
                  axis values {colname: function}
@param as_dict: return output as dict rather than JSON string
@param master: this is the top-level group (internal)

JSON Format:

{"c": [key, ...],          ....... list of keys for visible columns
 "g": [key, ...],          ....... list of keys for grouping axes
 "l": [(key, label), ...], ....... list of key-label pairs
 "k": key,                 ....... grouping key for subgroups
 "d": [                    ....... list of sub-groups
     {"v": string,         ....... the grouping value for this subgroup (represented)
      "k": key             ....... the grouping key for subgroups
      "d": [...]           ....... list of subgroups (nested)
      "i": [               ....... list of items in this group
           {key: value,    ....... key-value pairs for visible columns
           }, ...
      ],
      "t": {               ....... list of group totals
          key: value,      ....... key-value pairs for totals
      }
     }, ...
    ],
"i": [...],                ....... list of items (if no grouping)
"t": [...],                ....... list of grand totals
"e": boolean               ....... empty-flag
}