view 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
line wrap: on
line source

NEW_SONGS_SYNC = 2

class statsengine:
	def __init__(self, statsfile):
		self.__fname = statsfile
		try:
			self.__stats = eval(open(statsfile, "r").read())
		except:
			self.__stats = {}
		self.__new_songs = 0

		self.current = None
	
	def __del__(self):
		self.sync()
	
	def sync(self):
		f = open(self.__fname, "w")
		f.write(repr(self.__stats))
		f.close()

		self.__new_songs = 0

	def played(self):
		s = str(self.current)

		if not self.__stats.has_key(s):
			self.__stats[s] = 0

		self.__stats[s] += 1

		self.__new_songs += 1
		if self.__new_songs >= NEW_SONGS_SYNC:
			self.sync()