Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #89839 > unrolled thread

Messages with a time stamp

Started byCecil Westerhof <Cecil@decebal.nl>
First post2015-05-03 10:22 +0200
Last post2015-05-03 12:08 +0200
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

#89839 — Messages with a time stamp

FromCecil Westerhof <Cecil@decebal.nl>
Date2015-05-03 10:22 +0200
SubjectMessages with a time stamp
Message-ID<87r3qykr39.fsf@Equus.decebal.nl>
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

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [next] | [standalone]


#89841

FromMichiel Overtoom <motoom@xs4all.nl>
Date2015-05-03 11:10 +0200
Message-ID<mailman.56.1430644308.12865.python-list@python.org>
In reply to#89839
On May 3, 2015, at 10:22, Cecil Westerhof wrote:

> For testing I want my messages time stamped like:

For progress reporting, I often use the module below (eta.py), which also gives a projected time of completion:

import datetime, time, sys
etastart = 0

def eta(done, total, s, reportinterval=100, datetimeformat="%d %b %H:%M:%S"):
    global etastart
    if done == 0:
        etastart = datetime.datetime.now()
    if not done % reportinterval:
        noww = datetime.datetime.now()
        prtnow = noww.strftime(datetimeformat)
        if done:
            if not isinstance(etastart, datetime.datetime): raise RuntimeError("You should call eta() at least once with done=0")
            elapsed = noww - etastart
            secsperitem = float(elapsed.seconds) / done    
            totalsecs = secsperitem * total
            eta = etastart + datetime.timedelta(0, totalsecs)
            prteta = eta.strftime(datetimeformat)
            msg = "now %s, eta %s (%d/%d) %s" % (prtnow, prteta, done, total, s)
        else:
            msg = "now %s (%d/%d) %s" % (prtnow, done, total, s)
        sys.stderr.write(msg + "\n")
        
if __name__ == "__main__":
    for i in range(10):
        eta(i, 10, "idling for ten seconds", 1, "%H:%M:%S")
        time.sleep(1)



-- 
"You can't actually make computers run faster, you can only make them do less." - RiderOfGiraffes

[toc] | [prev] | [next] | [standalone]


#89843

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2015-05-03 10:36 +0100
Message-ID<mailman.58.1430645806.12865.python-list@python.org>
In reply to#89839
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

[toc] | [prev] | [next] | [standalone]


#89849

FromCecil Westerhof <Cecil@decebal.nl>
Date2015-05-03 12:08 +0200
Message-ID<87a8xmkm7a.fsf@Equus.decebal.nl>
In reply to#89843
Op Sunday 3 May 2015 11:36 CEST schreef Mark Lawrence:

> 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

That looks very promising. The only problem could be that it seems not
to have the possibility to do:
    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

But I could write a patch for that. Then I would be contributing
besides bugging this list. ;-)

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web