From f3205da059fc53fad487cbd84a5226ae3b99f10c Mon Sep 17 00:00:00 2001 From: jylpah Date: Sun, 14 Jan 2024 19:45:06 +0200 Subject: [PATCH 1/3] add_suffix() added --- src/pyutils/utils.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/pyutils/utils.py b/src/pyutils/utils.py index bc5112a..77decb5 100644 --- a/src/pyutils/utils.py +++ b/src/pyutils/utils.py @@ -368,3 +368,11 @@ def set_config( return config.getfloat(section, option) # type: ignore else: return config.get(section, option) # type: ignore + + +def add_suffix(path : Path, suffix: str) -> Path: + """add suffix if it does not exists. Does not replace the suffix""" + if path.suffix == suffix: + return path + else: + return path.parent / (path.name + suffix) \ No newline at end of file From 780e2e8ff7d9ab0b20294f8feaefdb9d62071c9d Mon Sep 17 00:00:00 2001 From: jylpah Date: Sun, 14 Jan 2024 19:45:37 +0200 Subject: [PATCH 2/3] test for add_suffix() --- tests/test_utils.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/test_utils.py b/tests/test_utils.py index c1f6089..796fe44 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,6 +1,8 @@ from enum import Enum from math import ceil from typing import Annotated, Optional, List, Sequence + +# from unittest import result import pytest # type: ignore from pathlib import Path import click @@ -16,6 +18,7 @@ is_valid_obj, get_type, get_subtype, + add_suffix, ) from pyutils import awrap @@ -327,3 +330,17 @@ async def test_8_awrap() -> None: async for i in awrap(t): assert i == s + 1, f"invalid value returned: {i} != {s+1}" s = i + + +@pytest.mark.parametrize( + "path,suffix,res", + [ + (Path("dir/test"), ".sfx", Path("dir/test.sfx")), + (Path("dir/test.sfx"), ".sfx", Path("dir/test.sfx")), + (Path("dir/test.sfx.py"), ".sfx", Path("dir/test.sfx.py.sfx")), + ], +) +def test_9_add_suffix(path: Path, suffix: str, res: Path) -> None: + assert ( + add_suffix(path, suffix) == res + ), f"incorrect result: {str(path)} != {str(res)}" From 06944e9035435b077f61cf79f6ec3d8088c33f17 Mon Sep 17 00:00:00 2001 From: jylpah Date: Sun, 14 Jan 2024 19:46:24 +0200 Subject: [PATCH 3/3] version 1.2.3 --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 5214df6..bce08c2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "pyutils" -version = "1.2.2" +version = "1.2.3" authors = [{ name = "Jylpah", email = "jylpah@gmail.com" }] description = "Misc Python utils and classes" readme = "README.md"