sw.py

Go to the documentation of this file.
00001 ##
00002 # 
00003 # Python script to provide stopwatch like behavior.  It is specific to 
00004 # the 32 bit Windows environment.
00005 # 
00006 # Instructions for use:
00007 #     1.  Start the program
00008 #     2.  When you press a key, the split time, total elapsed time,
00009 #         time/date and key pressed are printed on a line.
00010 # 
00011 # Special keys are:
00012 # 
00013 #     q        Quit
00014 #     Z        Rezero the timer
00015 #     C        Get prompted for a comment
00016 # 
00017 # If a file is included on the command line, the data are also logged to
00018 # that file.
00019 # 
00020 # Copyright (C) 2002 GDS Software
00021 # 
00022 # This program is free software; you can redistribute it and/or
00023 # modify it under the terms of the GNU General Public License as
00024 # published by the Free Software Foundation; either version 2 of
00025 # the License, or (at your option) any later version.
00026 # 
00027 # This program is distributed in the hope that it will be useful,
00028 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00029 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00030 # GNU General Public License for more details.
00031 # 
00032 # You should have received a copy of the GNU General Public
00033 # License along with this program; if not, write to the Free
00034 # Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00035 # MA  02111-1307  USA
00036 # 
00037 # See http://www.gnu.org/licenses/licenses.html for more details.
00038 # 
00039 
00040 import msvcrt, time, string, sys
00041 
00042 done       = 0
00043 start      = time.time()
00044 last_time  = start
00045 num_spaces = 10
00046 fmt        = "%%%d.1f %%%d.1f     %%s   %%s" % (num_spaces, num_spaces)
00047 log_stream = None
00048 
00049 # The Keys dictionary is used to decode special keystrokes
00050 Keys = {
00051     ";"  :  "F1",
00052     "<"  :  "F2",
00053     "="  :  "F3",
00054     ">"  :  "F4",
00055     "?"  :  "F5",
00056     "@"  :  "F6",
00057     "A"  :  "F7",
00058     "B"  :  "F8",
00059     "C"  :  "F9",
00060     "D"  :  "F10",
00061     "E"  :  "F11",
00062     "F"  :  "F12",
00063 }
00064 
00065 #-----------------------------------------------------------------------------
00066 
00067 def Usage():    
00068     print '''Commands:
00069   q       Quit
00070   Z       Zero time
00071   C       Enter a comment
00072 
00073 Any other keys take a split.  Start with a filename on the command
00074 line to also have the data appended to that file.  A state summary
00075 report will be written to the file.
00076 '''
00077 
00078 def Header():
00079     return '''Times are in seconds
00080 
00081  Diff time Total time
00082  --------- ----------'''
00083 
00084 def Log(str, quiet=0):
00085     if not quiet:
00086         print str
00087     if log_stream:
00088         log_stream.write(str + "\n")
00089 
00090 def Print(key):
00091     global last_time
00092     t = time.time()
00093     # Get current time string
00094     loc = time.localtime(t)
00095     str = time.asctime(loc)
00096     Log(fmt % ((t-last_time), (t-start), str, key))
00097     last_time = t
00098 
00099 ##
00100 # Return a string that represents the key pressed.  If the first
00101 #     getch() returns a '\000' character, then we call getch() again to
00102 #     get the second character and then perform a lookup in the Keys
00103 #     dictionary to get the string which represents the key pressed.
00104 #     
00105 def GetKey():
00106     key = msvcrt.getch()
00107     if key == '\003':  # Always exit on a ctrl-C
00108         sys.exit(0)
00109     if key == '\000':
00110         key = msvcrt.getch()
00111         key = "+" + key
00112     return key
00113 
00114 def main():
00115     global start, last_time, log_stream
00116     Usage()
00117     if len(sys.argv) > 1:
00118         log_stream = open(sys.argv[1], "w")
00119     Log(Header())
00120     while 1:
00121         while not msvcrt.kbhit():
00122             pass
00123         key = GetKey()
00124         if key == "q":
00125             Print("Quitting")
00126             if log_stream:
00127                 log_stream.close()
00128             sys.exit(0)
00129         elif key == "Z":
00130             start = time.time()
00131             last_time = start
00132             Log("Time reset")
00133         elif key == "C":
00134             # Prompt the user for a string
00135             str = raw_input("Enter comment string, '.<cr>' to finish:\n")
00136             while str != ".":
00137                 Log("+ " + str)
00138                 str = raw_input("")
00139         else:
00140             Print(key)
00141 
00142 if __name__ == "__main__":
00143     main()
00144 
00145 

© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved; If you reference this document or any part of this document you must use the citation verbatim (including the link) "© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved."

Notice: This source code contained in this document is NOT open source and is NOT being distributed as open source.

122,241 lines of code and growing...