Spaces:
Sleeping
Sleeping
File size: 1,735 Bytes
1bf41f9 8ccf878 f988527 1bf41f9 8ccf878 1bf41f9 8ccf878 447b452 1bf41f9 8ccf878 f988527 1bf41f9 afb0c77 271d94c 8ccf878 afb0c77 1bf41f9 f988527 1bf41f9 afb0c77 8ccf878 afb0c77 8ccf878 afb0c77 1bf41f9 f988527 1bf41f9 8ccf878 1bf41f9 8ccf878 afb0c77 1bf41f9 8ccf878 1bf41f9 afb0c77 8ccf878 afb0c77 1bf41f9 |
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 |
# -*- coding: utf-8 -*-
"""ใฆใผใใฃใชใใฃ"""
from dataclasses import dataclass
import time
def get_package_version() -> str:
"""
ใใผใธใงใณๆ
ๅ ฑ
"""
return '0.0.8'
@dataclass
class Stopwatch:
"""
็ต้ๆ้ใ่จๆธฌใใใใใฎใฏใฉในใ
Example:
from src.utils import Stopwatch
watch = Stopwatch.start_new()
### ่จๆธฌใใๅฆ็
print(f"{watch.elapsed:.3f}")
"""
_start_time: float = 0
_elapsed: float = 0
_is_running: bool = False
@property
def elapsed(self) -> float:
"""
็ต้ๆ้ใๅๅพใใพใใ
"""
if self._is_running:
end_time = time.perf_counter()
self._elapsed = end_time - self._start_time
return self._elapsed
@property
def is_running(self) -> bool:
"""
ๅฎ่กไธญใใฉใใใๅๅพใใพใใ
"""
return self._is_running
def start(self) -> None:
"""
่จๆธฌใ้ๅงใใพใใ
"""
self._start_time = time.perf_counter()
self._elapsed = 0
self._is_running = True
@classmethod
def start_new(cls):
"""
ในใใใใฆใฉใใใ็ๆใ่จๆธฌใ้ๅงใใพใใ
"""
stopwatch = Stopwatch()
stopwatch.start()
return stopwatch
def stop(self) -> float:
"""
่จๆธฌใ็ตไบใใพใใ
"""
if self._is_running:
end_time = time.perf_counter()
self._elapsed = end_time - self._start_time
self._is_running = False
return self._elapsed
|