Welcome, guest | Sign In | My Account | Store | Cart

Python allows a very high level and clean solution to control break reporting problems. This time we use a dictionary and a list to create summary reports.

Python, 75 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Version 1.0   Ernie P. Adorio
#  
# 1. Data source rows as tuples, fields should form a hierarchy.
#    but the rows do not have to be in sorted order.
#            (Region, city, value)

records = [  ('England', 'Manchester', 45600),
             ('Scotland', 'Edinburgh', 20000),
             ('England', 'London', 90000),
             ('Scotland', 'Glasgow', 12500),
             ('Wales', 'Cardiff', 29700),
             ('England', 'Liverpool', 29700),
             ('Scotland', 'Edinburgh', 1000),
             ('Wales', 'Bangor', 12800),
             ('Scotland', 'Glasgow', 13400),
             ('England', 'Liverpool', 30000)]


# 2. Hierarchy levels and index of value to summarize.
maxlevel   = 2  # Maximum hierarchy levels  Region/city.
valueIndex = 2  # Index of value to summarize in data tuple.

# 3. Summaries in dictionary format.
Dlevels  = {}   
for rec in records:
    v = rec[valueIndex]
    for i in range(maxlevel):   # handle the sublevels 
        key = rec[:i+1]       
        if key not in Dlevels:
            Dlevels[key] = v
        else:
            Dlevels[key] += v

# 4. Schwartzian transform 
Tlevels = []
for  key in Dlevels:
    value = Dlevels[key]
    Tlevels.append((key, value))
Tlevels.sort()

# 5. Reporting (must be tailored to your actual requirements).
Total = 0
for (key, value) in enumerate(Tlevels):
    location = value[0]
    amt      = value[1]
    L        = len(location)

    if L == 1:  # Major category.
        Region  = location[0]
        print Region, "   \t", amt
        Total += amt
    else: 
        print " " * L,
        city    = location[1]
        print city, "\t\t",  amt
        
print "=" * 10
print "Total   \t", Total  
    

"""
When the program is run, it outputs
England         195300
   Liverpool            59700
   London               90000
   Manchester           45600
Scotland        46900
   Edinburgh            21000
   Glasgow              25900
Wales           42500
   Bangor               12800
   Cardiff              29700
==========
Total           284700
"""
    
        
    

Data rows do not have to be sorted. User/programmer has access to multilevel summary values (allows headers, aside from footers) with summary values.

See also Recipes #473847 and #304162.

Created by Ernesto Adorio on Fri, 28 Apr 2006 (PSF)
Python recipes (4591)
Ernesto Adorio's recipes (9)

Required Modules

  • (none specified)

Other Information and Tasks