ProgressUpdater

class PlasmaCalcs.tools.timing.ProgressUpdater(print_freq=None, *, wait=False, print_time=True, clearline=True, clearN=100)

Bases: TickingWatch

class for printing messages but only when enough time has passed.

“Enough” is defined by the input parameter print_freq (in seconds).
print_freq: None or int (possibly negative or 0)
Number of seconds between progress updates.
None –> use DEFAULTS.PROGRESS_UPDATES_PRINT_FREQ
>0 –> minimum time between calls to self.tick() returning True.
=0 –> self.tick() will always return True.
<0 –> self.tick() will always return False.
wait: bool, default False
whether to wait until print_freq seconds has passed before doing the first printout.
print_time: bool, default False
whether to print time by default
clearline: bool, default True
whether to clear the current line before printing.
clearN: int, default 100
number of characters to clear if clearing a line.
Example:
updater = ProgressUpdater(print_freq=2, wait=True)
updater.print(‘This will not be printed’) # not printed because 2 seconds have not passed yet.
time.sleep(2.5) # << wait 2.5 seconds. (Or, put code here which takes >= 2 seconds)
updater.print(‘This WILL be printed!’) # prints, then timer is reset,
updater.print(‘This will not be printed’) # so it won’t print again until 2 more seconds have passed.

Methods

finalize([process_name, always])

prints 'Completed process in 0.00 seconds!', filling in the process name and time elapsed as appropriate.

force_print(*args_message[, end, print_time])

prints, without first checking whether to print.

get_marked_time([key])

returns marked time associated with this key.

get_marked_times()

returns dict of marked times.

mark_time([key, _time])

sets "marked" time to now (or _time, if provided).

message_to_print(*args_message[, sep])

returns message that would be printed, including info about time elapsed.

now()

returns time.time().

print(*args_message[, end, print_time])

prints message given by *args_message, if it is time to print.

print_clear([N, force])

clears current printed line, and moves cursor to beginning of the line.

printf([f, end, print_time])

prints message given by calling f() (which must return a single string).

reset(**kw)

resets the "t=0" time, also set self.nticks=0.

tick()

returns True if enough time has passed for the next tick.

time_elapsed_string([as_string])

returns string 'Total time elapsed: {:.2f} seconds.'.format(self.time_since_reset())

time_since_mark([key])

returns time [in seconds] since the "mark" time on this watch.

time_since_reset()

returns time [in seconds] since the "t=0" time on this watch.

time_since_tick()

get time since last tick.

_mark_tick_time(*[, _time])

mark time of this tick.

Attributes

print_freq

alias for self.interval

printed_anything

whether self has ever printed any messages.

time

alias to self.time_since_reset

time_elapsed

alias to self.time_since_reset

_mark_tick_time(*, _time=None)

mark time of this tick. Intended for internal use, only.

finalize(process_name=None, *, always=False, **kw__print)

prints ‘Completed process in 0.00 seconds!’, filling in the process name and time elapsed as appropriate.

Also, clear the line first, if self.clearline.
process_name: None (default) or string
None –> don’t print any info about the process which completed.
string –> include this name in the finalize message.
always: False (default) or True
False –> ignore this kwarg.
True –> always print the finalize message.
force_print(*args_message, end='', print_time=None, **kw__print)

prints, without first checking whether to print.

Clear the line first, if self.clearline.
print_time: None or bool
whether to print f’Total time elapsed: {format(t):.2f} seconds.’
None –> use self.print_time (default: True)
get_marked_time(key=None)

returns marked time associated with this key.

get_marked_times()

returns dict of marked times.

mark_time(key=None, *, _time=None)

sets “marked” time to now (or _time, if provided).

key: hashable object, default None
associate marked time with this key. (None –> “default” marked time.)
message_to_print(*args_message, sep=' ')

returns message that would be printed, including info about time elapsed.

Equivalent to message printed by self.print(…), if it’s time to print an update.
static now()

returns time.time(). Provided as a static method for convenience.

print(*args_message, end='', print_time=None, **kw__print)

prints message given by *args_message, if it is time to print.

Clear the line first, if self.clearline.
print_time: None or bool
whether to print f’Total time elapsed: {format(t):.2f} seconds.’
None –> use self.print_time (default: True)
print_clear(N=None, force=False)

clears current printed line, and moves cursor to beginning of the line.

only if self.clearline AND self has printed at least 1 message. OR if force.
troubleshooting: did you use end=’’, e.g. print(…, end=’’)?
(If not, your print statement may go to the next line.)
N: None or int
number of characters to clear.
None –> use self.clearN instead.
force: bool, default False
whether to force print, even if not self.clearline.
False –> require self.clearline, else don’t print.
Equivalent to:
print(’r’+ ‘ ‘*N +’r’, end=’’)
property print_freq

alias for self.interval

property printed_anything

whether self has ever printed any messages.

(Resets to False upon calling self.reset())
printf(f=<function ProgressUpdater.<lambda>>, *, end='', print_time=None, **kw__print)

prints message given by calling f() (which must return a single string).

[EFF] note: only calls f() if actually printing!
Can use this instead of self.print() if computing the print message might be expensive.
Clear the line first, if self.clearline.
print_time: None or bool
whether to print f’Total time elapsed: {format(t):.2f} seconds.’
None –> use self.print_time (default: True)
Example:
updater = ProgressUpdater(print_freq=2)
updater.printf(lambda: ‘{} ‘.format(object_whose_string_is_expensive_to_calculate))
reset(**kw)

resets the “t=0” time, also set self.nticks=0.

tick()

returns True if enough time has passed for the next tick.

If True, also “marks” the current time, as the previous tick time.
“Enough time” usually means at least self.interval seconds.
Exceptions:
if not self.waiting, return True (and set self.waiting = False)
if interval < 0, return False.
property time

alias to self.time_since_reset

property time_elapsed

alias to self.time_since_reset

time_elapsed_string(as_string=False)

returns string ‘Total time elapsed: {:.2f} seconds.’.format(self.time_since_reset())

time_since_mark(key=None)

returns time [in seconds] since the “mark” time on this watch.

“mark” set by self.mark_time(). Also set when created or reset().
key: hashable object, default None
“marked time” associated with this key.
time_since_reset()

returns time [in seconds] since the “t=0” time on this watch.

“t=0” set by self.reset(). Also set when created.
time_since_tick()

get time since last tick. Note: does not edit the “marked” time.