How to use rename_attributes method in localstack

Best Python code snippet using localstack_python

replace_func.py

Source:replace_func.py Github

copy

Full Screen

1import inspect2import chainer3class WrappedFunctionNode(chainer.FunctionNode):4 """Wrap the target function and operate as ``FunctionNode``5 Arguments:6 name (str): name of the function node7 func (func): the target function8 args (list): args for the function9 kwargs (dict): kwargs for the function10 arg_vars (list): list of `chainer.Variable`s in `args` and `kwargs`11 attributes (list): parameters to be set node's attributes12 """13 def __init__(self, name, func, args, kwargs, arg_vars, attributes=None):14 self.custom_function_node_name = name15 self.func = func16 self.args = args17 self.kwargs = kwargs18 self.arg_vars = arg_vars19 self.internal_results = None20 if attributes is not None:21 for k, v in attributes.items():22 setattr(self, k, v)23 def forward(self, xs):24 assert len(xs) == len(self.arg_vars)25 self.xs = xs26 results = self.func(*self.args, **self.kwargs)27 self.skeleton, flattened_results = self._flatten_return_value(results)28 dummy_results = tuple(_unwrap_var(ret) for ret in flattened_results)29 if all([_is_var(ret) for ret in flattened_results]):30 self.internal_results = flattened_results31 if not chainer.is_arrays_compatible(dummy_results):32 raise ValueError(33 'returned values from the function wrapped by \'as_funcnode\' '34 'must consist only array, function name: {}'.format(35 self.custom_function_node_name))36 return dummy_results37 def backward(self, target_input_indexes, grad_outputs):38 if self.internal_results is None:39 raise ValueError(40 'the target function does not support backward, propagation '41 'is failed')42 grad_inputs = chainer.grad(self.internal_results, self.arg_vars,43 grad_outputs=grad_outputs)44 assert len(self.arg_vars) == len(grad_inputs)45 return tuple(grad_input if i in target_input_indexes else None46 for i, grad_input in enumerate(grad_inputs))47 def _flatten_return_value(self, x):48 outputs = []49 def skeletonize(r):50 if isinstance(r, tuple):51 return tuple(skeletonize(e) for e in r)52 elif isinstance(r, list):53 return [skeletonize(e) for e in r]54 elif isinstance(r, dict):55 return {k: skeletonize(v) for k, v in r.items()}56 else:57 index = len(outputs)58 outputs.append(r)59 return index60 skeleton = skeletonize(x)61 return skeleton, outputs62 def reconstruct_return_value(self, outputs):63 def f(skeleton):64 if isinstance(skeleton, tuple):65 return tuple(f(e) for e in skeleton)66 elif isinstance(skeleton, list):67 return [f(e) for e in skeleton]68 elif isinstance(skeleton, dict):69 return {k: f(v) for k, v in skeleton.items()}70 else:71 return outputs[skeleton]72 return f(self.skeleton)73def fake_as_funcnode(alt_func, name, rename_attributes=None,74 experimental_warning=True):75 """The target function fakes FunctionNode76 The target function is replaced to the alternative function to connect77 variable node by acting function node. ``alt_func`` must satisfy the78 following restrictions.79 1. Inputs includes one or more ``chainer.Variable`` to trace variables.80 2. Output consists nothing but ``ndarray`` or ``chainer.Variable``81 Even if ``alt_func`` returns ``ndarray``, the value forced to be converted82 to ``chainer.Variable``. A caller of the target function have to care83 both cases, returning ``ndarray`` and ``chainer.Variable``.84 When ``alt_func`` returns ``list`` of variable, the wrapped function will85 also returns multiple variables as ``tuple``. However ``dict`` cannot86 be return, the wrapped function breaks down the returned values as87 ``tuple`` of values, keys will be ignored.88 Arguments of ``alt_func`` except for ``chainer.Variable`` are set as89 function attributes. Attribute names are set ``argN`` (N is index90 number) or keyword on default.91 Example:92 >>> def func(x, a, b, c=1, d=2): pass93 >>> # x is variable94 >>> func = onnx_chainer.replace_func.fake_as_funcnode(95 ... func, 'CustomNode',96 ... rename_attributes=[(1, 'value'), ('c', 'y')])97 Then ``func`` will be operated as a function node named "CustomNode", and98 ``'value'``, ``'b'``, ``'y'``, ``'d'`` are set as function's attributes.99 See tests/test_replace_func.py more details.100 Args:101 alt_func (func): actual called function. There are some constrains, see102 the above documentation.103 name (str): function name. This name is used for what ONNX operator104 to be assigned.105 rename_attributes (list or tuple): rename attribute name, set list106 of ``tuple(index_of_args, new_name)`` or107 ``tuple(kwargs_name, new_name)``108 experimental_warning: this function is experimental utility, if set109 ``False``, run without experimental warning.110 Returns:111 func: wrapped function, called on exporting.112 """113 def _wrapper(*args, **kwargs):114 inputs = []115 attributes = {}116 rename_attr_dict = {}117 if rename_attributes is not None:118 rename_attr_dict = {attr[0]: attr[1] for attr in rename_attributes}119 # resolve default value for kwargs120 arg_spec = inspect.signature(alt_func)121 bound = arg_spec.bind(*args, **kwargs)122 bound.apply_defaults()123 # default values are set on `bound.arguments`, but cannot get them124 # from `bound.kwargs`125 for i, (k, v) in enumerate(bound.arguments.items()):126 if i < len(args):127 continue128 kwargs[k] = v129 def set_attr(key, value):130 default_name = key if isinstance(key, str) else 'arg{}'.format(key)131 attributes[rename_attr_dict.get(key, default_name)] = value132 def expand_args(args_iter):133 for i, a in args_iter:134 if _is_var(a):135 inputs.append(a)136 elif isinstance(a, (tuple, list)):137 # all elements are variable -> add flatten them to inputs138 # all elements are not variable -> add them to attributes139 # mixed variable and other type value -> error140 flatten_arg = _flatten(a)141 var_or_not = map(_is_var, flatten_arg)142 if all(var_or_not):143 inputs.extend(flatten_arg)144 elif not any(var_or_not):145 set_attr(i, a)146 else:147 raise ValueError(148 'arguments mixed variable and other type are not '149 'supported')150 else:151 set_attr(i, a)152 expand_args(enumerate(args))153 expand_args(kwargs.items())154 if not inputs:155 raise ValueError(156 'arguments of the function wrapped by \'as_funcnode\' '157 'must include at least one chainer.Variable, function name: '158 '{}'.format(name))159 wrapped = WrappedFunctionNode(160 name, alt_func, args, kwargs, inputs, attributes=attributes)161 ret = wrapped.apply(inputs)162 return wrapped.reconstruct_return_value(ret)163 if experimental_warning:164 chainer.utils.experimental('as_funcnode')165 return _wrapper166def as_funcnode(name, rename_attributes=None):167 """The target function fakes FunctionNode168 The target function is overwrapped to connect variable node by acting169 function node. Expected to be used as decorator. More detail, see170 ``fake_as_funcnode`` documentation.171 Example:172 >>> @onnx_chainer.replace_func.as_funcnode(173 ... 'CustomNode', rename_attributes=[(1, 'value'), ('c', 'y')])174 ... def func(x, a, b, c=1, d=2): pass175 Args:176 name (str): function name. This name is used for what ONNX operator177 to be assigned.178 rename_attributes (list or tuple): rename attribute name, set list179 of ``tuple(index_of_args, new_name)`` or180 ``tuple(kwargs_name, new_name)``181 """182 def _wrapper(fn):183 return fake_as_funcnode(fn, name, rename_attributes=rename_attributes)184 return _wrapper185def _unwrap_var(var):186 return var.array if _is_var(var) else var187def _is_var(array):188 # alias for type checking189 return isinstance(array, chainer.Variable)190def _is_array(v):191 return not isinstance(v, (list, tuple))192def _flatten(xs):193 if _is_array(xs):194 return [xs]195 o = []196 for x in xs:197 if _is_array(x):198 o.append(x)199 else:200 o.extend(_flatten(x))...

