annotate vixm/stats.py @ 42:bc6db55448e1 pure

statistics engine
author Josef "Jeff" Sipek <jeffpc@josefsipek.net>
date Sun, 05 Nov 2006 19:25:30 -0500
parents
children 2d570448aba5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
42
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
1 NEW_SONGS_SYNC = 2
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
2
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
3 class statsengine:
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
4 def __init__(self, statsfile):
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
5 self.__fname = statsfile
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
6 try:
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
7 self.__stats = eval(open(statsfile, "r").read())
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
8 except:
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
9 self.__stats = {}
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
10 self.__new_songs = 0
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
11
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
12 self.current = None
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
13
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
14 def __del__(self):
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
15 self.sync()
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
16
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
17 def sync(self):
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
18 f = open(self.__fname, "w")
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
19 f.write(repr(self.__stats))
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
20 f.close()
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
21
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
22 self.__new_songs = 0
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
23
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
24 def played(self):
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
25 s = str(self.current)
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
26
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
27 if not self.__stats.has_key(s):
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
28 self.__stats[s] = 0
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
29
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
30 self.__stats[s] += 1
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
31
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
32 self.__new_songs += 1
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
33 if self.__new_songs >= NEW_SONGS_SYNC:
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
34 self.sync()
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
35