l10n: Updated Greek (el) translation to 20%
[gpodder.git] / src / gpodder / corestats.py
blob323f2a6227399d194824f5b14a962a54cf14a276
3 # corestats.py (COREy STATS)
4 # Copyright (c) 2006-2007, Corey Goldberg (corey@goldb.org)
5 # http://www.goldb.org/corestats.html
7 # statistical calculation class
8 # for processing numeric sequences
10 # license: GNU LGPL
12 # This library is free software; you can redistribute it and/or
13 # modify it under the terms of the GNU Lesser General Public
14 # License as published by the Free Software Foundation; either
15 # version 2.1 of the License, or (at your option) any later version.
17 # Slightly modified for use in gPodder
20 class Stats(object):
21 def __init__(self, sequence):
22 # sequence of numbers we will process
23 # convert all items to floats for numerical processing
24 self.sequence = [float(item) for item in sequence]
26 def sum(self):
27 if len(self.sequence) < 1:
28 return None
29 else:
30 return sum(self.sequence)
32 def count(self):
33 return len(self.sequence)
35 def min(self):
36 if len(self.sequence) < 1:
37 return None
38 else:
39 return min(self.sequence)
41 def max(self):
42 if len(self.sequence) < 1:
43 return None
44 else:
45 return max(self.sequence)
47 def avg(self):
48 if len(self.sequence) < 1:
49 return None
50 else:
51 return sum(self.sequence) / len(self.sequence)
53 def median(self):
54 if len(self.sequence) < 1:
55 return None
56 else:
57 self.sequence.sort()
58 return self.sequence[len(self.sequence) // 2]
60 def stdev(self):
61 if len(self.sequence) < 1:
62 return None
63 else:
64 avg = self.avg()
65 sdsq = sum([(i - avg) ** 2 for i in self.sequence])
66 stdev = (sdsq / (len(self.sequence) - 1)) ** .5
67 return stdev
69 def percentile(self, percentile):
70 if len(self.sequence) < 1:
71 value = None
72 elif percentile >= 100:
73 raise ValueError('percentile must be < 100')
74 else:
75 element_idx = int(len(self.sequence) * (percentile / 100.0))
76 self.sequence.sort()
77 value = self.sequence[element_idx]
78 return value