Skip to content

gufo.thor.utils

Various utilities.

ensure_directory(path)

Ensure that the directory exists.

Parameters:

Name Type Description Default
path Path

Directory path.

required

humanize_size(size)

Convert a size in bytes to a human-readable representation.

Sizes of 1 KiB and above are represented using binary units: K, M, and G. Values below 1 KiB are represented in bytes.

Parameters:

Name Type Description Default
size int

Size in bytes.

required

Returns:

Type Description
str

Human-readable size with at most one decimal place.

humanize_time(duration)

Convert a duration to a compact human-readable representation.

Parameters:

Name Type Description Default
duration timedelta

Duration to format.

required

Returns:

Type Description
str

Duration expressed in days, hours, minutes, and seconds.

is_test()

Check if code executed in the test suite.

Returns:

Name Type Description
True bool

If is in test suite.

False bool

Otherwise.

merge_dict(x, y)

Deep merge dictionaries.

Do not affect source dictionaries. Second dictionary overrides first.

Parameters:

Name Type Description Default
x dict[str, Any]

First dictionary.

required
y dict[str, Any]

Second dictionary.

required

Returns:

Type Description
dict[str, Any]

Merged dictionary.

split_version(v)

Parse a version string into comparable integer components.

Parameters:

Name Type Description Default
v str

Dot-separated version string.

required

Returns:

Type Description
tuple[int, ...]

Version components as a tuple of integers.

write_file(path, content, backup_path=None)

Write data to file.

Overwrite file content only if changed. Create all necessary directories.

Parameters:

Name Type Description Default
path Path

File path.

required
content str | bytes

File content.

required
backup_path Path | None

Path to store a copy of file when overwritten.

None

Returns:

Name Type Description
True bool

if file was written.

False bool

if file wasn't changed.