Skip to content

Commit 7f44f13

Browse files
authored
Add tts function and tests for it. (#2)
* Add tts fimction and tests * Add tts fimction and tests * Fix tox * Add ipdb and ipython to poetry dev group deps
1 parent 3e6a139 commit 7f44f13

File tree

8 files changed

+604
-6
lines changed

8 files changed

+604
-6
lines changed

VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.0.0

poetry.lock

Lines changed: 500 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyhelper_utils/__init__.py

Whitespace-only changes.

pyhelper_utils/general.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import re
2+
3+
4+
def tts(ts):
5+
"""
6+
Convert time string to seconds.
7+
8+
Args:
9+
ts (str): time string to convert, can be and int followed by s/m/h
10+
if only numbers was sent return int(ts)
11+
12+
Example:
13+
>>> tts(ts="1h")
14+
3600
15+
>>> tts(ts="3600")
16+
3600
17+
18+
Returns:
19+
int: Time in seconds
20+
"""
21+
try:
22+
time_and_unit = re.match(r"(?P<time>\d+)(?P<unit>\w)", str(ts)).groupdict()
23+
except AttributeError:
24+
return int(ts)
25+
26+
_time = int(time_and_unit["time"])
27+
_unit = time_and_unit["unit"].lower()
28+
if _unit == "s":
29+
return _time
30+
elif _unit == "m":
31+
return _time * 60
32+
elif _unit == "h":
33+
return _time * 60 * 60
34+
else:
35+
return int(ts)

pyproject.toml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
[tool.coverage.run]
2+
omit = ["tests/*"]
3+
4+
[tool.coverage.report]
5+
fail_under = 80
6+
skip_empty = true
7+
8+
[tool.coverage.html]
9+
directory = ".tests_coverage"
10+
111
[tool.ruff]
212
preview = true
313
line-length = 120
@@ -6,3 +16,46 @@ output-format = "grouped"
616

717
[tool.ruff.format]
818
exclude = [".git", ".venv", ".mypy_cache", ".tox", "__pycache__"]
19+
20+
[tool.poetry]
21+
name = "pyhelper-utils"
22+
version = "0.0.0"
23+
description = "Collective utility functions for python projects"
24+
readme = "README.md"
25+
repository = "https://github.com/RedHatQE/pyhelper-utils"
26+
authors = [
27+
"Meni Yakove <myakove@gmail.com>",
28+
"Ruth Netser <rnetser@gmail.com>",
29+
"Debarati Basu-Nag <dbasunag@redhat.com>",
30+
]
31+
license = "Apache-2.0"
32+
packages = [{ include = "pyhelper_utils" }]
33+
homepage = "https://github.com/RedHatQE/pyhelper-utils"
34+
documentation = "https://github.com/RedHatQE/pyhelper-utils/blob/main/README.md"
35+
classifiers = [
36+
"Programming Language :: Python :: 3",
37+
"Operating System :: OS Independent",
38+
]
39+
40+
[tool.poetry.dependencies]
41+
python = "^3.8"
42+
43+
[tool.poetry.group.tests.dependencies]
44+
pytest = "^8.1.1"
45+
pytest-cov = "^5.0.0"
46+
47+
48+
[tool.poetry.group.dev.dependencies]
49+
ipdb = "^0.13.13"
50+
ipython = "*"
51+
52+
[tool.poetry-dynamic-versioning]
53+
enable = true
54+
pattern = "((?P<epoch>\\d+)!)?(?P<base>\\d+(\\.\\d+)*)"
55+
56+
[tool.poetry-dynamic-versioning.substitution]
57+
files = ["VERSION"]
58+
59+
[build-system]
60+
requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning"]
61+
build-backend = "poetry_dynamic_versioning.backend"

pytest.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[pytest]
2+
addopts =
3+
--cov-config=pyproject.toml --cov-report=html --cov-report=term --cov=pyhelper_utils

tests/test_general.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from pyhelper_utils.general import tts
2+
3+
4+
def test_tts():
5+
assert tts("1m") == 60
6+
assert tts("1h") == 3600
7+
assert tts("3600") == 3600

tox.ini

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
[tox]
2-
envlist = unused-code
2+
envlist = unittests
33
skipsdist = True
44

5-
#Unused code
6-
[testenv:unused-code]
5+
[testenv:unittests]
76
basepython = python3
8-
recreate=True
97
setenv =
108
PYTHONPATH = {toxinidir}
119
deps =
12-
python-utility-scripts
10+
poetry
1311
commands =
14-
pyappsutils-unusedcode --exclude-function-prefixes 'process_webhook'
12+
poetry install
13+
poetry run pytest tests

0 commit comments

Comments
 (0)