Skip to content

Utils

Utils module.

======================================================================================================================== Name: pymodeller/utils.py Description: Utils module. Project: PyModeller

Functions:

Name Description
compare_dirs

Compare dirs.

deep_merge

Combine two dicts.

ensure_init_py_in_subdirectories

Recursively walks through all directories starting from root_dir

file_hash

Generate hash.

generate_init_file

Inspects Python files within a directory, extracts classes, and generates an init.py file.

get_file_hash

Compute SHA-256 hash of a file.

get_import_path

Converts a file path structure into a valid Python dot-notation import path.

get_variants

Convert a string into snake_case, camelCase, and UPPER_CASE.

to_camel_case

Convert snake_case or UPPER_CASE to camelCase.

to_pascal_case

Generate class.

to_snake_case

Convert CamelCase or mixed strings to snake_case.

write_env_file

Save env file.

compare_dirs

compare_dirs(dir1: Path, dir2: Path) -> dict

Compare dirs.

deep_merge

deep_merge(base: dict, overrides: dict) -> dict

Combine two dicts.

ensure_init_py_in_subdirectories

ensure_init_py_in_subdirectories(
    root_dir: str | Path,
) -> list[Path]

Recursively walks through all directories starting from root_dir and creates an empty init.py file if one does not exist.

Parameters:

Name Type Description Default
root_dir str | Path

The target directory path to inspect.

required

Returns:

Type Description
list[Path]

List of Path objects representing all init.py files created.

file_hash

file_hash(path: Path) -> str

Generate hash.

generate_init_file

generate_init_file(package_dir: Path | str) -> Path

Inspects Python files within a directory, extracts classes, and generates an init.py file.

Scans all .py files inside the target directory, gathers exported class definitions, and uses a Jinja2 template to generate an alphabetically sorted __init__.py.

Parameters:

Name Type Description Default
package_dir Path | str

The directory path containing the Python files to inspect.

required

Returns:

Name Type Description
Path Path

The file path to the generated __init__.py.

get_file_hash

get_file_hash(path: Path) -> str

Compute SHA-256 hash of a file.

get_import_path

get_import_path(
    base_dir: str, subfolder: str, file_name: str
) -> str

Converts a file path structure into a valid Python dot-notation import path.

Example

'./src/event_driven', 'domain/schemas', 'user.py' becomes 'event_driven.domain.schemas.user'

Parameters:

Name Type Description Default
base_dir str

The base directory path (e.g., './src/event_driven').

required
subfolder str

The relative subfolder path (e.g., 'domain/schemas').

required
file_name str

The target filename (e.g., 'user.py').

required

Returns:

Name Type Description
str str

The dot-separated Python import module path.

get_variants

get_variants(name: str) -> str

Convert a string into snake_case, camelCase, and UPPER_CASE. Returns a list of unique values.

Parameters:

Name Type Description Default
name str

The input string (supports snake_case, camelCase, etc.)

required

Returns:

Type Description
str

List of unique strings: [snake, camel, upper]

to_camel_case

to_camel_case(snake_str: str) -> str

Convert snake_case or UPPER_CASE to camelCase.

to_pascal_case

to_pascal_case(text: str) -> str

Generate class.

to_snake_case

to_snake_case(name: str) -> str

Convert CamelCase or mixed strings to snake_case.

Parameters:

Name Type Description Default
name str

The string to convert (e.g., "CamelCase", "camelCase", "my-header").

required

Returns:

Name Type Description
str str

The converted snake_case string.

write_env_file

write_env_file(
    path: Path, data: dict, prefix: str = ""
) -> None

Save env file.