How to use on_not_implemented_error method in localstack

Best Python code snippet using localstack_python

serialization.py

Source:serialization.py Github

copy

Full Screen

1import json2import warnings3try:4 import yaml5 yaml_available = True6except ImportError:7 yaml_available = False8from albumentations import __version__9__all__ = ['to_dict', 'from_dict', 'save', 'load']10SERIALIZABLE_REGISTRY = {}11class SerializableMeta(type):12 """13 A metaclass that is used to register classes in `SERIALIZABLE_REGISTRY` so they can be found later14 while deserializing transformation pipeline using classes full names.15 """16 def __new__(meta, name, bases, class_dict):17 cls = type.__new__(meta, name, bases, class_dict)18 SERIALIZABLE_REGISTRY[cls.get_class_fullname()] = cls19 return cls20def to_dict(transform, on_not_implemented_error='raise'):21 """22 Take a transform pipeline and convert it to a serializable representation that uses only standard23 python data types: dictionaries, lists, strings, integers, and floats.24 Args:25 transform (object): A transform that should be serialized. If the transform doesn't implement the `to_dict`26 method and `on_not_implemented_error` equals to 'raise' then `NotImplementedError` is raised.27 If `on_not_implemented_error` equals to 'warn' then `NotImplementedError` will be ignored28 but no transform parameters will be serialized.29 """30 if on_not_implemented_error not in {'raise', 'warn'}:31 raise ValueError(32 "Unknown on_not_implemented_error value: {}. Supported values are: 'raise' and 'warn'".format(33 on_not_implemented_error34 )35 )36 try:37 transform_dict = transform.to_dict()38 except NotImplementedError as e:39 if on_not_implemented_error == 'raise':40 raise e41 else:42 transform_dict = {}43 warnings.warn(44 "Got NotImplementedError while trying to serialize {obj}. Object arguments are not preserved. "45 "Implement either '{cls_name}.get_transform_init_args_names' or '{cls_name}.get_transform_init_args' "46 "method to make the transform serializable".format(47 obj=transform,48 cls_name=transform.__class__.__name__,49 )50 )51 return {52 '__version__': __version__,53 'transform': transform_dict,54 }55def from_dict(transform_dict):56 """57 Args:58 transform (dict): A dictionary with serialized transform pipeline.59 """60 transform = transform_dict['transform']61 name = transform['__class_fullname__']62 args = {k: v for k, v in transform.items() if k != '__class_fullname__'}63 cls = SERIALIZABLE_REGISTRY[name]64 if 'transforms' in args:65 args['transforms'] = [from_dict({'transform': t}) for t in args['transforms']]66 return cls(**args)67def check_data_format(data_format):68 if data_format not in {'json', 'yaml'}:69 raise ValueError(70 "Unknown data_format {}. Supported formats are: 'json' and 'yaml'".format(data_format)71 )72def save(transform, filepath, data_format='json', on_not_implemented_error='raise'):73 """74 Take a transform pipeline, serialize it and save a serialized version to a file75 using either json or yaml format.76 Args:77 transform (obj): Transform to serialize.78 filepath (str): Filepath to write to.79 data_format (str): Serialization format. Should be either `json` or 'yaml'.80 on_not_implemented_error (str): Parameter that describes what to do if a transform doesn't implement81 the `to_dict` method. If 'raise' then `NotImplementedError` is raised, if `warn` then the exception will be82 ignored and no transform arguments will be saved.83 """84 check_data_format(data_format)85 transform_dict = to_dict(transform, on_not_implemented_error=on_not_implemented_error)86 dump_fn = json.dump if data_format == 'json' else yaml.safe_dump87 with open(filepath, 'w') as f:88 dump_fn(transform_dict, f)89def load(filepath, data_format='json'):90 """91 Load a serialized pipeline from a json or yaml file and construct a transform pipeline.92 Args:93 transform (obj): Transform to serialize.94 filepath (str): Filepath to read from.95 data_format (str): Serialization format. Should be either `json` or 'yaml'.96 """97 check_data_format(data_format)98 load_fn = json.load if data_format == 'json' else yaml.safe_load99 with open(filepath) as f:100 transform_dict = load_fn(f)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful