How to use decorator_func method in locust

Best Python code snippet using locust

tf_decorator.py

Source:tf_decorator.py Github

copy

Full Screen

1# Copyright 2017 The TensorFlow Authors. All Rights Reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14# ==============================================================================15"""Base TFDecorator class and utility functions for working with decorators.16There are two ways to create decorators that TensorFlow can introspect into.17This is important for documentation generation purposes, so that function18signatures aren't obscured by the (*args, **kwds) signature that decorators19often provide.201. Call `tf_decorator.make_decorator` on your wrapper function. If your21decorator is stateless, or can capture all of the variables it needs to work22with through lexical closure, this is the simplest option. Create your wrapper23function as usual, but instead of returning it, return24`tf_decorator.make_decorator(your_wrapper)`. This will attach some decorator25introspection metadata onto your wrapper and return it.26Example:27 def print_hello_before_calling(target):28 def wrapper(*args, **kwargs):29 print('hello')30 return target(*args, **kwargs)31 return tf_decorator.make_decorator(wrapper)322. Derive from TFDecorator. If your decorator needs to be stateful, you can33implement it in terms of a TFDecorator. Store whatever state you need in your34derived class, and implement the `__call__` method to do your work before35calling into your target. You can retrieve the target via36`super(MyDecoratorClass, self).decorated_target`, and call it with whatever37parameters it needs.38Example:39 class CallCounter(tf_decorator.TFDecorator):40 def __init__(self, target):41 super(CallCounter, self).__init__('count_calls', target)42 self.call_count = 043 def __call__(self, *args, **kwargs):44 self.call_count += 145 return super(CallCounter, self).decorated_target(*args, **kwargs)46 def count_calls(target):47 return CallCounter(target)48"""49from __future__ import absolute_import50from __future__ import division51from __future__ import print_function52import functools as _functools53import inspect as _inspect54def make_decorator(target,55 decorator_func,56 decorator_name=None,57 decorator_doc='',58 decorator_argspec=None):59 """Make a decorator from a wrapper and a target.60 Args:61 target: The final callable to be wrapped.62 decorator_func: The wrapper function.63 decorator_name: The name of the decorator. If `None`, the name of the64 function calling make_decorator.65 decorator_doc: Documentation specific to this application of66 `decorator_func` to `target`.67 decorator_argspec: The new callable signature of this decorator.68 Returns:69 The `decorator_func` argument with new metadata attached.70 """71 if decorator_name is None:72 decorator_name = _inspect.stack()[1][3] # Caller's name.73 decorator = TFDecorator(decorator_name, target, decorator_doc,74 decorator_argspec)75 setattr(decorator_func, '_tf_decorator', decorator)76 decorator_func.__name__ = target.__name__77 decorator_func.__module__ = target.__module__78 decorator_func.__doc__ = decorator.__doc__79 decorator_func.__wrapped__ = target80 return decorator_func81def unwrap(maybe_tf_decorator):82 """Unwraps an object into a list of TFDecorators and a final target.83 Args:84 maybe_tf_decorator: Any callable object.85 Returns:86 A tuple whose first element is an list of TFDecorator-derived objects that87 were applied to the final callable target, and whose second element is the88 final undecorated callable target. If the `maybe_tf_decorator` parameter is89 not decorated by any TFDecorators, the first tuple element will be an empty90 list. The `TFDecorator` list is ordered from outermost to innermost91 decorators.92 """93 decorators = []94 cur = maybe_tf_decorator95 while True:96 if isinstance(cur, TFDecorator):97 decorators.append(cur)98 elif hasattr(cur, '_tf_decorator'):99 decorators.append(getattr(cur, '_tf_decorator'))100 else:101 break102 cur = decorators[-1].decorated_target103 return decorators, cur104class TFDecorator(object):105 """Base class for all TensorFlow decorators.106 TFDecorator captures and exposes the wrapped target, and provides details107 about the current decorator.108 """109 def __init__(self,110 decorator_name,111 target,112 decorator_doc='',113 decorator_argspec=None):114 self._decorated_target = target115 self._decorator_name = decorator_name116 self._decorator_doc = decorator_doc117 self._decorator_argspec = decorator_argspec118 self.__name__ = target.__name__119 if self._decorator_doc:120 self.__doc__ = self._decorator_doc121 elif target.__doc__:122 self.__doc__ = target.__doc__123 else:124 self.__doc__ = ''125 def __get__(self, obj, objtype):126 return _functools.partial(self.__call__, obj)127 def __call__(self, *args, **kwargs):128 return self._decorated_target(*args, **kwargs)129 @property130 def decorated_target(self):131 return self._decorated_target132 @property133 def decorator_name(self):134 return self._decorator_name135 @property136 def decorator_doc(self):137 return self._decorator_doc138 @property139 def decorator_argspec(self):...

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 locust 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