added recursive functions and number length function

This commit is contained in:
2025-10-04 19:17:17 +02:00
parent 1c557913f9
commit c1e76b660a

View File

@@ -7,6 +7,28 @@ from .refit_logger import logger
# reusable
def get_int_length(number: int) -> int:
"""Takes an number as its input and returns the numbers diget amount.
This function takes an integer number, converts it into a string and
converts its digets. It returns the length of the number as an integer.
Args:
number (int): The number you need the length of
Examples:
>>> get_int_length(100)
'3'
>>> get_int_length(10)
'2'
>>> get_int_length(4000)
'4'
"""
number_string = str(number)
amount_didgets = len(number_string)
return amount_didgets
def get_standard_name_number(current_number: int, number_str_length: int) -> str:
"""Returns a number string filled to the length of the input number
@@ -24,14 +46,14 @@ def get_standard_name_number(current_number: int, number_str_length: int) -> str
>>> get_standard_name_number(23, 4)
'0023'
"""
logger.debug(
f"FUNC: get_standard_name_number() index={current_number} string_length={number_str_length}"
)
# logger.debug(
# f"FUNC: get_standard_name_number() index={current_number} string_length={number_str_length}"
# )
temp_current_number = str(current_number)
standard_name_number = str.zfill(temp_current_number, number_str_length)
logger.debug(
f"FUNC: get_standard_name_number() return value= '{standard_name_number}'"
)
# logger.debug(
# f"FUNC: get_standard_name_number() return value= '{standard_name_number}'"
# )
return standard_name_number
@@ -59,15 +81,8 @@ def get_standard_folder_name(name: str) -> str:
'directory_name'
"""
logger.debug(
f"FUNC: get_standard_folder_name() MSG: entered function with value: '{name}'"
)
standard_folder_name = name[0] if name is not None else "directory"
logger.debug(
f"FUNC: get_standard_folder_name() MSG: exiting function with folder name: '{standard_folder_name}'"
)
return standard_folder_name
@@ -95,15 +110,8 @@ def get_standard_file_name(name) -> str:
'file_name'
"""
logger.debug(
f"FUNC: get_standard_file_name() MSG: entered function with name='{name}'"
)
standard_file_name = name[0] if name is not None else "file"
logger.debug(
f"FUNC: get_standard_file_name MSG: Continuing with expected input: '{standard_file_name}'"
)
logger.debug("FUNC get_standard_file_name() MSG: Exit")
return standard_file_name
@@ -125,16 +133,16 @@ def get_current_path(path) -> str:
if path is None:
# Set the current directory if none is passed with the command.
path = "."
logger.warning(
f"FUNC: {get_current_path.__name__}() MSG: Path now has the value: '{path}'"
)
# logger.warning(
# f"FUNC: {get_current_path.__name__}() MSG: Path now has the value: '{path}'"
# )
return path
else:
# Checks if the path, entered by the user, exists.
if os.path.exists(path) is True:
logger.debug(
f"FUNC: {get_current_path.__name__} MSG: Path '{path}' exists, continue...."
)
# logger.debug(
# f"FUNC: {get_current_path.__name__} MSG: Path '{path}' exists, continue...."
# )
return path
else:
ERROR_MESSAGE = (
@@ -145,37 +153,44 @@ def get_current_path(path) -> str:
sys.exit(1)
# TODO: - add logging
# TODO: - add dynamic name input
# - add docstring
# - add proper naming to the files
def create_recursive_folders(input_path, target_depth, current_depth, width):
"""Creates a recursive directory structure with y as depth and x as width"""
def create_linear_directories(input_path, target_depth, current_depth):
"""Creates the linear directories for the x*y pattern"""
# Exits the recursion if the target depth is reached.
if current_depth >= target_depth:
logger.debug(
f"FUNC: create_linear_directories(entered) VALUES: path='{input_path}', target_depth='{target_depth}', current_depth='{current_depth}'"
)
if current_depth > target_depth:
return
# Getting the lenth of the number string which gets added to the dir name.
length_width = len(str(width))
directory_name = "level_" + get_standard_name_number(
current_depth,
get_int_length(target_depth),
)
path = os.path.join(input_path, directory_name)
os.mkdir(path)
create_linear_directories(path, target_depth, current_depth + 1)
# TODO: - add dynamic name input
# - add docstring
def create_parallel_directories(input_path, target_depth, width):
"""Creates directories after the pattern x*y"""
logger.debug(
f"FUNC: create_recursive_folders(length_width) MSG: given values: path={input_path} target_depth={target_depth} current_depth={current_depth}, with={width}"
f"FUNC: create_parallel_directories(entered) VALUES: path='{input_path}', target_depth='{target_depth}', width='{width}'"
)
# Creates the directories of the current depth
# # Getting the lenth of the number string which gets addedto the dir name.
# length_width = get_int_length(width)
for i in range(width):
# Getting the numbering of the folder.
folder_index = i + 1
new_directory_name = "sub_dir" + get_standard_name_number(
folder_index, length_width
)
new_directory_path = os.path.join(input_path, new_directory_name)
os.mkdir(new_directory_path)
print(new_directory_path)
logger.debug(f"New Path: path={new_directory_path}")
directory_name = "branch_" + get_standard_name_number(i, get_int_length(width))
path = os.path.join(input_path, directory_name)
os.mkdir(path)
create_recursive_folders(
new_directory_path,
target_depth,
current_depth + 1,
width,
)
create_linear_directories(path, target_depth, current_depth=0)