Path: csiph.com!usenet.pasdenom.info!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!ecngs!feeder2.ecngs.de!87.79.20.101.MISMATCH!newsreader4.netcologne.de!news.netcologne.de!feeder1.xsusenet.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'from:addr:yahoo.co.uk': 0.04; 'true,': 0.05; '"""': 0.07; 'calculating': 0.09; 'lawrence': 0.09; 'newline': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'url:github': 0.09; 'def': 0.12; 'language.': 0.14; '###': 0.16; 'belongs': 0.16; 'newline,': 0.16; 'prepended': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reinvent': 0.16; 'stdout': 0.16; 'url:py': 0.16; 'variants': 0.16; 'language': 0.16; 'wrote:': 0.18; 'skip:f 30': 0.19; 'stack': 0.19; 'example': 0.22; 'print': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'non': 0.24; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'testing': 0.29; 'possibility': 0.29; '100000': 0.31; 'prints': 0.31; 'class': 0.32; 'run': 0.32; 'url:python': 0.33; 'subject:time': 0.33; 'at:': 0.34; 'skip:_ 10': 0.34; 'maybe': 0.34; 'message.': 0.35; 'subject:with': 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'false': 0.36; 'url:org': 0.36; 'behind': 0.37; 'step': 0.37; 'sometimes': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'previous': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'space': 0.40; 'blank': 0.60; 'url:3': 0.61; 'times': 0.62; 'our': 0.64; 'charset:windows-1252': 0.65; 'here': 0.66; 'shortly': 0.68; 'url:master': 0.84; 'wheel': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: Messages with a time stamp Date: Sun, 03 May 2015 10:36:30 +0100 References: <87r3qykr39.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-78-147-17-139.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 In-Reply-To: <87r3qykr39.fsf@Equus.decebal.nl> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 81 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430645806 news.xs4all.nl 2915 [2001:888:2000:d::a6]:48273 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89843 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