from ..exceptions import LiveViewException
from ..models.liveview_tuple import LvTuple
from ..util import convert_tuple_with_schema
[docs]class SnapshotResult:
"""The class is used to encapsulate the results of snapshot queries.
Instantiated through LiveViewClient#snapshot_query().
Parameters:
- snapshot : The raw JSON of a snapshot query, as a list.
Attributes:
- schema : Dict containing the Schema for the table queried.
- keys : List containing corresponding Key(s) for the tuples in 'data'
- cols : List of the names of columns in the table
- snapshot_data : List of dicts, serving as the snapshot data.
- size : Number of tuples in the snapshot.
"""
def __init__(self, snapshot) -> None:
self.data = []
self.keys = []
_header = snapshot.pop(0)
_footer = snapshot.pop()
self.schema = _header.get('data').get('schema')
self.cols = list(self.schema.keys())
for s in snapshot:
self.data.append(LvTuple(s.get('data'), self.schema))
self.keys.append(s.get('key'))
self.size = len(self.data)
[docs] def to_dict(self) -> dict:
"""Returns the data of the snapshot as a dictionary.
Returns:
- A Dict, with keys 'schema', 'keys', and 'data'
"""
output = {
'schema': self.schema,
'keys': self.keys,
'data': list(map(lambda t: convert_tuple_with_schema(t, self.schema), self.data)),
}
return output
[docs] def get_column(self, col_name) -> list:
"""Returns a list containing every entry for a given column.
Parameters:
- col_name : The name of the column to get data from.
Returns:
- A list of values.
"""
if col_name not in self.cols:
raise LiveViewException(f'{col_name} not found. available columns are: {self.cols}')
return [data_dict.get(col_name) for data_dict in self.data]