ResultData class
Please note, for usage illustrations see ResultData class.
API ResultData.
- class bh_apistatus.result_data.ResultData(**kwargs)
Bases:
objectProvides ability to dynamically add and access data as either dictionaries or lists during processing.
Final data can be serialised as a dictionary, and converted into JSON and returned to (web) clients.
- Parameters:
**kwargs – See below
- Keyword Arguments:
data (
Union[dict, list]) – a dictionary or a list of dictionaries to be added.data_name (
str) – associated name fordataabove.
- add(data: dict | list, name=None) None
Add a dictionary or a list of dictionaries.
- Parameters:
data – the dictionary or the list of dictionaries to be added.
name (str) – the associated name for
data, which become the instance attribute name.
- Returns:
None.
- as_dict() dict
Return all data as a dictionary whose root key is
data.I.e.:
data = ResultData(data={'first_name': 'Be Hai','surname': 'Nguyen',}) data_dict = data.as_dict() # data_dict = {'data': {'first_name': 'Be Hai', 'surname': 'Nguyen'}} assert data_dict['data']['first_name'] == 'Be Hai' assert data_dict['data']['surname'] == 'Nguyen'
- Returns:
a dictionary representation of all internal data. When there is no data, an effectively empty dictionary is returned
{'data': {}}.- Return type:
dict.
- serialise_data() dict | list
All data consolidated as a dictionary whose root key is
data. Return the value (object) of root keydata.I.e.:
data = ResultData(data={'first_name': 'Be Hai','surname': 'Nguyen',}) data_dict = data.serialise_data() # data_dict = {'first_name': 'Be Hai', 'surname': 'Nguyen'} assert data_dict['first_name'] == 'Be Hai' assert data_dict['surname'] == 'Nguyen'
Or as a list of dictionaries (one in this example):
data = ResultData(data=[{'first_name': 'Be Hai','surname': 'Nguyen',}]) data_dict = data.serialise_data() # data_dict = [{'first_name': 'Be Hai', 'surname': 'Nguyen'}] assert data_dict[0]['first_name'] == 'Be Hai' assert data_dict[0]['surname'] == 'Nguyen'
Call method
as_dict()to do most of the work. The returned value can be added as data to other instances ofResultData.When there is no data added prior, it just returns an empty dictionary
{}.- Returns:
a dictionary or a list of dictionaries representation of all internal data.
- Return type:
Union[dict, list].