How to use add_wrapper method in toolium

Best Python code snippet using toolium_python

proxy-benchmarks.py

Source:proxy-benchmarks.py Github

copy

Full Screen

...15 strategy = "generated methods (getattr + closure)"16 works_for = "methods"17 def __init__(self, wrapped):18 self._wrapped = wrapped19def add_wrapper(cls, method):20 def wrapper(self, *args, **kwargs):21 return getattr(self._wrapped, method)(*args, **kwargs)22 setattr(cls, method, wrapper)23for method in methods:24 add_wrapper(Proxy2, method)25################################################################26class Proxy3:27 strategy = "generated methods (exec)"28 works_for = "methods"29 def __init__(self, wrapped):30 self._wrapped = wrapped31def add_wrapper(cls, method):32 code = textwrap.dedent("""33 def wrapper(self, *args, **kwargs):34 return self._wrapped.{}(*args, **kwargs)35 """.format(method))36 ns = {}37 exec(code, ns)38 setattr(cls, method, ns["wrapper"])39for method in methods:40 add_wrapper(Proxy3, method)41################################################################42class Proxy4:43 strategy = "generated properties (getattr + closure)"44 works_for = "any attr"45 def __init__(self, wrapped):46 self._wrapped = wrapped47def add_wrapper(cls, attr):48 def getter(self):49 return getattr(self._wrapped, attr)50 def setter(self, newval):51 setattr(self._wrapped, attr, newval)52 def deleter(self):53 delattr(self._wrapped, attr)54 setattr(cls, attr, property(getter, setter, deleter))55for method in methods:56 add_wrapper(Proxy4, method)57################################################################58class Proxy5:59 strategy = "generated properties (exec)"60 works_for = "any attr"61 def __init__(self, wrapped):62 self._wrapped = wrapped63def add_wrapper(cls, attr):64 code = textwrap.dedent("""65 def getter(self):66 return self._wrapped.{attr}67 def setter(self, newval):68 self._wrapped.{attr} = newval69 def deleter(self):70 del self._wrapped.{attr}71 """.format(attr=attr))72 ns = {}73 exec(code, ns)74 setattr(cls, attr, property(ns["getter"], ns["setter"], ns["deleter"]))75for method in methods:76 add_wrapper(Proxy5, method)77################################################################78# methods only79class Proxy6:80 strategy = "copy attrs from wrappee to wrapper"81 works_for = "methods + constant attrs"82 def __init__(self, wrapper):83 self._wrapper = wrapper84 for method in methods:85 setattr(self, method, getattr(self._wrapper, method))86 87################################################################88classes = [Proxy1, Proxy2, Proxy3, Proxy4, Proxy5, Proxy6]89def check(cls):90 with open("/etc/passwd") as f:...

Full Screen

Full Screen

builders.py

Source:builders.py Github

copy

Full Screen

...14 """Base class for function wrapper builders"""15 def __init__(self) -> None:16 self.wrappers: List[FnWrapper] = []17 self.id_fields: Optional[list[str]] = None18 def add_wrapper(self, wrapper: FnWrapper):19 self.wrappers.append(wrapper)20 return self21 def pop_id_fields(self, *id_fields: str, keep: bool = False):22 """(Optionally) removes fields from the input kwargs23 and reinserts them back into the output24 Additionally, `*id_fields` are appended to `self.id_fields` list for use in SQL generation25 ---26 Args:27 - `*id_fields` (str): fields to pop from the kwargs28 - keep (bool, optional): if True, `id_fields` are not removed from kwargs29 (but still remembered and reinserted into the output), False by default"""30 self.add_wrapper(PopIdFields(*id_fields, keep=keep))31 # Remember the names of the popped fields (useful for SQL generation)32 if not self.id_fields:33 self.id_fields = []34 for id_field in id_fields:35 self.id_fields.append(id_field)36 return self37 def rename_input(self, remap_fields: dict[str, str]):38 """Remaps field names in the `**kwargs` dictionary"""39 self.add_wrapper(RenameInput(remap_fields))40 return self41 def rename_output(self, remap_fields: dict[str, str]):42 """Remaps field values in dictionaries generated by the inner function"""43 self.add_wrapper(RenameOutput(remap_fields))44 return self45 def add_to_input(self, add_values: dict[str, Any]):46 """Add extra key-value pairs to input"""47 self.add_wrapper(AddToInput(add_values))48 return self49 def add_to_output(self, add_values: dict[str, Any]):50 """Add extra key-value pairs to output"""51 self.add_wrapper(AddToOutput(add_values))52 return self53 def _wrap_all(self, fn: OneToMany) -> OneToMany:54 """Apply all of the wrappers in the order they were added"""55 for wrapper in self.wrappers:56 fn = wrapper.wrap(fn)57 return fn58class GeneratorBuilder(FnBuilderBase):59 """Build a function of type `(*args) -> Generator[dict]`"""60 def __init__(self, fn: OneToMany) -> None:61 """Args:62 - fn (OneToMany): Base function around which to create wrappers"""63 super().__init__()64 self.fn = fn65 def build(self) -> OneToMany:...

Full Screen

Full Screen

test_foo.py

Source:test_foo.py Github

copy

Full Screen

...10 [1, 1., float],11 [1., 1., float]12])13def test_add_wrapper_type_results(num_1, num_2, expected_type):14 assert type(add_wrapper(num_1, num_2)) == expected_type15@pytest.mark.parametrize("num_1, num_2, expected_result", [16 [1, 2, 3],17 [0., 1, 1.],18 [0, 0., 0.],19 [-1., 2, 1.]20])21def test_add_wrapper_operation(num_1, num_2, expected_result):22 assert add_wrapper(num_1, num_2) == pytest.approx(expected_result)23@pytest.mark.parametrize("func, a, b, expected_result", [24 [add, 1, 1, 2],25 [add, -1, 1, 0],26 [multiply, 1, 2, 2],27 [multiply, 2, 3, 6],28 [multiply, -5, 5, -25]29])30def test_function_wrapper(func, a, b, expected_result):31 wrapper_result = f_wrapper(func, a, b)...

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