Utilities

Here are a few utilities used by PHUB. Maybe they can be helpful to you too.

phub.utils.concat(*args) str

Concatenate multiple URL or file path parts.

Parameters:
*args

A variable number of arguments, each can be a str or a pathlib.Path object.

Returns:

The concatenated path as a string.

Return type:

str

phub.utils.urlify(data: dict) str

Convert a dictionary to string arguments.

Parameters:
data : dict

Parameters values.

Returns:

A raw URL arguments string.

Return type:

str

phub.utils.closest(numbers: list[int], value: int) int

Find the closest value in a list to the given value.

Parameters:
numbers : list[int]

List of possible values.

value : int

Target value.

Returns:

The closest value in the list to the target value.

Return type:

int

phub.utils.serialize(object_: object, recursive: bool = False) object

Simple serializer for PHUB objects.

Parameters:
object : Any

The object to serialize.

recursive : bool

Whether to allow serializing PHUB objects inside the object.

Returns:

A JSON serializable object.

Return type:

Any

phub.utils.dictify(object_: object, keys: str | list[str], all_: list[str], recursive: bool) dict

Dictify an object.

Parameters:
object : Any

The object to serialize.

keys : list[str]

A list of keys to dictify.

all : list[str]

The total list of serializeable object keys.

recursive : bool

Whether to allow serializing PHUB objects inside the object.

Returns:

The object dictified.

Return type:

dict

phub.utils.suppress(gen: ~typing.Iterable, errs: Exception | tuple[Exception] = <class 'phub.errors.VideoError'>) Iterator

Set up a generator to bypass items that throw errors.

Parameters:
gen : Iterable

The iterable to suppress.

errs : Exception

The errors that fall under the suppression rule.

Returns

Iterator: The result iterator.

phub.utils.least_factors(n: int) int

Find the least factor of a given integer.

Parameters:
n : int

Input integer.

Returns:

The least factor of n.

Return type:

int

phub.utils.head(client: object, url: str) str | bool

Performs a silent HEAD request to check if a page is available without fetching its content.

Parameters:
client : Client

A client containing an initialised session.

url : str

The url of the request.

Returns:

The redirect URL if success, False otherwise.

Return type:

str | bool