Skip to content

Commit 0f08aa3

Browse files
authored
Merge pull request #437 from legendu-net/dev
Merge dev into main
2 parents 22c3f26 + 25c3f98 commit 0f08aa3

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

aiutil/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""A utils Python package for data scientists."""
22

3-
__version__ = "0.84.0"
3+
__version__ = "0.85.0"

aiutil/filesystem.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22
"""Filesystem related util functions."""
33

4+
from collections import namedtuple
45
from typing import Iterable, Callable
56
import itertools
67
import os
@@ -18,6 +19,7 @@
1819
import dulwich.porcelain
1920

2021
HOME = Path.home()
22+
PosixPathPair = namedtuple("PosixPathPair", ["prefix", "base"])
2123

2224

2325
def copy_if_exists(src: str, dst: str | Path = HOME) -> bool:
@@ -691,3 +693,28 @@ def filter(
691693
if sub_pattern:
692694
return _filter_sp(path, pattern=pattern, sub_pattern=sub_pattern)
693695
return _filter_num(path, pattern=pattern, num_lines=num_lines)
696+
697+
698+
def trace_dir_upwards(path: str | Path, name: str) -> PosixPathPair:
699+
"""Find the parent directory with the specified name.
700+
701+
Args:
702+
path: A local path contains `/name/`.
703+
name: The base name (stem) of the parent directory.
704+
705+
Returns:
706+
A PosixPathPair which contains the parent directory
707+
and the relative path to this parent directory.
708+
"""
709+
710+
def _trace_dir_upwards(path: Path) -> Path:
711+
while (stem := path.stem) != name:
712+
if not stem:
713+
raise ValueError(f"The path {path} does not contain /{name}/!")
714+
path = path.parent
715+
return path
716+
717+
if isinstance(path, str):
718+
path = Path(path)
719+
prefix = _trace_dir_upwards(path)
720+
return PosixPathPair(prefix, path.relative_to(prefix))

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "aiutil"
3-
version = "0.83.0"
3+
version = "0.85.0"
44
description = "A utils Python package for data scientists."
55
authors = [{ name = "Benjamin Du", email = "[email protected]" }]
66
requires-python = ">=3.10,<3.13"

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)