Skip to content

Instantly share code, notes, and snippets.

@timbuchwaldt
Created December 20, 2011 20:45
Show Gist options
  • Save timbuchwaldt/1503194 to your computer and use it in GitHub Desktop.
Save timbuchwaldt/1503194 to your computer and use it in GitHub Desktop.
mt - minimal timing in python
Copyright 2011, Tim Buchwaldt. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY Tim ''AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Tim Buchwaldt OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those of the
authors and should not be interpreted as representing official policies, either expressed
or implied, of Tim Buchwaldt.
import time
import datetime
from termcolor import colored
class mt:
# init function, no values required, but you can supply name & maximum value before the "timing" string is printed in red
def __init__(self,name="",maximum = 0.5):
# store datetime.today() as a instance variable, as well as name & maximum
self.t1 = datetime.datetime.today()
self.name = name
self.maximum = maximum
# stop function, no arguments supplyable
def stop(self):
# store seconds measure in instance variable
self.t2 = datetime.datetime.today()
# calculate distance in seconds
sec=(self.t2-self.t1).total_seconds()
# check if its below the threshold
writing = 'green' if sec <= self.maximum else 'red'
# print colored timing
print colored("[TIMING]",writing,'on_white')+colored(" "+self.name+": ",'green')+colored(sec,'red')+colored("s \n", 'red')
# return timing & name as array for further processing
return [sec,self.name]
#usage:
# t = mt()
# timer now runs
# time.sleep(0.2)
# print t.stop() - [0.2000123,""] (prints [TIMING] in green because < 0.5s)
# settings:
# t2 = mt("your timing", 1.0)
# time.sleep(2)
# t2.stop() - [2.0000123,"your timing] (prints [TIMING] in red because > 1s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment