Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #89843
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Subject | Re: Messages with a time stamp |
| Date | 2015-05-03 10:36 +0100 |
| References | <87r3qykr39.fsf@Equus.decebal.nl> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.58.1430645806.12865.python-list@python.org> (permalink) |
On 03/05/2015 09:22, Cecil Westerhof wrote: > For testing I want my messages time stamped like: > 02:06:32: Check that the non recursive variants give the same value from 1000 upto 100000 step 1000 > 02:06:32: Currently at 1000 > 02:06:33: Currently at 11000 > 02:06:35: Currently at 21000 > 02:06:42: Currently at 31000 > 02:06:56: Currently at 41000 > 02:07:18: Currently at 51000 > 02:07:51: Currently at 61000 > 02:08:43: Currently at 71000 > 02:09:49: Currently at 81000 > 02:11:13: Currently at 91000 > 02:13:01: Calculating values OK > > 02:13:01: Start with the time needed to calculate 100000 times > 02:13:01: Timing factorial_iterative (985): 31 > 02:13:32: Timing factorial_recursive (985): 55 > 02:14:28: Timing factorial_recursive_old (985): 56 > 02:15:24: Timing factorial_tail_recursion (985): 35 > 02:16:00: Timing factorial_tail_recursion_old(985): 40 > > 02:16:40: Start with the time needed to calculate 1 times > No recursive, because without tail recursion you would run out of stack space > 02:16:40: Timing factorial_iterative (100000): 3.7705 > 02:16:44: Timing factorial_tail_recursion (100000): 3.7692 > 02:16:48: Timing factorial_tail_recursion_old(100000): 4.1537 > > And sometimes I do not want the time shown, to signify that the > message belongs to the previous message. And sometimes I want no > newline, because I want to print something behind it. For example the > time needed to calculate something: > 02:13:01: Timing factorial_iterative (985): 31 > > For this I wrote: > ### Have the possibility to give the stream instead of using stdout > class TimedMessage: > """ > For printing messages with time prepended before it > Has the possibilty to keep time print blank for when several messages > are send shortly after eachother. > Also the possibilty to stay on the same line when things need to be appended > """ > > def give_msg(self, message, show_time = True, use_newline = True): > """ > Prints the message to stdout > Use show_time = False when you do not want time > Use use_newline = False if you do not want a newline > """ > > if show_time: > time = strftime(self._format) > else: > time = self._blank_time > formatted_message = time + message > if use_newline: > print(formatted_message) > else: > sys.stdout.write(formatted_message) > sys.stdout.flush() > > def __init__(self, format = '%H:%M:%S: '): > self._format = format > self._blank_time = ' ' * len(strftime(self._format)) > > Can I improve on this? > > It is shared at: > https://github.com/CecilWesterhof/PythonLibrary/blob/master/utilDecebal.py > Rather than reinvent the wheel maybe you can pinch something from here https://docs.python.org/3/howto/logging-cookbook.html#logging-to-multiple-destinations -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Messages with a time stamp Cecil Westerhof <Cecil@decebal.nl> - 2015-05-03 10:22 +0200
Re: Messages with a time stamp Michiel Overtoom <motoom@xs4all.nl> - 2015-05-03 11:10 +0200
Re: Messages with a time stamp Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-05-03 10:36 +0100
Re: Messages with a time stamp Cecil Westerhof <Cecil@decebal.nl> - 2015-05-03 12:08 +0200
csiph-web