from time import time
class Timer( object ) :
	def __init__( self, format="%.2f" ):
		self.t0 = time()
		self.last = time()
		self.format = format

	def __float__( self ):
		new = time()
		returnvalue = new - self.last
		self.last = new
		return returnvalue
	
	# returns the time since the last call	
	def __str__( self ):
		new = time()
		returnvalue = ( self.format + " s" ) % ( new - self.last )
		self.last = new
		return returnvalue

	# returns the time since instantiation
	def __repr__( self ):
		new = time()
		returnvalue = ( self.format + " s" ) % ( new - self.t0 )
		self.last = new
		return returnvalue

	def __call__( self ):
		return self.__repr__()

class Counter( object ):
	def __init__( self, threshold=1, power=10 ):
		self.ctr = 0
		self.orig_threshold = threshold
		self.threshold = threshold
		self.power = power

	def __call__( self, message ):

		self.ctr += 1
		message = ( "iteration %8d " % self.ctr ) + message 

		if self.ctr % self.threshold == 0 :
			print message
		if self.ctr > self.threshold*10 :
			self.threshold *= self.power

	def reset( self ):
		self.ctr = 0
		self.threshold = self.orig_threshold
		
