~ubuntu-branches/ubuntu/utopic/gozerbot/utopic

« back to all changes in this revision

Viewing changes to gozerbot/utils/statdict.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Malcolm
  • Date: 2009-09-14 09:00:29 UTC
  • mfrom: (1.1.4 upstream) (3.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090914090029-uval0ekt72kmklxw
Tags: 0.9.1.3-3
Changed dependency on python-setuptools to python-pkg-resources
(Closes: #546435) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# gozerbot/statdict.py
 
2
#
 
3
#
 
4
 
 
5
""" stats dict """
 
6
 
 
7
__copyright__ = 'this file is in the public domain'
 
8
 
 
9
class Statdict(dict):
 
10
 
 
11
    """ dictionary to hold stats """
 
12
 
 
13
    def set(self, item, value):
 
14
        """ set item to value """
 
15
        self[item] = value
 
16
 
 
17
    def upitem(self, item, value=1):
 
18
        """ increase item """
 
19
        if not self.has_key(item):
 
20
            self[item] = value
 
21
            return
 
22
        self[item] += value
 
23
 
 
24
    def top(self, start=1, limit=None):
 
25
        """ return highest items """
 
26
        result = []
 
27
        for item, value in self.iteritems():
 
28
            if value >= start:
 
29
                result.append((item, value))
 
30
        result.sort(lambda b, a: cmp(a[1], b[1]))
 
31
        if limit:
 
32
            result =  result[:limit]
 
33
        return result
 
34
 
 
35
    def down(self, end=100, limit=None):
 
36
        """ return lowest items """
 
37
        result = []
 
38
        for item, value in self.iteritems():
 
39
            if value <= end:
 
40
                result.append((item, value))
 
41
        result.sort(lambda a, b: cmp(a[1], b[1]))
 
42
        if limit:
 
43
            return result[:limit]
 
44
        else:
 
45
            return result