Full Screen

Full Screen

main.py

Source:main.py Github

copy

Full Screen

...107 }108# add neighbourhoods layer109neighbourhoods_layer = layer_management.add_layer_as_reprojected_gpkg(project_folder, geo_layer_folder, NEIGHBOURHOODS['path'], CRS, 'reprojected/{}'.format(NEIGHBOURHOODS['identifier']))110# rename attributes111layer_management.rename_attributes(neighbourhoods_layer, NEIGHBOURHOODS['rename_attributes'])112# clean up neighbourhoods layer113layer_management.delete_all_attributes_except(NEIGHBOURHOODS['attributes_to_keep'], neighbourhoods_layer)114# for each neighbourhood find surrounding neighbourhoods115spatial_analysis.add_adjacent_features(neighbourhoods_layer, 'adjacent_neighbourhoods', 'neighbourhood_code')116# add geo coordinates to neighbourhoods layer117spatial_analysis.add_coordinates(neighbourhoods_layer)118# add municipality layer119municipality_layer = layer_management.add_layer_as_reprojected_gpkg(project_folder, geo_layer_folder, MUNICIPALITIES['path'], CRS, 'reprojected/{}'.format(MUNICIPALITIES['identifier']))120# add corresponding municipality code to each neighbourhood121spatial_analysis.add_overlapping_features(neighbourhoods_layer, municipality_layer, 'municipality_code', MUNICIPALITIES['id_attribute'], area_to_point=True, prefix = MUNICIPALITIES['code_prefix'])122spatial_analysis.add_overlapping_features(neighbourhoods_layer, municipality_layer, 'municipality_name', MUNICIPALITIES['name_attribute'], area_to_point=True)123# loop over heat source files124for NAME, SOURCE in HEAT_SOURCES.items():125 print(NAME)126 # add heat source layers127 heat_layer = layer_management.add_layer_as_reprojected_gpkg(project_folder, geo_layer_folder, SOURCE['path'], CRS, 'reprojected/{}'.format(SOURCE['identifier']))128 # rename attributes129 layer_management.rename_attributes(heat_layer, SOURCE['rename_attributes'])130 #clean up layer131 layer_management.delete_all_attributes_except(SOURCE['attributes_to_keep'], heat_layer)132 # generate unique ID for each heat source133 layer_management.add_unique_id(heat_layer, 'source_id', SOURCE['identifier'])134 # add geo coordinates135 spatial_analysis.add_coordinates(heat_layer)136 # add heat source IDs to neighbourhoods if heat source overlaps with neighbourhood137 spatial_analysis.add_overlapping_features(heat_layer, neighbourhoods_layer, 'neighbourhoods_in_range', 'neighbourhood_code')138 # export to CSV139 layer_management.export_to_csv(heat_layer, project_folder, 'input_data/', SOURCE['output_file'])140# export neighbourhoods layer to CSV...

