Skip to content

Commit

Permalink
Merge pull request #120 from Jylpah/dev-1.0
Browse files Browse the repository at this point in the history
version 1.2.3: `add_suffix()` added to utils
  • Loading branch information
Jylpah authored Jan 14, 2024
2 parents e4e9f4a + 06944e9 commit ea799ba
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "pyutils"
version = "1.2.2"
version = "1.2.3"
authors = [{ name = "Jylpah", email = "[email protected]" }]
description = "Misc Python utils and classes"
readme = "README.md"
Expand Down
8 changes: 8 additions & 0 deletions src/pyutils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
17 changes: 17 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,6 +18,7 @@
is_valid_obj,
get_type,
get_subtype,
add_suffix,
)
from pyutils import awrap

Expand Down Expand Up @@ -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)}"

0 comments on commit ea799ba

Please sign in to comment.