added recursive functions and number length function
This commit is contained in:
@@ -7,6 +7,28 @@ from .refit_logger import logger
|
|||||||
# reusable
|
# 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:
|
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
|
"""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)
|
>>> get_standard_name_number(23, 4)
|
||||||
'0023'
|
'0023'
|
||||||
"""
|
"""
|
||||||
logger.debug(
|
# logger.debug(
|
||||||
f"FUNC: get_standard_name_number() index={current_number} string_length={number_str_length}"
|
# f"FUNC: get_standard_name_number() index={current_number} string_length={number_str_length}"
|
||||||
)
|
# )
|
||||||
temp_current_number = str(current_number)
|
temp_current_number = str(current_number)
|
||||||
standard_name_number = str.zfill(temp_current_number, number_str_length)
|
standard_name_number = str.zfill(temp_current_number, number_str_length)
|
||||||
logger.debug(
|
# logger.debug(
|
||||||
f"FUNC: get_standard_name_number() return value= '{standard_name_number}'"
|
# f"FUNC: get_standard_name_number() return value= '{standard_name_number}'"
|
||||||
)
|
# )
|
||||||
return standard_name_number
|
return standard_name_number
|
||||||
|
|
||||||
|
|
||||||
@@ -59,15 +81,8 @@ def get_standard_folder_name(name: str) -> str:
|
|||||||
'directory_name'
|
'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"
|
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
|
return standard_folder_name
|
||||||
|
|
||||||
|
|
||||||
@@ -95,15 +110,8 @@ def get_standard_file_name(name) -> str:
|
|||||||
'file_name'
|
'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"
|
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
|
return standard_file_name
|
||||||
|
|
||||||
|
|
||||||
@@ -125,16 +133,16 @@ def get_current_path(path) -> str:
|
|||||||
if path is None:
|
if path is None:
|
||||||
# Set the current directory if none is passed with the command.
|
# Set the current directory if none is passed with the command.
|
||||||
path = "."
|
path = "."
|
||||||
logger.warning(
|
# logger.warning(
|
||||||
f"FUNC: {get_current_path.__name__}() MSG: Path now has the value: '{path}'"
|
# f"FUNC: {get_current_path.__name__}() MSG: Path now has the value: '{path}'"
|
||||||
)
|
# )
|
||||||
return path
|
return path
|
||||||
else:
|
else:
|
||||||
# Checks if the path, entered by the user, exists.
|
# Checks if the path, entered by the user, exists.
|
||||||
if os.path.exists(path) is True:
|
if os.path.exists(path) is True:
|
||||||
logger.debug(
|
# logger.debug(
|
||||||
f"FUNC: {get_current_path.__name__} MSG: Path '{path}' exists, continue...."
|
# f"FUNC: {get_current_path.__name__} MSG: Path '{path}' exists, continue...."
|
||||||
)
|
# )
|
||||||
return path
|
return path
|
||||||
else:
|
else:
|
||||||
ERROR_MESSAGE = (
|
ERROR_MESSAGE = (
|
||||||
@@ -145,37 +153,44 @@ def get_current_path(path) -> str:
|
|||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
# TODO: - add logging
|
# TODO: - add dynamic name input
|
||||||
# - add docstring
|
# - add docstring
|
||||||
# - add proper naming to the files
|
def create_linear_directories(input_path, target_depth, current_depth):
|
||||||
def create_recursive_folders(input_path, target_depth, current_depth, width):
|
"""Creates the linear directories for the x*y pattern"""
|
||||||
"""Creates a recursive directory structure with y as depth and x as width"""
|
|
||||||
|
|
||||||
# Exits the recursion if the target depth is reached.
|
logger.debug(
|
||||||
if current_depth >= target_depth:
|
f"FUNC: create_linear_directories(entered) VALUES: path='{input_path}', target_depth='{target_depth}', current_depth='{current_depth}'"
|
||||||
|
)
|
||||||
|
|
||||||
|
if current_depth > target_depth:
|
||||||
return
|
return
|
||||||
|
|
||||||
# Getting the lenth of the number string which gets added to the dir name.
|
directory_name = "level_" + get_standard_name_number(
|
||||||
length_width = len(str(width))
|
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(
|
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):
|
for i in range(width):
|
||||||
# Getting the numbering of the folder.
|
directory_name = "branch_" + get_standard_name_number(i, get_int_length(width))
|
||||||
folder_index = i + 1
|
path = os.path.join(input_path, directory_name)
|
||||||
new_directory_name = "sub_dir" + get_standard_name_number(
|
os.mkdir(path)
|
||||||
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}")
|
|
||||||
|
|
||||||
create_recursive_folders(
|
create_linear_directories(path, target_depth, current_depth=0)
|
||||||
new_directory_path,
|
|
||||||
target_depth,
|
|
||||||
current_depth + 1,
|
|
||||||
width,
|
|
||||||
)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user