Utils module#
Utility helpers used across the pyTermite package.
Small convenience functions for loading serial mappings, serial -> URL conversion, simple dict utilities and serializing complex objects to plain Python data types suitable for JSON output.
- pytermite.utils.create_base_url(serial_number: str) str[source]#
Create the camera REST base URL from a serial number.
- Parameters:
serial_number (str) – The camera serial number used to derive the REST API base URL.
- Returns:
Fully qualified base URL for the camera’s REST API.
- Return type:
str
- pytermite.utils.load_serial_numbers_from_json(filepath: Path | str) dict[str, str][source]#
Load a JSON file containing a mapping of camera names to serial numbers.
- Parameters:
filepath (pathlib.Path | str) – Path to a JSON file containing a mapping of camera name -> serial string.
- Returns:
Parsed mapping from the JSON file.
- Return type:
dict[str, str]
- pytermite.utils.reverse_dict(d: dict) dict[source]#
Return a dict with keys and values swapped.
- Parameters:
d (dict) – Mapping to reverse.
- Returns:
Reversed mapping where original values become keys.
- Return type:
dict
- pytermite.utils.serialize_dict(d: dict) dict[source]#
Serialize complex objects in a dict to JSON-friendly Python types.
- Parameters:
d (dict) – Dictionary potentially containing dataclasses, enums or objects.
- Returns:
Dictionary where values have been converted to primitives, dicts or strings suitable for JSON serialization.
- Return type:
dict
- pytermite.utils.write_json_to_file(data: dict, filepath: Path | str) None[source]#
Write a mapping to a file as pretty-printed JSON, creating parents.
- Parameters:
data (dict) – Mapping to write as JSON.
filepath (pathlib.Path | str) – Destination file path. If extension is not .json it will be added.