-
Notifications
You must be signed in to change notification settings - Fork 7
/
running_stats.py
121 lines (101 loc) · 3.83 KB
/
running_stats.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'''Tool for a script to keep track changes performed on a large number
of objects.
StatsCount - when you are counting incidences of a small set of outcomes
StatsList - when you also want to remember an ID associated with each incidence
Examples:
from running_stats import StatsCount
package_stats = StatsCount()
for package in packages:
if package.enabled:
package.delete()
package_stats.increment('deleted')
else:
package_stats.increment('not deleted')
print package_stats.report()
> deleted: 30
> not deleted: 70
from running_stats import StatsList
package_stats = StatsList()
for package in packages:
if package.enabled:
package.delete()
package_stats.add('deleted', package.name)
else:
package_stats.add('not deleted' package.name)
print package_stats.report()
> deleted: 30 pollution-uk, flood-regions, river-quality, ...
> not deleted: 70 spending-bristol, ...
'''
import copy
class StatsCount(dict):
# {category:count}
_init_value = 0
report_value_limit = 250
def _init_category(self, category):
if not self.has_key(category):
self[category] = copy.deepcopy(self._init_value)
def increment(self, category):
self._init_category(category)
self[category] += 1
def report_value(self, category):
'''Returns the value for a category and value to sort categories by.'''
value = repr(self[category])
if len(value) > self.report_value_limit:
value = value[:self.report_value_limit] + '...'
return (value, self[category])
def report(self, indent=1, order_by_title=False):
lines = []
indent_str = '\t' * indent
report_dict = dict()
for category in self.keys():
report_dict[category] = self.report_value(category)
if order_by_title:
items = sorted(report_dict.iteritems())
else:
items = sorted(report_dict.iteritems(),
key=lambda x: -x[1][1])
for category, value_tuple in items:
value = value_tuple[0]
lines.append(indent_str + '%s: %s' % (category, value))
if not self:
lines = [indent_str + 'None']
return '\n'.join(lines)
class StatsList(StatsCount):
# {category:[values]}
_init_value = []
def add(self, category, value):
self._init_category(category)
self[category].append(value)
return '%s: %s' % (category, value) # so you can log it too
def report_value(self, category):
value = self[category]
number_of_values = len(value)
value_str = '%i %r' % (number_of_values, value)
if len(value_str) > self.report_value_limit:
value_str = value_str[:self.report_value_limit] + '...'
return (value_str, number_of_values)
class OpenSpendingStats(StatsList):
def add_source(self, category, row, value='notspecified'):
id = '%s %s %s' % (row['package_name'], row['resource_id'], row['url'])
if value != 'notspecified':
id += ' %r' % value
return self.add(category, value=id)
def add_spending(self, category, row, value='notspecified'):
id = row['resource_id']
if row['sheet_id']:
id += ' Sheet=%s' % row['sheet_id']
if 'row_number' in row:
id += ' Row=%s' % row['row_number']
id += ' RowID=%s' % row['row_id']
if value != 'notspecified':
id += ' %r' % value
return self.add(category, value=id)
if __name__ == '__main__':
package_stats = StatsList()
package_stats.add('Success', 'good1')
package_stats.add('Success', 'good2')
package_stats.add('Success', 'good3')
package_stats.add('Success', 'good4')
package_stats.add('Failure', 'bad1')
print package_stats.report()
print StatsList().report()