tcms.core.db module

class tcms.core.db.GroupByResult(data=None, total_name=None)[source]

Bases: object

Group By result

This object can be used as a normal dict object with less support of stock dictionary methods. Consumers can do

  • get a subtotal associated with a name
  • get a subtotal’s percentage
  • know whether it’s empty. Empty means no data from database of the GROUP BY query
  • how many subtotals there

The main purpose of GroupByResult is to get specific subtotal(s) and the percentage of each of them. Rules to get such values

  • each subtotal is associated with a name. If name you give does not exist, 0 is returned, otherwise proper value is returned.
  • percentage of each subtotal has a special name with format of subtotal name plus ‘_percent’.


Suppose, a GroupByResult object named gbr is {‘A’: 100, ‘B’: 200}

To get subtotal of A, gbr.A

To get percentage of B, gbr.B_percent

get(key, default=None)[source]
leaf_values_count(value_in_row=False, refresh=False)[source]

Calculate the total number of leaf values under this level

After the first time this method gets call, the result will be cached as meta data of this level node. So, any number of subsequent invocations of this method will return result by reading self._meta directly without repeating calculation. Unless, pass True to argument refresh.

  • value_in_row (bool) – whether leaf value should be treated as a row, in such way, leaf value will be displayed in one row.
  • refresh (bool) – whether force to recalculate

the total number of leaf values under this level

Return type:


setdefault(key, default=None)[source]

Get the total value of this GROUP BY result

Total value comes from two situations. One is that there is no total value computed in database side by issuing GROUP BY with ROLLUP. In this case, total value will be calculated from all subtotal values. Inversely, the total value will be returned directly.