I'm self taught in python 3, and I frequently keep track of how long my scripts have been running using the time library.
import time startTime = time.time() #The main body of script... print('The script finished in {0} seconds'.format(time.time() - startTime) However this can be unhelpful if the script has been running for some time, an example output being:
'The script has been running for 4323.580279111862 seconds' I wrote a function which takes a float variable and prints the time in a more readable way. I convert the seconds to integer to get rid of the 12 decimal places, and I can pass a boolean value which changes the wording of the print to indicate that the script is still running or has finished.
def printRuntime(seconds,finished=False): seconds = int(seconds) status = 'has been running for' if finished == True: status = 'finished in' if seconds < 60: print('The script {} {} seconds'.format(status,seconds)) return elif seconds < 3600: minutes = seconds // 60 seconds = seconds - 60*minutes print('The script {} {} minutes & {} seconds'.format(status,minutes,seconds)) return else: hours = seconds // 3600 minutes = (seconds - 3600*hours) // 60 seconds = seconds - 3600*hours - 60*minutes print('The script {} {} hours, {} minutes & {} seconds'.format(status,hours,minutes,seconds)) return import time startTime = time.time() #The main body of script... printRuntime(time.time() - startTime) Since I'm self taught I have no real idea about best practice. I feel like there must be a more concise way of doing this, what mistakes am I making?