Сравнение кода. Опрос.
После долгого затишья хочу начать с простенького опроса.
Два примера ниже выполняют одну и ту же задачу. Какой вариант вам нравится больше? И почему? Напишите в комментах.
Первый вариант:
def _write_steps(
self,
steps: List[SoftwareProfileStep],
profile_dir_path: Path,
) -> None:
"""
Записать шаги профиля в файловое хранилище
Args:
steps: Список шагов профиля управления ПО.
profile_dir_path: Путь к директории профиля в файловом хранилище
"""
steps_j2 = []
for step in steps:
if step.type == SoftwareProfileStepType.PACKAGE:
step_var_uid = self._write_step_vars(
step,
profile_dir_path,
)
steps_j2.append(
self._step_package_template.format(
action=step.package_action,
step_vars=step_var_uid,
)
)
elif step.type == SoftwareProfileStepType.REPOSITORY:
steps_j2.append(
self._step_repo_template.format(
step_uid=step.uid,
)
)
steps_list = list(map(str, steps_j2))
steps_str = self.__to_jinja_statement(
self._steps_template.format(
steps=steps_list,
)
)
with Path(profile_dir_path / "steps.j2").open("w") as steps_file:
steps_file.write(steps_str)
Второй вариант:
def _write_steps(
self,
steps: List[SoftwareProfileStep],
profile_dir_path: Path,
) -> None:
"""
Записать шаги профиля в файловое хранилище
Args:
steps: Список шагов профиля управления ПО.
profile_dir_path: Путь к директории профиля в файловом хранилище
"""
steps_j2 = [
self.__fill_step_template(step)
for step in steps
]
steps_list = list(map(str, steps_j2))
steps_str = self.__to_jinja_statement(
self._steps_template.format(
steps=steps_list,
)
)
with Path(profile_dir_path / "steps.j2").open("w") as steps_file:
steps_file.write(steps_str)
@staticmethod
def __fill_step_template(step: SoftwareProfileStep):
def _package_template(step: SoftwareProfileStep):
step_var_uid = self._write_step_vars(
step,
profile_dir_path,
)
return self._step_package_template.format(
action=step.package_action,
step_vars=step_var_uid,
)
def _repo_template(step: SoftwareProfileStep):
return self._step_repo_template.format(
step_uid=step.uid,
)
actions = {
SoftwareProfileStepType.PACKAGE: _packege_template,
SoftwareProfileStepType.REPOSITORY: _repo_template,
}
return actions.get(step.type)(step)