PK|O3Q..codetiming/__init__.py"""A flexible, customizable timer for your Python code You can use `codetiming.Timer` in several different ways: 1. As a **class**: t = Timer(name="class") t.start() # Do something t.stop() 2. As a **context manager**: with Timer(name="context manager"): # Do something 3. As a **decorator**: @Timer(name="decorator") def stuff(): # Do something """ # Import Timer for cleaner namespace from codetiming._timer import Timer, TimerError # noqa # Versioning is handled by bump2version __version__ = "0.1.2" PKOˬcodetiming/_timer.py"""Definition of Timer See help(codetiming) for quick instructions, and https://pypi.org/project/codetiming/ for more details. """ # Standard library imports from contextlib import ContextDecorator from dataclasses import dataclass, field import time from typing import Any, Callable, ClassVar, Dict, Optional class TimerError(Exception): """A custom exception used to report errors in use of Timer class""" @dataclass class Timer(ContextDecorator): """Time your code using a class, context manager, or decorator""" timers: ClassVar[Dict[str, float]] = dict() name: Optional[str] = None text: str = "Elapsed time: {:0.4f} seconds" logger: Optional[Callable[[str], None]] = print _start_time: Optional[float] = field(default=None, init=False, repr=False) def __post_init__(self) -> None: """Initialization: add timer to dict of timers""" if self.name: self.timers.setdefault(self.name, 0) def start(self) -> None: """Start a new timer""" if self._start_time is not None: raise TimerError(f"Timer is running. Use .stop() to stop it") self._start_time = time.perf_counter() def stop(self) -> float: """Stop the timer, and report the elapsed time""" if self._start_time is None: raise TimerError(f"Timer is not running. Use .start() to start it") # Calculate elapsed time elapsed_time = time.perf_counter() - self._start_time self._start_time = None # Report elapsed time if self.logger: self.logger(self.text.format(elapsed_time)) if self.name: self.timers[self.name] += elapsed_time return elapsed_time def __enter__(self) -> "Timer": """Start a new timer as a context manager""" self.start() return self def __exit__(self, *exc_info: Any) -> None: """Stop the context manager timer""" self.stop() PK]N?O0,,"codetiming-0.1.2.dist-info/LICENSEMIT License Copyright (c) 2019 Real Python Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!HPO codetiming-0.1.2.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,szd&Y)r$[)T&UrPK!H5#codetiming-0.1.2.dist-info/METADATAWms6 _%rޚ5}65]nWesDxlM?>sD*"U`/ $:Nٝp7 .f dQ\nCR[ '@;i 6v&d+]^%0qQo+.Jdi~z0x-3mR5\XJ'o\!Jf 6iD6XOB2OiwZi{"ǃ0x.mbTpgX舼N QL*S˩uU .p(SL<2:UL$!{ !\IU=ES|>?\L `D91M}Qse'k7H&5\̭ɛQ*1݋|AJTY߬=6 V|7[Y\J%N)0rmy##Es:GR֐LW}JkCK./MFH0@'-P]T{Y.(j~)'zK^71?dzߝvve+7GܿV}bRyAqWv16f9f~\Īqh'J W(kh N6~B6<?)`<t,Ao?_vYp"kl30 fz9bWI?2} ޻2I.Ix$QxuMߟyM7:i2 T(8o Tǧ7jɛ`wWMp%xZʽG_ TX- Mאj< [aT 10z2ibvC8 `k~0zO6K :t\K7n?=W\H7$,j9+{+Rcr=klݬӍwG̹\^`{fZզ22l_pFqMǟE4!DV΂Cqd:0l"KPn/t'm my%r K)8ڂr94=rh%'N W` ᔧ<{@ ?}& ':O&(FȉFs^Ee|z &P9,qE (<>/7/Lhs{>δ+S6vdx4TWL#-jCZV#1YgcDuvbfI.eb-O-DB:ۦaDTH*X%=4^(d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