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
--
https://mail.python.org/mailman/listinfo/python-list