Full Screen

Full Screen

test_ast.py

Source:test_ast.py Github

copy

Full Screen

...20 with self.assertRaises(KeyError):21 find_function(self.root, 'bad_name')22class TestAttrRename(AstTestCase):23 def test_load(self):24 root = rename_attributes(ast.parse('var = self.value1'),25 load_names={'value1': 'value2'})26 expected = ast.parse('var = self.value2')27 self.assertEqual(root, expected)28 def test_store(self):29 root = rename_attributes(ast.parse('self.var1 = value'),30 store_names={'var1': 'var2'})31 expected = ast.parse('self.var2 = value')32 self.assertEqual(root, expected)33 def test_load_and_store(self):34 root = rename_attributes(ast.parse('self.var1 = self.value1'),35 load_names={'value1': 'value2'},36 store_names={'var1': 'var2'})37 expected = ast.parse('self.var2 = self.value2')38 self.assertEqual(root, expected)39 def test_load_and_store_same_key(self):40 root = rename_attributes(ast.parse('self.foo1 = self.foo1'),41 load_names={'foo1': 'load'},42 store_names={'foo1': 'store'})43 expected = ast.parse('self.store = self.load')44 self.assertEqual(root, expected)45 def test_ignore_function_call(self):46 root = rename_attributes(ast.parse('self.myfunc(1)'),47 load_names={'myfunc': 'load'},48 store_names={'myfunc': 'store'})49 expected = ast.parse('self.myfunc(1)')50 self.assertEqual(root, expected)51class TestRemoveFunctionParameters(AstTestCase):52 def setUp(self):53 self.root = parse_source(SimpleClass)54 def test_remove_all_params(self):55 actual = find_function(self.root, 'method_with_params')56 remove_function_parameters(actual)57 root = ast.parse('def method_with_params(): return self, thing')58 expected = find_function(root, 'method_with_params')...

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