Debian Codegraph @

planetary_system_stacker/timer.py

raw symbols SCIP
1 # -*- coding: utf-8; -*-
2 """
3 Copyright (c) 2018 Rolf Hempel, [email protected]
4
5 This file is part of the PlanetarySystemStacker tool (PSS).
6 https://github.com/Rolf-Hempel/PlanetarySystemStacker
7
8 PSS is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with PSS. If not, see <http://www.gnu.org/licenses/>.
20
21 """
22
23 from time import time, sleep
24 from collections import OrderedDict
25
26 from exceptions import ArgumentError
27 from miscellaneous import Miscellaneous
28
29
30 class timer(object):
31 """
32 Measure execution times of code sections. Several timers can be used at the same time. Timers
33 can be created, incremented, and deleted.
34 """
35
36 def __init__(self):
37 """
38 Initialize the timer object.
39 """
40
41 self.counters = OrderedDict()
42
43 def create(self, name):
44 """
45 Create a named timer, and start it.
46
47 :param name: Name of the timer
48 :return: -
49 """
50
51 if name in self.counters.keys():
52 raise ArgumentError("Attempt to initialize timer with existing name")
53 else:
54 self.counters[name] = [0., time()]
55
56 def create_no_check(self, name):
57 """
58 If a named timer does not exist, create one. If it exists, reset and start it.
59
60 :param name: Name of the timer
61 :return: -
62 """
63
64 self.counters[name] = [0., time()]
65
66 def delete(self, name):
67 """
68 Delete a named timer.
69
70 :param name: Name of the timer
71 :return: -
72 """
73
74 if name not in self.counters.keys():
75 raise ArgumentError("Attempt to delete timer with undefined name")
76 else:
77 del self.counters[name]
78
79 def exists(self, name):
80 """
81 Find out if a named timer with a given name already exists.
82
83 :param name: Name of the timer
84 :return: True, if the timer exists; otherwise False.
85 """
86
87 return name in self.counters.keys()
88
89 def start(self, name):
90 """
91 Start a timer.
92
93 :param name: Name of the timer
94 :return: -
95 """
96
97 if name not in self.counters.keys():
98 raise ArgumentError("Attempt to start timer with undefined name")
99 else:
100 self.counters[name][1] = time()
101
102 def stop(self, name):
103 """
104 Stop a timer.
105
106 :param name: Name of the timer
107 :return: Elapsed time accumulated during start/stop intervals of this timer so far.
108 """
109
110 if name not in self.counters.keys():
111 raise ArgumentError("Attempt to stop timer with undefined name")
112 else:
113 self.counters[name][0] += time() - self.counters[name][1]
114 return self.counters[name][0]
115
116 def read(self, name):
117 """
118 Read out a timer.
119
120 :param name: Name of the timer
121 :return: Elapsed time accumulated during start/stop intervals of this timer so far.
122 """
123
124 if name not in self.counters.keys():
125 raise ArgumentError("Attempt to read out timer with undefined name")
126 else:
127 return self.counters[name][0]
128
129 def reset(self, name):
130 """
131 Reset a timer to zero without deleting it.
132
133 :param name: Name of the timer.
134 :return: -
135 """
136
137 if name not in self.counters.keys():
138 raise ArgumentError("Attempt to reset timer with undefined name")
139 else:
140 self.counters[name][0] = 0.
141
142 def print(self):
143 print("--------------------------------------------------\nStatus of time counters:")
144 for name in self.counters.keys():
145
146 print("{0:40} {1:8.3f}".format(name, self.counters[name][0]))
147 print("--------------------------------------------------")
148
149 def protocol(self, logfile):
150 Miscellaneous.protocol("", logfile, precede_with_timestamp=False)
151 Miscellaneous.protocol(" --------------------------------------------------\n"
152 " Status of time counters:", logfile,
153 precede_with_timestamp=False)
154 for name in self.counters.keys():
155 Miscellaneous.protocol(
156 " {0:40} {1:8.3f}".format(name, self.counters[name][0]), logfile,
157 precede_with_timestamp=False)
158 Miscellaneous.protocol(" --------------------------------------------------\n",
159 logfile, precede_with_timestamp=False)
160
161
162 if __name__ == "__main__":
163 my_timer = timer()
164 my_timer.create('Second')
165 my_timer.create('First')
166 try:
167 my_timer.create('First')
168 except ArgumentError as e:
169 print(e.message)
170
171 my_timer.start('First')
172 sleep(0.1)
173 print("First timer: " + str(my_timer.stop('First')))
174
175 my_timer.start('First')
176 sleep(0.5)
177 print("First timer: " + str(my_timer.stop('First')))
178
179 start = time()
180 sleep(0.1)
181 print("Elapsed time (without timer): " + str(time() - start))
182
183 my_timer.start('Second')
184 sleep(0.1)
185 print("Second timer: " + str(my_timer.stop('Second')))
186
187 my_timer.reset('First')
188 my_timer.start('First')
189 sleep(0.5)
190 print("First timer: " + str(my_timer.stop('First')))
191
192 my_timer.print()
193
194 my_timer.delete('First')
195 try:
196 my_timer.read('First')
197 except ArgumentError as e:
198 print(e.message)