Devices¶
Devices(baze)
¶
Class used for handling Devices.
Source code in echo_baze/baze_root.py
def __init__(self, baze: e_bz.Baze) -> None:
"""Base class that all subclasses should inherit from.
Parameters
----------
baze : Baze
Top level object carrying all functionality and the connection handler.
"""
# check inputs
if not isinstance(baze, e_bz.Baze):
raise ValueError(f"baze must be of type Baze, not {type(baze)}")
self.baze: e_bz.Baze = baze
get(output_type='dict')
¶
Gets all Devices with detailed information.
The most useful keys/columns returned are:
- acronym
- nodId
- protocol
- description
Parameters:
-
(output_type¶Literal['dict', 'DataFrame'], default:'dict') –Output type of the data. Can be one of ["dict", "DataFrame"] By default "dict"
Returns:
-
dict[str, dict[str, Any]]–In case output_type == "dict" it will return a dict with the following format: {name: {attribute: value, ...}, ...}
-
DataFrame–In case output_type == "DataFrame" it will return a DataFrame with the following format: index = name, columns = [attribute, ...]
Source code in echo_baze/devices.py
@validate_call
def get(
self,
output_type: Literal["dict", "DataFrame"] = "dict",
) -> dict[str, dict[str, Any]] | DataFrame:
"""Gets all Devices with detailed information.
The most useful keys/columns returned are:
- acronym
- nodId
- protocol
- description
Parameters
----------
output_type : Literal["dict", "DataFrame"], optional
Output type of the data. Can be one of ["dict", "DataFrame"]
By default "dict"
Returns
-------
dict[str, dict[str, Any]]
In case output_type == "dict" it will return a dict with the following format: {name: {attribute: value, ...}, ...}
DataFrame
In case output_type == "DataFrame" it will return a DataFrame with the following format: index = name, columns = [attribute, ...]
"""
# getting the data
endpoint = "configuration/nodes/devices"
result = self.baze.conn.get(endpoint)
self._handle_http_errors(result)
# converting to dict
result: list[dict[str, Any]] = result.json()["nodes"]
# converting to a better format
results = {}
for node_values in result.values():
for device in node_values["devices"]:
results[device["name"]] = device
# converting to desired format
match output_type:
case "dict":
return results
case "DataFrame":
df = DataFrame(results).T
return df
case _:
raise ValueError(f"Invalid output_type: {output_type}")
get_ids()
¶
Gets a dictionary with all device names and ids.
Returns:
-
dict[str, int]:–Dictionary with all Devices in the format {name: id, ...}
Source code in echo_baze/devices.py
def get_ids(self) -> dict[str, int]:
"""Gets a dictionary with all device names and ids.
Returns
-------
dict[str, int]:
Dictionary with all Devices in the format {name: id, ...}
"""
endpoint = "configuration/nodes/devices"
# getting the data
result = self.baze.conn.get(endpoint)
self._handle_http_errors(result)
# converting to dict
results = {}
for node_values in result.json()["nodes"].values():
results |= {device["name"]: device["id"] for device in node_values["devices"]}
return results