PKNN8;codetiming/__init__.py"""A flexible, customizable timer for your Python code You can use `codetiming.Timer` in several different ways: 1. As a **class**: ```python t = Timer(name="class") t.start() # Do something t.stop() ``` 2. As a **context manager**: ```python with Timer(name="context manager"): # Do something ``` 3. As a **decorator**: ```python @Timer(name="decorator") def stuff(): # Do something ``` """ from contextlib import ContextDecorator from dataclasses import dataclass, field import time from typing import Any, Callable, ClassVar, Dict, Optional __version__ = "0.1.0" 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() PK8N?O0,,"codetiming-0.1.0.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.0.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,szd&Y)r$[)T&UrPK!Hfќ #codetiming-0.1.0.dist-info/METADATAVko6_qJIAݦhNФahHƇ߽lY͌l$7,e~J YFp +xL(gf(G- V ,_ GL*XIzetཊܘJGr0aQ$20: >RTG$gZ{S)Hy"3R#Tn<QfUmiwDp1%׉{ociz #XT. $2%_\Vb0c5D@^c[1YLy # ^&~z [ׅW7cU ܊Ork+GV,oY.BgYJ^8,y%%̓k WQVrXAdkZQg? eoz\o$SsPS#Y $&R (iKMӜ%w?L1E·S,gwi0OaUz uwz\< 51y)bB*x4G xq#i1Q`\jm+S@8 8 Dr7mI*9.i ʅ 潝]+L:l1b5H/{"~VE؊:,c 4D"Y}Vs0`)>~0xڍ xq ;k!fL׏Ą nT5@V+q < kًnG`ssj\g Y>FjfI^4Ҧ>$2 R#y.$l 圗 s@"b9Lb1aTnր&j;{4̡~zw؀o+-\}2<oe j+STqd.ynA(ۑVh)!m o],z^UH]=veܸ:D<~J1X$l\8 )ߑY-JUsaӓv<+JU#SWȿR_uWw[ ECMnwV4L7mYQ引4՘z7¸hSvhttr=p>IĹ6d:~2ׇtc;MDW?Mz1 azџ&" CMB?0ڀĖmsv*)]n4qG;{s7?[T/Ы;"md0PKNN8;codetiming/__init__.pyPK8N?O0,,"3 codetiming-0.1.0.dist-info/LICENSEPK!HPO  codetiming-0.1.0.dist-info/WHEELPK!Hfќ #-codetiming-0.1.0.dist-info/METADATAPK!H![!codetiming-0.1.0.dist-info/RECORDPK*