Skip to content

Commit

Permalink
add typehints and remove return self
Browse files Browse the repository at this point in the history
  • Loading branch information
ravener committed Jun 17, 2022
1 parent 6d2e7bf commit 9534e86
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 47 deletions.
8 changes: 2 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Stopwatch.py
============
A simple stopwatch for python, small, efficient and no dependencies
A simple stopwatch for Python.

Install
-------
Requires Python 3+ since this module depends on ``time.perf_counter()`` which does not exist in Python 2.
Requires Python 3.5+

.. code:: sh
Expand Down Expand Up @@ -34,7 +34,3 @@ Usage
License
-------
MIT

Credits
-------
Originally written by `dirigeants <https://github.com/dirigeants>`_ in `this file <https://github.com/dirigeants/klasa/blob/master/src/lib/util/Stopwatch.js>`_ i just looked at it and rewrote it in python, and it felt useful to put it in a module.
22 changes: 12 additions & 10 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
from setuptools import setup, find_packages
from setuptools import setup
from pathlib import Path

description = "A simple stopwatch for python"

long_description = ""

with open("README.rst") as f:
long_description = f.read()

version = "1.1.0"

long_description = Path("README.rst").read_text("utf-8")
version = "2.0.0"
packages = ["stopwatch"]

setup(
Expand All @@ -21,10 +17,16 @@
author="Ravener",
author_email="ravener.anime@gmail.com",
license="MIT",
packages=find_packages(exclude=["test"]),
python_requires=">=3",
packages=packages,
python_requires=">=3.5",
classifiers=[
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
Expand Down
2 changes: 1 addition & 1 deletion stopwatch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
__license__ = "MIT"
__author__ = "Ravener"
__github__ = "https://github.com/ravener/stopwatch.py"
__version__ = "1.1.0"
__version__ = "2.0.0"
29 changes: 12 additions & 17 deletions stopwatch/stopwatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,49 +23,44 @@
"""
import time

# Ported from https://github.com/dirigeants/klasa/blob/master/src/lib/util/Stopwatch.js

class Stopwatch:
def __init__(self, digits=2):
digits: int

def __init__(self, digits: int = 2):
self.digits = digits

self._start = time.perf_counter()
self._end = None

@property
def duration(self):
def duration(self) -> float:
return (
self._end - self._start if self._end else time.perf_counter() - self._start
)

@property
def running(self):
def running(self) -> bool:
return not self._end

def restart(self):
def restart(self) -> None:
self._start = time.perf_counter()
self._end = None

return self

def reset(self):
def reset(self) -> None:
self._start = time.perf_counter()
self._end = self._start

return self

def start(self):
def start(self) -> None:
if not self.running:
self._start = time.perf_counter() - self.duration
self._end = None

return self

def stop(self):
def stop(self) -> None:
if self.running:
self._end = time.perf_counter()

return self

def __str__(self):
def __str__(self) -> str:
time = self.duration

if time >= 1:
Expand Down
22 changes: 9 additions & 13 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@
import time
import unittest

# Testing various functions to check if it works as expected
# To run this you will need python3.5+ due to async/await usage
# To make it compatible with python3.4 change the
# `async def` to a
# @asyncio.coroutine
# def stuff():
# ...
# and the `await` to a `yield from`
# For even lower python versions where asyncio didn't exist, just comment that lines


class StopwatchTest(unittest.TestCase):
def testStopwatch(self):
Expand All @@ -43,18 +33,22 @@ def testStop(self):
"""Tests stopwatch's stopped state"""
stopwatch = Stopwatch()
stopwatch.stop()

now = str(stopwatch)
time.sleep(0.1)
after = str(stopwatch)

# A stopped stopwatch should not move
self.assertEqual(now, after)

def testRunning(self):
"""Tests that the running boolean works as expected"""
stopwatch = Stopwatch()
self.assertTrue(stopwatch.running)

stopwatch.stop()
self.assertFalse(stopwatch.running)

stopwatch.restart()
self.assertTrue(stopwatch.running)

Expand All @@ -72,16 +66,18 @@ def testDuration(self):
stopwatch = Stopwatch()
time.sleep(1)
stopwatch.stop()
duration = stopwatch.duration * 1000
self.assertTrue(duration >= 1000)

self.assertTrue(stopwatch.duration >= 1)

def testAsync(self):
"""Tests that it doesn't do any bad behaviors on asyncio event loop"""

async def main():
stopwatch = Stopwatch()
await asyncio.sleep(1)
self.assertTrue((stopwatch.duration * 1000) >= 1000)
stopwatch.stop()

self.assertTrue(stopwatch.duration >= 1)

asyncio.run(main())

Expand Down

0 comments on commit 9534e86

Please sign in to comment.