-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstopwatch.py
904 lines (793 loc) · 32.8 KB
/
stopwatch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
"""
===============================================================================
Developed by Tariq Shihadah
tariq.shihadah@gmail.com
10/08/2018
===============================================================================
"""
################
# DEPENDENCIES #
################
import time
from statistics import mean, median, stdev
from tarpy.util.general import chunkit, infinite_count
#############
# STOPWATCH #
#############
class Stopwatch(object):
"""
A functional, stopwatch-like tool with the ability to report time passed
and lap times in terms of seconds or hours:minutes:seconds using a default
or a given reporting format. The stopwatch can also report basic
statistics on lap times, such as min, max, mean, median, and standard
deviation, as well as supervise timed loops, periodic time reporting, and
more.
Parameters
----------
:start: `boolean, default True`
whether to start the stopwatch upon initialization
:numeric: `boolean, default False` whether to default to reporting time
values numerically or with a formatted string
:hms: `boolean, default True` whether to default to reporting time
values in terms of hours, minutes, and seconds (returned as a
list if numeric is True)
:form: `string, default None`
a formatting string to use when reporting time values as a
formatted string
:process: `func, default None`
a function to process raw results of seconds for reporting
purposes (supersedes numeric, hms, and form parameters)
:resource: `{'perf_counter', 'monotonic', 'process_time', 'time'},
default 'perf_counter'`
which function from the `time` module to use to perform timing
operations
Methods
-------
:laps: returns a list of lap times for all completed laps
:check: returns the total elapsed time on the stopwatch
:lap: returns the total elapsed time for the current lap and begins
a new lap
:check_after: returns the total elapsed time on the stopwatch after a
specified number of function calls
:lap_after: returns the total elapsed time for the current lap and begins
a new lap after a specified number of function calls
:pause: pauses the stopwatch
:start: resumes the stopwatch
:reset: resets the stopwatch
:hit: increase the number of hits on the stopwatch by one or a
specified number
:reset_hits: reset the number of hits on the stopwatch to zero
:lastlap: returns the lap time of the most recently logged lap
:maxlap: returns the lap time of the longest lap
:minlap: returns the lap time of the shortest lap
:meanlap: returns the mean of all lap times
:medianlap: returns the median of all lap times
:stdevlap: returns the standard deviation of all lap times
:func_timer: function wrapper which logs each call of the function as a
lap
:loop_timer: creates an iterable which logs each chunk of iterations as a
lap and can report each chunk's operation time
:timed_loop: iterate the given iterable for a fixed amount of time before
cutting it off
:now: report current time in datetime format
:report: report active time passed along with a text message
:report_now: report current time along with a text message
Properties
----------
:active: returns a boolean indicating whether the stop watch is active
(True) or paused (False)
:inactive: returns a boolean indicating whether the stop watch is paused
(True) or active (False)
:resource: returns the selected `time` module function being used to
perform timing operations
:hits: returns the number of hits on the stopwatch
"""
def __init__(self, start=True, numeric=False, hms=True, form=None,
process=None, resource='perf_counter'):
# Get timing resource
try:
self._resource = getattr(time, resource)
except AttributeError:
raise AttributeError("""Please select a timing resource from the \
list of default options: {'perf_counter', 'monotonic', 'process_time', \
'time'}.""")
# Initialize attributes
self.reset(start=start)
# Set default time reporting format
self.numeric = numeric
self.hms = hms
self.process = process
if hms:
if form is None:
self.form = "{0:.0f}:{1:02.0f}:{2:05.2f}"
else:
self.form = form
else:
if form is None:
self.form = "{0:.2f}"
else:
self.form = form
def __str__(self):
return str(self.check(numeric=False))
@property
def resource(self):
return self._resource
@property
def _time(self):
if self._pause:
return self._pause
else:
return self._resource()
@property
def active(self):
return not bool(self._pause)
@property
def inactive(self):
return bool(self._pause)
@property
def hits(self, **kwargs):
return self._hits
def laps(self, **kwargs):
return [self._report(lap, **kwargs) for lap in self._laps]
def stats(self, **kwargs):
"""
Print the statistics of all logged laps.
"""
print("""\
Lap Statistics
--------------
Count: {} laps
Range: {} - {}
Median: {}
Mean: {}
Stdev.: {}
--------------
""".format(len(self._laps), self.minlap(), self.maxlap(), self.medianlap(),
self.meanlap(), self.stdevlap()))
def lastlap(self, **kwargs):
"""
Return the last logged lap length.
Returns
-------
a report of the maximum of lap lengths
"""
if len(self._laps) < 1:
val = 0
else:
val = self._laps[-1]
return self._report(val, **kwargs)
def maxlap(self, **kwargs):
"""
Return the maximum lap length.
Returns
-------
a report of the maximum of lap lengths
"""
if len(self._laps) < 1:
val = 0
else:
val = max(self._laps)
return self._report(val, **kwargs)
def minlap(self, **kwargs):
"""
Return the minimum lap length.
Returns
-------
a report of the minimum of lap lengths
"""
if len(self._laps) < 1:
val = 0
else:
val = min(self._laps)
return self._report(val, **kwargs)
def meanlap(self, **kwargs):
"""
Return the mean lap length.
Returns
-------
a report of the mean of lap lengths
"""
if len(self._laps) < 1:
val = 0
else:
val = mean(self._laps)
return self._report(val, **kwargs)
def medianlap(self, **kwargs):
"""
Return the median lap length.
Returns
-------
a report of the median of lap lengths
"""
if len(self._laps) < 1:
val = 0
else:
val = median(self._laps)
return self._report(val, **kwargs)
def stdevlap(self, **kwargs):
"""
Return the standard deviation of lap lengths.
Returns
-------
a report of the standard deviation of lap lengths
"""
if len(self._laps) < 1:
val = 0
else:
val = stdev(self._laps)
return self._report(val, **kwargs)
def check(self, start=False, pause=False, **kwargs):
"""
Return the amount of active time passed.
Parameters
----------
:start: `boolean` whether to automatically start the stopwatch
upon calling the function if it is not already active
:pause: `boolean` whether to automatically pause the stopwatch
upon calling the function if it is not already active
Returns
-------
a report of the total elapsed active time
"""
# Pause or start stopwatch if requested
self.pause(pause)
self.start(start)
# Get amount of active time passed
stamp = kwargs.get('stamp', self._time)
self._check = stamp - self._start - self._less
return self._report(self._check, **kwargs)
def lap(self, printit=False, start=False, pause=False, log=True,
check=False, form=None, **kwargs):
"""
Split lap, and return the amount of time passed since the last split.
Parameters
----------
:printit: `boolean` whether to automatically print the output to
the standard out or to return it instead
:start: `boolean` whether to automatically start the stopwatch
upon calling the function if it is not already active
:pause: `boolean` whether to automatically pause the stopwatch
upon calling the function if it is not already active
:log: `boolean` whether to log the lap time in the object laps
property
:form: `string` a formatting string to use when printing results
if printing is requested; should include formatting keys
of 'l' for laps, 'c' for total active time
:check: `boolean` whether to also return the total amount of
active time passed as a second value
Returns
-------
a report of the most recent lap time; also returns the total amount
of time passed at the time of the lap split as a second value if
requested
"""
# Pause or start stopwatch if requested
self.pause(pause)
self.start(start)
# Get split and lap times
get_split = self._time
get_lap = get_split - self._split - self._lap_less
# Log lap time (if the timer is running or if it is paused but elapsed
# time is greater than zero)
if log and (not self._pause or get_lap > 0):
self._laps.append(get_lap)
self._lap = get_lap
self._split = get_split
self._lap_less = 0 # Remove lap subtractions
# Report
res = (self._report(get_lap, **kwargs), self.check(stamp=get_split))
if printit:
if form is None:
form='Lap Time: {l}; \tTotal Time: {c}'
print(form.format(l=res[0], c=res[1]))
elif check:
return res
else:
return res[0]
def check_after(self, after=None, printit=False, start=False, pause=False,
**kwargs):
"""
Return the amount of active time passed after a number of hits.
Parameters
----------
:after: `integer` the number of hits (calls of this function)
which must occur before a lap gets logged
:printit: `boolean` whether to automatically print the output to
the standard out or to return it instead
:start: `boolean` whether to automatically start the stopwatch
upon calling the function if it is not already active
:pause: `boolean` whether to automatically pause the stopwatch
upon calling the function if it is not already active
Returns
-------
a report of the total elapsed active time
"""
# Pause or start stopwatch if requested
self.pause(pause)
self.start(start)
self.hit(**kwargs)
if self._hits % after == 0:
res = self.check(**kwargs)
if printit:
print(res)
else:
return res
else:
return None
def lap_after(self, after=None, printit=False, start=False, pause=False,
form=None, form_vals=None, log=True, check=False, **kwargs):
"""
Split lap after a number of hits, and return the amount of time passed
since the last split.
Parameters
----------
:after: `integer` the number of hits (calls of this function)
which must occur before a lap gets logged
:printit: `boolean` whether to automatically print the output to
the standard out or to return it instead
:start: `boolean` whether to automatically start the stopwatch
upon calling the function if it is not already active
:pause: `boolean` whether to automatically pause the stopwatch
upon calling the function if it is not already active
:form: `string` a formatting string to use when printing results
if printing is requested; should include formatting keys
of 'h' for hits, 'l' for laps, 'c' for total active time
:form_vals: `dict` a dictionary of formatting values to be entered in
the formatting string
:log: `boolean` whether to log the lap time in the object laps
property
:check: `boolean` whether to also return the total amount of
active time passed as a second value
Returns
-------
a report of the most recent lap time; also returns the total amount
of time passed at the time of the lap split as a second value if
requested
"""
# Pause or start stopwatch if requested
self.pause(pause)
self.start(start)
self.hit(**kwargs)
if self._hits % after == 0:
res = self.lap(log=log, check=True, **kwargs)
if printit:
if form is None:
form='Hits: {h}; \tLap Time: {l}; \tTotal Time: {c}'
if type(form_vals) is dict:
print(form.format(h=self._hits, l=res[0], c=res[1],
**form_vals))
elif form_vals is None:
print(form.format(h=self._hits, l=res[0], c=res[1]))
else:
raise TypeError("""Formatting values must be provided as a
dictionary of formatting keys and inputs""")
else:
return res if check else res[0]
else:
return None
def hit(self, hits=1, **kwargs):
"""
Increase the number of hits on the stopwatch by one or a given number.
Parameters
----------
:hits: `integer` the number of hits to increase the hit count by
"""
self._hits += hits
def reset_hits(self):
"""
Reset the number of hits on the stopwatch to zero.
"""
self._hits = 0
def pause(self, pauseit=True):
"""
Pause the stopwatch, stopping time for both checks and laps.
Returns
-------
None
"""
if pauseit:
# Check if the stopwatch is currently active
if not self._pause:
# Set pause time as current time (True)
self._pause = self._time
def start(self, startit=True):
"""
Start the stopwatch, resuming time for both checks and laps.
Returns
-------
None
"""
if startit:
# Check if the stopwatch is currently paused
if self._pause:
# Update pause subtractions
self._less += self._resource() - self._pause
self._lap_less += self._resource() - self._pause
# Set pause time to zero (False)
self._pause = 0
def reset(self, start=True, **kwargs):
"""
Reset the stopwatch and all stopwatch and lap variables.
Parameters
----------
:start: `boolean` whether to automatically start the stopwatch
upon re-initialization
Returns
-------
None
"""
# Get start time
_start = kwargs.get('_start', self._resource())
_less = kwargs.get('_less', 0)
self._start = self._split = _start
self._check = 0
self._lap = 0
self._hits = 0
# Set pause subtractions
if start:
self._pause = 0
else:
self._pause = _start
self._less = _less
self._lap_less = 0
# Reset records
self._laps = []
# Reset operational values
self._break_loop = False
def func_timer(self, func, report=False):
"""
Function wrapper which times each call of the decorated function,
logging the operation time of the function as a new lap. The wrapper
automatically pauses the stopwatch between calls. The wrapper can
print a report of function operation time if requested.
Parameters
----------
:func: `function` the target function which will wrapped, timed,
and reported on
:report: `boolean, default True` whether to print split time
reports for each operation of the wrapped function
"""
self.reset(start=False)
def wrapper(*args, **kwargs):
self.start()
result = func(*args, **kwargs)
if report: print("Operation time: {}".format(
self.lap(pause=True)))
else: self.lap(pause=True)
return result
return wrapper
def loop_timer(self, iterable, every=None, report=False):
"""
Creates a self-reporting iterable from an input iterable, printing
split and total times elapsed for the iteration of a loop. The report
is generated periodically based on the input `every` integer. To not
print reports but only log them as laps, set the `report` parameter to
`False`.
Parameters
----------
:iterable: `iterable` an iterable which will be iterated by the
function, yielding the same values
:every: `integer, default 1` the number of iterated items to be
evaluated in each timer report
:report: `boolean, default False` whether to print split time
reports for the iteration of each chunk of items
Yield
-----
iterable values
"""
# Initialize
self._break_loop = False
# Divide iterable into chunks of input size
if every is None:
every = 1
else:
try:
every = int(every)
if every < 1:
raise ValueError("The 'every' parameter must be > 0")
except:
raise TypeError("The 'every' parameter must be an integer")
chunks = chunkit(iterable, size=every)
if report:
try:
i = len(iterable)
except:
i = 0
for chunk in chunks:
i += len(chunk)
j = len(chunks)
print("Begin loop timer ({} items in {} chunks).".format(i, j))
self.reset(start=False) # Reset stopwatch
# Iterate over chunks of input iterable
for i, chunk in enumerate(chunks):
# Iterate and yield items within the chunk
self.start()
for j, x in enumerate(chunk):
yield x
if self._break_loop: break
# Log/report times
if report: print("""\
Items: {:,.0f} - {:,.0f} \tSplit time: {} \tTotal time: {}\
""".format(i*every+1, i*every+j+1, *self.lap(check=True)))
else: self.lap()
if self._break_loop: break
if report: print("End loop timer.")
def timed_loop(self, iterable=None, s=0, m=0, h=0, cutoff=0, **kwargs):
"""
Iterate the given iterable for a fixed amount of time before cutting
it off.
Parameters
----------
:iterable: `iterable` an iterable which will be iterated by the
function, yielding the same values; if no value is given,
an infinite counter will be initialized, starting at 0
and counting by 1 until the input time has elapsed
:s: `int, float` number of seconds to use for expiratoin timer
:m: `int, float` number of minutes to use for expiratoin timer
:h: `int, float` number of hours to use for expiratoin timer
:cutoff: `int {0: overtime, 1: lastlap, 2: meanlap, 3: medianlap,
4: maxlap}, default 0}` whether to cut-off iteration early
to avoid over-running expiration time based on the
expected time of the next lap, estimating based on the
last, mean, median, or max lap time
:kwargs: keyword arguments passed to the loop_timer method
Yield
-----
iterable values
"""
# Compute expiration time
timer = s + 60*m + 3600*h
# Define cutoff method options
every = kwargs.get('every', 1)
cutoff_ops = {1:'lastlap', 2:'meanlap', 3:'medianlap', 4: 'maxlap'}
# If no iterable given, create infinite generator
if iterable is None:
iterable = infinite_count()
# Iterate
for i, x in enumerate(self.loop_timer(iterable, **kwargs)):
# Yield next value
yield x
# Check for end of chunk
if (i) % every == 0:
# Check for expiration
elapsed = self.check(numeric=True, hms=False)
if cutoff == 0:
if elapsed >= timer:
self._break_loop = True
# Check if there is time for another lap before expiration
else:
try:
func = getattr(self, cutoff_ops[cutoff])
x = func(numeric=True, hms=False)
if elapsed + x*every > timer:
self._break_loop = True
except:
raise ValueError("Invalid cutoff option.")
def sync(self, *sws):
"""
Synchronize input stopwatch instances with this stopwatch by resetting
them and matching their active time variables.
Parameters
----------
:sw: `Stopwatch` instances of the Stopwatch object type to
synchronize with the current stopwatch
"""
# Iterate over input arguments
for sw in sws:
# Confirm that input arguments are Stopwatch-type
if not isinstance(sw, Stopwatch):
raise TypeError("Input arguments must be Stopwatch-type.")
# Syncronize stopwatch information
sw.reset(start=self.active, _start=self._start, _less=self._less)
def now(self, printit=False, form=None, long=False):
"""
Report the current time in datetime format.
Parameters
----------
:printit: `boolean` whether to automatically print the output to
the standard out or to return it instead
:form: `string` a formatting string to use when reporting time
values as a formatted string (ignored if numeric is True);
the string will receive three inputs of hours, minutes,
and seconds if hms is True and will receive one input of
seconds if hms is False
"""
if form is None:
if long:
form = "%A %B %d, %Y %H:%M:%S"
else:
form = "%Y-%m-%d %H:%M:%S"
res = time.strftime(form)
if printit:
print(res)
else:
return res
def report(self, text, *fmts, reset=False, **kwargs):
"""
Print the current amount of active time passed along with a given
text message. This is helpful when periodically reporting on the
progress of a lengthy process.
Parameters
----------
:text: `str` a text message to include after the reported active
time passed
:*fmts: `str` formatting strings to be passed to the string
formatting function which gets called on the provided
text message; can also be passed through kwargs
:reset: whether to reset the stopwatch at the call of the function
"""
# Check active time passed
if reset:
self.reset(start=False)
res = self.check(start=True, **kwargs)
else:
res = self.check(**kwargs)
# Format text string
text = str(text)
text = '[{}] '.format(res) + text.format(*fmts, **kwargs)
print(text)
def report_new(self, text, *fmts, **kwargs):
"""
Reset the stopwatch and report no time passed along with a given
text message. This is helpful when periodically reporting on the
progress of a lengthy process, providing the initial message and
baseline time point.
Parameters
----------
:text: `str` a text message to include after the reported active
time passed
:*fmts: `str` formatting strings to be passed to the string
formatting function which gets called on the provided
text message; can also be passed through kwargs
"""
self.report(text, *fmts, reset=True, **kwargs)
def report_beg(self, text, *fmts, **kwargs):
"""
Reset the stopwatch and report no time passed along with a given
text message. This is helpful when periodically reporting on the
progress of a lengthy process, providing the initial message and
baseline time point.
Parameters
----------
:text: `str` a text message to include after the reported active
time passed
:*fmts: `str` formatting strings to be passed to the string
formatting function which gets called on the provided
text message; can also be passed through kwargs
"""
print("START TIME: {}".format(self.now(**kwargs)))
self.report(text, *fmts, reset=True, **kwargs)
def report_end(self, text, *fmts, **kwargs):
"""
Reset the stopwatch and report no time passed along with a given
text message. This is helpful when periodically reporting on the
progress of a lengthy process, providing the initial message and
baseline time point.
Parameters
----------
:text: `str` a text message to include after the reported active
time passed
:*fmts: `str` formatting strings to be passed to the string
formatting function which gets called on the provided
text message; can also be passed through kwargs
"""
self.report(text, *fmts, reset=False, **kwargs)
print("END TIME: {}".format(self.now(**kwargs)))
def report_now(self, text, *fmts, **kwargs):
"""
Print the current time along with a given text message. This is
helpful when periodically reporting on the progress of a lengthy
process.
Parameters
----------
:text: `str` a text message to include after the reported active
time passed
:*fmts: `str` formatting strings to be passed to the string
formatting function which gets called on the provided
text message; can also be passed through kwargs
"""
# Format text string
text = str(text)
text = '[{}] '.format(self.now(**kwargs)) + \
text.format(*fmts, **kwargs)
print(text)
def _report(self, delta, printit=False, **kwargs):
"""
Report the given amount of seconds elapsed in the stopwatch's chosen
or default format.
"""
# Get formatting info
numeric = kwargs.get('numeric', self.numeric)
hms = kwargs.get('hms', self.hms)
form = kwargs.get('form', self.form)
# If a report process is given, apply
if not self.process is None:
res = self.process(delta)
# If numeric report requested, apply
if numeric:
# If hour-minute-second disaggregation requested, apply
if hms:
res = s_to_hms(delta)
else:
res = delta
# If formatted report requested (else), apply
else:
# If hour-minute-second disaggregation requested, apply
if hms:
try:
res = form.format(*s_to_hms(delta))
except IndexError:
raise ValueError("Invalid formatting string.")
else:
try:
res = form.format(delta)
except IndexError:
raise ValueError("Invalid formatting string.")
# Return or print the result
if printit:
print(res)
return None
else:
return res
########################
# SUPPORTING FUNCTIONS #
########################
def stopwatch(numeric=False, hms=True, form=None):
"""
Creates a Stopwatch object based on the input parameters and returns a
handle for the check function, which will return the time difference
between the original call of the stopwatch function and the call of the
check function.
Parameters
----------
:numeric: `boolean` whether to default to reporting time values
numerically or with a formatted string
:hms: `boolean` whether to default to reporting time values in terms
of hours, minutes, and seconds (returned as a list if numeric
is True)
:form: `string` a formatting string to use when reporting time values
as a formatted string (ignored if numeric is True); the string
will receive three inputs of hours, minutes, and seconds if
hms is True and will receive one input of seconds if hms is
False
Returns
-------
a stopwatch object, initialized using the given parameters
"""
obj = Stopwatch(numeric=numeric, hms=hms, form=form)
return obj.check
def timed_loop(iterable=None, s=0, m=0, h=0, **kwargs):
"""
"""
sw = Stopwatch()
return sw.timed_loop(iterable=iterable, s=s, m=m, h=h, **kwargs)
def s_to_hms(s):
"""
Convert a number of seconds into a three-number tuple of hours, minutes,
and seconds.
Parameters
----------
:seconds: `numeric` number of seconds to convert to hours, minutes, and
seconds
Returns
-------
a tuple of hours, minutes, and seconds
"""
h = s // 3600
m = (s % 3600) // 60
s = s % 60
return h, m, s
def hms_to_s(h, m, s):
"""
Convert numbers of hours, minutes, and seconds into a number of seconds.
Parameters
----------
:h: `numeric` number of hours to convert to seconds
:m: `numeric` number of minutes to convert to seconds
:s: `numeric` number of seconds
Returns
-------
total number of seconds
"""
seconds = h * 3600 + m * 60 + s
return seconds