Skip to content

Commit

Permalink
add find_func_by_keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
xyluo25 committed Mar 29, 2024
1 parent 66312e4 commit a0d92df
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
42 changes: 41 additions & 1 deletion pyufunc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@
"util_pathio" : __util_pathio.__all__,
"util_test" : __util_test.__all__,
"util_vis" : __util_vis.__all__,
"pkg_utils" : __pkg_utils.__all__ + ["show_util_func_by_category", "show_util_func_by_keyword"],
"pkg_utils" : __pkg_utils.__all__ + ["show_util_func_by_category",
"show_util_func_by_keyword",
"find_func_by_keyword"],
}


Expand Down Expand Up @@ -165,4 +167,42 @@ def show_util_func_by_keyword(verbose: bool = True) -> None:
return None
return res_str


def find_func_by_keyword(keyword: str, verbose: bool = True) -> list:
"""find all available utility functions in pyufunc by keyword.
Args:
keyword (str): the keyword
verbose (bool, optional): whether to print string information. Defaults to True.
Returns:
list: if verbose is True, print the result string; otherwise return the result list.
Examples:
>>> import pyufunc as uf
>>> uf.find_func_by_keyword("show")
Available functions by keyword: show
- show_numpy_docstring_style
- show_available_utility_func
...
"""

res_str_by_keyword = ""
res_str_lst = []
ufunc_count = 0

for func_str in list(chain.from_iterable(ufunc_category.values())):
if keyword.lower() in func_str.lower():
res_str_by_keyword += f" \n{ufunc_count + 1}. {func_str}\n"
res_str_lst.append(func_str)
ufunc_count += 1

if verbose:
res_str_head = f"Available functions by keyword: {keyword}"
res_str = res_str_head + f" ({ufunc_count}):\n" + res_str_by_keyword

print(res_str)
return ""
return res_str_lst

__all__ = list(chain(*ufunc_category.values()))
15 changes: 14 additions & 1 deletion pyufunc/util_img/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,18 @@
# Author/Copyright: Mr. Xiangyong Luo
##############################################################

from ._img_cvt import (
cvt_img_to_bytes,
cvt_PIL_img_to_bytes,
cvt_img_bytes_to_PIL_img,
)

__all__ = []
__all__ = [
# _img_cvt
"cvt_img_to_bytes",
"cvt_PIL_img_to_bytes",
"cvt_img_bytes_to_PIL_img",

# _img_rotate

]
11 changes: 8 additions & 3 deletions pyufunc/util_img/_img_cvt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
# Contact Info: [email protected]
# Author/Copyright: Mr. Xiangyong Luo
##############################################################


from PIL import Image
from __future__ import annotations
from typing import TYPE_CHECKING
import io

# https://stackoverflow.com/questions/61384752/how-to-type-hint-with-an-optional-import
if TYPE_CHECKING:
# check the support version of python
# https://pillow.readthedocs.io/en/stable/installation.html
from PIL import Image


def cvt_img_to_bytes(img_path: str) -> bytes:
"""Convert image to bytes
Expand Down
5 changes: 4 additions & 1 deletion requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ Shapely>=2.0.3
sphinx_multiproject>=1.0.0rc1
urllib3>=2.2.1
tqdm
pyufunc
pyufunc

pillow
opencv-python

0 comments on commit a0d92df

Please sign in to comment.