Skip to content

Commit

Permalink
fix relative path error
Browse files Browse the repository at this point in the history
  • Loading branch information
xyluo25 committed Jan 15, 2025
1 parent 8f56939 commit c986cd8
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pyufunc/util_data_processing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Author/Copyright: Mr. Xiangyong Luo
##############################################################

from _data_cleaning import get_layer_boundary
from ._data_cleaning import get_layer_boundary
from ._dict import (dict_split_by_chunk,
dict_delete_keys)
from ._int_to_alpha import cvt_int_to_alpha
Expand Down
86 changes: 86 additions & 0 deletions tests/_path_setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
'''
##############################################################
# Created Date: Wednesday, January 15th 2025
# Contact Info: luoxiangyong01@gmail.com
# Author/Copyright: Mr. Xiangyong Luo
##############################################################
'''
from pathlib import Path
import sys
import os


def add_pkg_to_sys_path(pkg_name: str, verbose: bool = True) -> bool:
"""Automatically finds an importable Python package by its name
in the current directory, parent directories, or child directories,
and adds it to the system path.
This is useful when writing test functions and
needing to import a package that is not installed.
Args:
package_name (str): The name of the package to locate and add.
verbose (bool): Whether to print the process info. Defaults to True.
Location:
pyufunc/util_pathio/_path.py
Examples:
>>> import pyufunc as pf
>>> pf.add_pkg_to_sys_path('my_package', False)
Returns:
bool: True if the package is found and added to the system path, otherwise
"""

# TDD: check if the package path is a string
if not isinstance(pkg_name, str):
raise ValueError("pkg_path should be a string.")

# Helper function to check if a directory is an importable Python package
def is_importable_package(path):
return os.path.isdir(path) and (
# Check for traditional package
os.path.isfile(os.path.join(path, '__init__.py'))
# Accept single-module package
or any(fname.endswith('.py') for fname in os.listdir(path))
)

# Helper function to locate the package in the directory tree
def locate_package(start_path, package_name):
for root, dirs, _ in os.walk(start_path):
if package_name in dirs:
package_path = os.path.join(root, package_name)
if is_importable_package(package_path):
return package_path
return None

# Start searching in the current directory and parent directories
current_dir = os.getcwd()
while True:
# Look for the package in the current directory and its subdirectories
package_path = locate_package(current_dir, pkg_name)
if package_path:
break
# Move to the parent directory
parent_dir = os.path.dirname(current_dir)
if parent_dir == current_dir: # Stop if we've reached the root
break
current_dir = parent_dir

# If found, add to the system path
if package_path:
absolute_path = Path(os.path.abspath(package_path))
absolute_path_parent = absolute_path.parent.absolute()

if absolute_path_parent not in sys.path:
sys.path.insert(0, str(absolute_path_parent))
if verbose:
print(f"Added {absolute_path_parent} to system path.")
else:
if verbose:
print(f"{absolute_path_parent} is already in the system path.")
return True

print(f" :Importable package '{pkg_name}' not found"
"in the current directory, parent directories, or children.")
return False
31 changes: 17 additions & 14 deletions tests/test_dp_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,67 @@
##############################################################

from __future__ import absolute_import

import pytest
from pyufunc.util_data_processing._list import split_list_by_equal_sublist, split_list_by_fixed_length

from _path_setup import add_pkg_to_sys_path
add_pkg_to_sys_path("pyufunc")

from pyufunc import list_split_by_equal_sublist, list_split_by_fixed_length


class TestSplitListByEqualSublist:
def test_split_list_by_equal_sublist(self):
def test_list_split_by_equal_sublist(self):
lst = list(range(10))
lst_ = split_list_by_equal_sublist(lst, num_of_sub=3)
lst_ = list_split_by_equal_sublist(lst, num_of_sub=3)
assert [list(dat) for dat in lst_] == [[0, 1, 2, 3], [4, 5, 6], [7, 8, 9]]

def test_with_invalid_num_of_sub(self):
with pytest.raises(AssertionError) as excinfo:
list(split_list_by_equal_sublist(list(range(10)), 0))
list(list_split_by_equal_sublist(list(range(10)), 0))
assert "num_of_sub should be a positive integer" in str(excinfo.value)

def test_with_invalid_lst_type(self):
lst = str(list(range(10)))
with pytest.raises(AssertionError) as excinfo:
list(split_list_by_equal_sublist(lst, 3))
list(list_split_by_equal_sublist(lst, 3))
assert "lst should be a list" in str(excinfo.value)

def test_with_invalid_num_of_sub_type(self):
lst = list(range(10))
with pytest.raises(AssertionError) as excinfo:
list(split_list_by_equal_sublist(lst, "3"))
list(list_split_by_equal_sublist(lst, "3"))
assert "num_of_sub should be an integer" in str(excinfo.value)

def test_with_num_of_sub_greater_than_lst_length(self):
lst = list(range(10))
lst_ = list(split_list_by_equal_sublist(lst, num_of_sub=20))
lst_ = list(list_split_by_equal_sublist(lst, num_of_sub=20))
assert lst == lst_


class TestSplitListByFixedLength:
def test_split_list_by_fixed_length(self):
def test_list_split_by_fixed_length(self):
lst = list(range(10))
lst_ = split_list_by_fixed_length(lst, fixed_length=3)
lst_ = list_split_by_fixed_length(lst, fixed_length=3)
assert [list(dat) for dat in lst_] == [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]

def test_with_invalid_length(self):
with pytest.raises(AssertionError) as excinfo:
list(split_list_by_fixed_length(list(range(10)), fixed_length=0))
list(list_split_by_fixed_length(list(range(10)), fixed_length=0))
assert "fixed_length should be a positive integer" in str(excinfo.value)

def test_with_invalid_lst_type(self):
lst = str(list(range(10)))
with pytest.raises(AssertionError) as excinfo:
list(split_list_by_fixed_length(lst, fixed_length=3))
list(list_split_by_fixed_length(lst, fixed_length=3))
assert "lst should be a list" in str(excinfo.value)

def test_with_invalid_length_type(self):
lst = list(range(10))
with pytest.raises(AssertionError) as excinfo:
list(split_list_by_fixed_length(lst, fixed_length="3"))
list(list_split_by_fixed_length(lst, fixed_length="3"))
assert "fixed_length should be an integer" in str(excinfo.value)

def test_with_length_greater_than_lst_length(self):
lst = list(range(10))
lst_ = split_list_by_fixed_length(lst, fixed_length=20)
lst_ = list_split_by_fixed_length(lst, fixed_length=20)
assert [list(dat) for dat in lst_] == [lst]
3 changes: 2 additions & 1 deletion tests/test_pathio_io.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import pytest
from pathlib import Path
import shutil

from pyufunc.util_pathio._io import get_file_size, get_dir_size
from pyufunc import path2linux
import shutil


class TestGetFileSize:
Expand Down

0 comments on commit c986cd8

Please sign in to comment.