Skip to content

Commit

Permalink
Chore: publish; Update: stem parent as list
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanHeng committed Feb 21, 2024
1 parent 319d843 commit edd721f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

VERSION = '0.36.0'
VERSION = '0.37.0'
DESCRIPTION = 'Machine Learning project startup utilities'
LONG_DESCRIPTION = 'My commonly used utilities for machine learning projects'

Expand All @@ -13,7 +13,7 @@
description=DESCRIPTION,
long_description=LONG_DESCRIPTION,
url='https://github.com/StefanHeng/stef-util',
download_url='https://github.com/StefanHeng/stef-util/archive/refs/tags/v0.36.0.tar.gz',
download_url='https://github.com/StefanHeng/stef-util/archive/refs/tags/v0.37.0.tar.gz',
packages=find_packages(),
include_package_data=True,
install_requires=[
Expand Down
13 changes: 9 additions & 4 deletions stefutil/os_n_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
from os.path import join as os_join
from pathlib import Path
from typing import Union
from typing import List, Union


__all__ = ['get_hostname', 'stem']
Expand All @@ -15,17 +15,20 @@ def get_hostname() -> str:
return os.uname().nodename


def stem(path: Union[str, Path], keep_ext=False, top_n: int = None) -> Union[str, Path]:
def stem(
path: Union[str, Path], keep_ext=False, top_n: int = None, as_list: bool = False
) -> Union[str, List[str]]:
"""
:param path: A potentially full path to a file
:param keep_ext: If True, file extensions is preserved
:param top_n: If given, keep the top `top_n` parent directories
:param as_list: If True, return as a list
Relevant only when `top_n` is given
:return: The file name, without parent directories
"""
if top_n:
ret = stem(path=path, keep_ext=keep_ext, top_n=None)
if isinstance(path, Path):

dirs = []
for _ in range(top_n):
path = path.parent
Expand All @@ -34,7 +37,8 @@ def stem(path: Union[str, Path], keep_ext=False, top_n: int = None) -> Union[str
else:
dirs = path.split(os.sep)
dirs = dirs[-top_n-1:-1]
return os_join(*dirs, ret)
dirs += [ret]
return dirs if as_list else os_join(*dirs)
else:
return os.path.basename(path) if keep_ext else Path(path).stem

Expand All @@ -46,6 +50,7 @@ def check_stem():
path_ = Path(path)
print(path)
print(stem(path, top_n=n))
print(stem(path, top_n=n, as_list=True))
print(path_)
print(stem(path_, top_n=n))
check_stem()

0 comments on commit edd721f

Please sign in to comment.