def coerce_type(cls, obj, typedef=None, key_order=None, **kwargs): r"""Coerce objects of specific types to match the data type. Args: obj (object): Object to be coerced. typedef (dict, optional): Type defintion that object should be coerced to. Defaults to None. key_order (list, optional): Order that keys from a dictionary should be used to compose an array. Defaults to None. **kwargs: Additional keyword arguments are metadata entries that may aid in coercing the type. Returns: object: Coerced object. Raises: RuntimeError: If obj is a dictionary, but key_order is not provided. """ from yggdrasil.serialize import pandas2list, numpy2list, dict2list if isinstance(obj, pd.DataFrame): obj = pandas2list(obj) elif isinstance(obj, np.ndarray) and (len(obj.dtype) > 0): obj = numpy2list(obj) elif isinstance(obj, dict): if (key_order is not None) or (len(obj) == 1): obj = dict2list(obj, order=key_order) else: obj = [obj] elif isinstance(typedef, dict) and (len(typedef.get('items', [])) == 1): typedef_validated = kwargs.get('typedef_validated', False) if cls.check_decoded([obj], typedef, typedef_validated=typedef_validated): obj = [obj] return obj
def coerce_type(cls, obj, typedef=None, key_order=None, dont_wrap_single=False, **kwargs): r"""Coerce objects of specific types to match the data type. Args: obj (object): Object to be coerced. typedef (dict, optional): Type defintion that object should be coerced to. Defaults to None. key_order (list, optional): Order that keys from a dictionary should be used to compose an array. Defaults to None. dont_wrap_single (bool, optional): If True, single element data types will not attempt to wrap input object in additional list. Defaults to False. **kwargs: Additional keyword arguments are metadata entries that may aid in coercing the type. Returns: object: Coerced object. Raises: RuntimeError: If obj is a dictionary, but key_order is not provided. """ from yggdrasil.serialize import pandas2list, numpy2list, dict2list if isinstance(obj, pd.DataFrame): obj = pandas2list(obj) elif isinstance(obj, np.ndarray) and (len(obj.dtype) == 0): obj = [obj] elif isinstance(obj, np.ndarray) and (len(obj.dtype) > 0): obj = numpy2list(obj) elif isinstance(obj, dict): if (key_order is not None) or (len(obj) == 1): obj = dict2list(obj, order=key_order) elif (isinstance(typedef, dict) and isinstance(typedef.get('items', None), list) and all([('title' in x) for x in typedef['items']])): key_order = [x['title'] for x in typedef['items']] obj = dict2list(obj, order=key_order) else: obj = [obj] elif ((isinstance(typedef, dict) and (not dont_wrap_single) and (len(typedef.get('items', [])) == 1))): typedef_validated = kwargs.get('typedef_validated', False) try_obj = cls.coerce_type([obj], typedef=typedef, key_order=key_order, dont_wrap_single=True, **kwargs) if cls.check_decoded(try_obj, typedef, typedef_validated=typedef_validated): obj = try_obj return super(JSONArrayMetaschemaType, cls).coerce_type(obj, typedef=typedef, **kwargs)
def pandas_recv_converter(obj): r"""Performs conversion to a limited set of objects from a Pandas data frame for receiving from a file via PandasFileComm. Currently supports converting to lists/tuples of numpy arrays. Args: obj (pandas.DataFrame): Data frame to convert. Returns: list: pandas.DataFrame: Converted data frame (or unmodified input if conversion could not be completed. """ return serialize.pandas2list(obj)
def get_testing_options(cls, as_frames=False, no_names=False): r"""Method to return a dictionary of testing options for this class. Args: as_frames (bool, optional): If True, the test objects will be Pandas data frames. Defaults to False. no_names (bool, optional): If True, an example is returned where the names are not provided to the deserializer. Defaults to False. Returns: dict: Dictionary of variables to use for testing. Key/value pairs: kwargs (dict): Keyword arguments for comms tested with the provided content. send (list): List of objects to send to test file. recv (list): List of objects that will be received from a test file that was sent the messages in 'send'. contents (bytes): Bytes contents of test file created by sending the messages in 'send'. """ out_seri = PandasSerialize.get_testing_options(no_names=no_names) out = { 'kwargs': out_seri['kwargs'], 'send': out_seri['objects'], 'recv': [pd.concat(out_seri['objects'], ignore_index=True)], 'dict': serialize.pandas2dict(out_seri['objects'][0]), 'contents': out_seri['contents'], 'msg_array': serialize.pandas2numpy(out_seri['objects'][0]) } if not as_frames: out['recv'] = [serialize.pandas2list(x) for x in out['recv']] out['send'] = [serialize.pandas2list(x) for x in out['send']] out['msg'] = out['send'][0] for k in ['format_str', 'as_array']: if k in out['kwargs']: del out['kwargs'][k] return out