tools¶
Classes¶
Functions¶
generate_nested_dict ¶
Generate a nested dictionary structure representing directory hierarchy.
Recursively walks through a directory tree and constructs a nested dictionary that mirrors the folder structure with files organized under their respective directories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path_storage | str | Root directory path to generate structure from. | required |
Returns:
| Name | Type | Description |
|---|---|---|
dict | dict | Nested dictionary representing directory hierarchy with sorted file lists. |
Examples:
>>> generate_nested_dict("/path/to/project")
{
'folder1': {
'subfolder1': ['file1.txt', 'file2.txt'],
'subfolder2': ['file3.txt']
},
'folder2': ['file4.txt']
}
Source code in pyadvtools/tools.py
iterate_obtain_full_file_names ¶
iterate_obtain_full_file_names(
path_storage,
extension,
reverse=True,
is_standard_file_name=True,
search_year_list=[],
)
Recursively retrieve full file paths with specified extension.
Walks through a directory tree and collects files matching the given extension, with optional filtering based on year patterns and sorting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path_storage | str | Root directory path to search for files. | required |
extension | str | Target file extension to filter (e.g., 'txt', 'csv'). | required |
reverse | bool | If True, sorts files in reverse order; otherwise natural order. | True |
is_standard_file_name | bool | If True, enables year-based filtering. | True |
search_year_list | list[str] | List of years to filter filenames. | [] |
Returns:
| Type | Description |
|---|---|
list[str] | List[str]: List of full file paths matching criteria, sorted accordingly. |
Examples:
>>> files = iterate_obtain_full_file_names("/path", "txt", True, True, ["2023"])
# Returns all .txt files from 2023, sorted in reverse order
Source code in pyadvtools/tools.py
transform_to_data_list ¶
transform_to_data_list(
original_data,
extension,
reverse=False,
is_standard_file_name=True,
search_year_list=[],
insert_flag=None,
before_after="after",
)
Transform input data from various formats into a unified list of strings.
Supports multiple input types including directories, files, raw strings, and string lists, returning a consolidated list of text lines.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
original_data | list[str] | str | Input source - directory path, file path, multi-line string, or list of strings. | required |
extension | str | Target file extension to filter when processing directories. | required |
reverse | bool | Whether to reverse the order of files when reading from directory. | False |
is_standard_file_name | bool | Whether to use standardized file name processing. | True |
search_year_list | list[str] | Optional list of years to filter files by. | [] |
insert_flag | list[str] | str | None | Content to insert between combined data chunks. | None |
before_after | str | Insert position relative to existing content. | 'after' |
Returns:
| Type | Description |
|---|---|
list[str] | List[str]: Consolidated list of text lines from all processed sources. |
Examples:
>>> transform_to_data_list("/path/to/files", "txt")
# Returns combined content from all .txt files in directory
>>> transform_to_data_list("line1\nline2", "txt")
['line1\n', 'line2\n']