How to use converted method in yandex-tank

Best Python code snippet using yandex-tank

api_test.py

Source:api_test.py Github

copy

Full Screen

...396 self.assertFalse(converter_testing.is_inside_generated_code())397 converter_testing.whitelist(test_fn)398 api.converted_call(399 functools.partial(test_fn, None), (), None, options=DEFAULT_RECURSIVE)400 def test_converted_call_already_converted(self):401 def f(x):402 return x == 0403 x = api.converted_call(404 f, (constant_op.constant(0),), None, options=DEFAULT_RECURSIVE)405 self.assertTrue(self.evaluate(x))406 converted_f = api.to_graph(407 f, experimental_optional_features=converter.Feature.ALL)408 x = api.converted_call(409 converted_f, (constant_op.constant(0),),410 None,411 options=DEFAULT_RECURSIVE)412 self.assertTrue(self.evaluate(x))413 def test_converted_call_then_already_converted_dynamic(self):414 @api.convert()415 def g(x):416 if x > 0:417 return x418 else:419 return -x420 def f(g, x):421 return g(x)422 x = api.converted_call(423 f, (g, constant_op.constant(1)), None, options=DEFAULT_RECURSIVE)424 self.assertEqual(self.evaluate(x), 1)425 def test_converted_call_forced_when_explicitly_whitelisted(self):426 @api.do_not_convert()427 def f(x):428 return x + 1429 opts = converter.ConversionOptions(recursive=True, user_requested=True)430 x = api.converted_call(f, (constant_op.constant(0),), None, options=opts)431 self.assertTrue(self.evaluate(x))432 converted_f = api.to_graph(433 f, experimental_optional_features=converter.Feature.ALL)434 x = api.converted_call(converted_f, (0,), None, options=DEFAULT_RECURSIVE)435 self.assertEqual(x, 1)436 @test_util.run_deprecated_v1437 def test_converted_call_no_user_code(self):438 def f(x):439 return len(x)440 opts = converter.ConversionOptions(internal_convert_user_code=False)441 # f should not be converted, causing len to error out.442 with self.assertRaisesRegexp(Exception, 'len is not well defined'):443 api.converted_call(f, (constant_op.constant([0]),), None, options=opts)444 # len on the other hand should work fine.445 x = api.converted_call(446 len, (constant_op.constant([0]),), None, options=opts)447 # The constant has static shape so the result is a primitive not a Tensor.448 self.assertEqual(x, 1)449 def test_converted_call_no_kwargs_allowed(self):450 def f(*args):451 # Note: np.broadcast rejects any **kwargs, even *{}452 return np.broadcast(args[:1])453 opts = converter.ConversionOptions(internal_convert_user_code=False)454 self.assertIsNotNone(455 api.converted_call(f, (1, 2, 3, 4), None, options=opts))456 def test_converted_call_whitelisted_method(self):457 model = sequential.Sequential([core.Dense(2)])458 x = api.converted_call(459 model.call, (constant_op.constant([[0.0]]),), {'training': True},460 options=DEFAULT_RECURSIVE)461 self.evaluate(variables.global_variables_initializer())462 self.assertAllEqual([[0.0, 0.0]], self.evaluate(x))463 def test_converted_call_whitelisted_method_via_owner(self):464 model = sequential.Sequential([core.Dense(2)])465 x = api.converted_call(466 model.call, (constant_op.constant([[0.0]]),), {'training': True},467 options=DEFAULT_RECURSIVE)468 self.evaluate(variables.global_variables_initializer())469 self.assertAllEqual([[0.0, 0.0]], self.evaluate(x))470 def test_converted_call_numpy(self):471 x = api.converted_call(np.arange, (5,), None, options=DEFAULT_RECURSIVE)472 self.assertAllEqual(x, list(range(5)))473 def test_converted_call_tf_op_forced(self):474 # TODO(mdan): Add the missing level of support to LOGICAL_EXPRESSIONS.475 opts = converter.ConversionOptions(476 user_requested=True, optional_features=None)477 x = api.converted_call(gen_math_ops.add, (1, 1), None, options=opts)478 self.assertAllEqual(self.evaluate(x), 2)479 def test_converted_call_exec_generated_code(self):480 temp_mod = imp.new_module('test_module')481 dynamic_code = """482 def foo(x):483 return x + 1484 """485 exec(textwrap.dedent(dynamic_code), temp_mod.__dict__) # pylint:disable=exec-used486 opts = converter.ConversionOptions(optional_features=None)487 x = api.converted_call(temp_mod.foo, (1,), None, options=opts)488 self.assertAllEqual(x, 2)489 def test_converted_call_namedtuple(self):490 x = api.converted_call(491 collections.namedtuple, ('TestNamedtuple', ('a', 'b')),492 None,493 options=DEFAULT_RECURSIVE)494 self.assertTrue(inspect_utils.isnamedtuple(x))495 def test_converted_call_namedtuple_via_collections(self):496 x = api.converted_call(497 collections.namedtuple, ('TestNamedtuple', ('a', 'b')),498 None,499 options=DEFAULT_RECURSIVE)500 self.assertTrue(inspect_utils.isnamedtuple(x))501 def test_converted_call_namedtuple_subclass_bound_method(self):502 class TestClass(collections.namedtuple('TestNamedtuple', ('a', 'b'))):503 def test_method(self, x):504 while tf.reduce_sum(x) > self.a:505 x //= self.b506 return x507 obj = TestClass(5, 2)508 x = api.converted_call(509 obj.test_method, (constant_op.constant([2, 4]),),510 None,511 options=DEFAULT_RECURSIVE)512 self.assertAllEqual(self.evaluate(x), [1, 2])513 def test_converted_call_namedtuple_method(self):514 class TestClass(collections.namedtuple('TestNamedtuple', ('a', 'b'))):515 pass516 obj = TestClass(5, 2)517 # _asdict is a documented method of namedtuple.518 x = api.converted_call(obj._asdict, (), None, options=DEFAULT_RECURSIVE)519 self.assertDictEqual(x, {'a': 5, 'b': 2})520 def test_converted_call_namedtuple_subclass_unbound_method(self):521 class TestClass(collections.namedtuple('TestNamedtuple', ('a', 'b'))):522 def test_method(self, x):523 while tf.reduce_sum(x) > self.a:524 x //= self.b525 return x526 obj = TestClass(5, 2)527 x = api.converted_call(528 TestClass.test_method, (obj, constant_op.constant([2, 4])),529 None,530 options=DEFAULT_RECURSIVE)531 self.assertAllEqual(self.evaluate(x), [1, 2])532 def test_converted_call_lambda(self):533 l = lambda x: x == 0534 x = api.converted_call(535 l, (constant_op.constant(0),), None, options=DEFAULT_RECURSIVE)536 self.evaluate(variables.global_variables_initializer())537 self.assertAllEqual(True, self.evaluate(x))538 def test_converted_call_defun_object_method(self):539 # pylint:disable=method-hidden540 class TestClass(object):541 def method(self):542 return 1543 def prepare(self):544 self.method = function.defun(self.method)545 # pylint:enable=method-hidden546 tc = TestClass()547 tc.prepare()548 x = api.converted_call(tc.method, (), None, options=DEFAULT_RECURSIVE)549 self.assertAllEqual(1, self.evaluate(x))550 def test_converted_call_through_tf_dataset(self):551 def other_fn(x):552 if x > 0:553 return x554 return -x555 def f():556 return dataset_ops.Dataset.range(-3, 3).map(other_fn)557 # Dataset iteration only works inside tf.558 @def_function.function559 def graph_fn():560 ds = api.converted_call(f, (), None, options=DEFAULT_RECURSIVE)561 itr = iter(ds)562 return next(itr), next(itr), next(itr)563 self.assertAllEqual(self.evaluate(graph_fn()), (3, 2, 1))564 def test_converted_call_no_leaks_via_closure(self):565 def test_fn():566 res = TestResource()567 def f(y):568 return res.x + y569 api.converted_call(f, (1,), None, options=DEFAULT_RECURSIVE)570 self.assertNoMemoryLeaks(test_fn)571 def test_converted_call_no_leaks_via_inner_function_closure(self):572 def test_fn():573 res = TestResource()574 def f(y):575 def inner_f():576 return res.x + y577 return inner_f578 api.converted_call(f, (1,), None, options=DEFAULT_RECURSIVE)()579 self.assertNoMemoryLeaks(test_fn)580 def test_converted_call_no_caching_on_abort(self):581 def test_fn(needs_autograph):582 if needs_autograph:583 if constant_op.constant(True):584 x = constant_op.constant(1)585 else:586 x = constant_op.constant(2)587 else:588 x = 3589 return x590 def call_in_disabled_context():591 with ag_ctx.ControlStatusCtx(status=ag_ctx.Status.DISABLED):592 return api.converted_call(593 test_fn, (False,), None, options=DEFAULT_RECURSIVE)594 def call_in_default_context():595 with ag_ctx.ControlStatusCtx(status=ag_ctx.Status.ENABLED):596 return api.converted_call(597 test_fn, (True,), None, options=DEFAULT_RECURSIVE)598 # Note: this is an invariant, not a test (see above).599 assert call_in_disabled_context() == 3600 # If api.convert placed test_fn in the unconverted cache, this second601 # invocation would fail.602 self.assertEqual(self.evaluate(call_in_default_context()), 1)603 def test_converted_call_caching_of_whitelisted_bound_methods(self):604 class TestClass(object):605 def __init__(self):606 self.__private = constant_op.constant(-1)607 def test_method(self):608 return self.__private609 # TODO(mdan): Refactor to avoid this use of global state.610 cache_size_before = len(conversion._WHITELIST_CACHE)611 # First invocation with fallback on, to allow recording it into cache.612 os.environ['AUTOGRAPH_STRICT_CONVERSION'] = '0'613 tc = TestClass()614 api.converted_call(tc.test_method, (), None, options=DEFAULT_RECURSIVE)615 os.environ['AUTOGRAPH_STRICT_CONVERSION'] = '1'616 # Entry should be added to the whitelist cache.617 self.assertEqual(len(conversion._WHITELIST_CACHE), cache_size_before + 1)618 # A second invocation should go through even with fallback off.619 tc = TestClass()620 api.converted_call(tc.test_method, (), None, options=DEFAULT_RECURSIVE)621 # No new entries should appear in the whitelist cache.622 self.assertEqual(len(conversion._WHITELIST_CACHE), cache_size_before + 1)623 def test_context_tracking_direct_calls(self):624 @api.do_not_convert()625 def unconverted_fn():626 self.assertEqual(ag_ctx.control_status_ctx().status,627 ag_ctx.Status.DISABLED)628 @api.convert()629 def converted_fn():630 self.assertEqual(ag_ctx.control_status_ctx().status,631 ag_ctx.Status.ENABLED)632 unconverted_fn()633 self.assertEqual(ag_ctx.control_status_ctx().status,634 ag_ctx.Status.ENABLED)635 self.assertEqual(ag_ctx.control_status_ctx().status,636 ag_ctx.Status.UNSPECIFIED)637 converted_fn()638 self.assertEqual(ag_ctx.control_status_ctx().status,639 ag_ctx.Status.UNSPECIFIED)640 @api.call_with_unspecified_conversion_status641 def unspecified_fn():642 self.assertEqual(ag_ctx.control_status_ctx().status,643 ag_ctx.Status.UNSPECIFIED)644 unspecified_fn()645 def test_to_graph_basic(self):646 def test_fn(x, s):647 while tf.reduce_sum(x) > s:648 x //= 2649 return x650 compiled_fn = api.to_graph(test_fn)651 with tf.Graph().as_default():652 x = compiled_fn(constant_op.constant((4, 8)), 4)653 self.assertAllEqual(self.evaluate(x), (1, 2))654 @test_util.run_deprecated_v1655 def test_to_graph_with_defaults(self):656 foo = 4657 def test_fn(x, s=foo):658 while tf.reduce_sum(x) > s:659 x //= 2660 return x661 compiled_fn = api.to_graph(test_fn)662 with self.cached_session() as sess:663 x = compiled_fn(constant_op.constant([4, 8]))664 self.assertListEqual([1, 2], self.evaluate(x).tolist())665 def test_to_graph_with_globals(self):666 def test_fn(x):667 global global_n668 global_n = x + global_n669 return global_n670 converted_fn = api.to_graph(test_fn)671 prev_val = global_n672 converted_fn(10)673 self.assertGreater(global_n, prev_val)674 def test_to_graph_with_kwargs_clashing_converted_call(self):675 def called_fn(**kwargs):676 return kwargs['f'] + kwargs['owner']677 def test_fn():678 # These arg names intentionally match converted_call's679 return called_fn(f=1, owner=2)680 compiled_fn = api.to_graph(test_fn)681 self.assertEqual(compiled_fn(), 3)682 def test_to_graph_with_kwargs_clashing_unconverted_call(self):683 @api.do_not_convert684 def called_fn(**kwargs):685 return kwargs['f'] + kwargs['owner']686 def test_fn():687 # These arg names intentionally match _call_unconverted's688 return called_fn(f=1, owner=2)689 compiled_fn = api.to_graph(test_fn)690 self.assertEqual(compiled_fn(), 3)691 def test_to_graph_caching(self):692 def test_fn(x):693 if x > 0:694 return x695 else:696 return -x697 converted_functions = tuple(api.to_graph(test_fn) for _ in (-1, 0, 1))698 # All outputs are from the same module. We can't use __module__ because699 # that's reset when we instantiate the function (see conversion.py).700 # TODO(mdan): Can and should we overwrite __module__ instead?701 module_names = frozenset(f.ag_module for f in converted_functions)702 self.assertEqual(len(module_names), 1)703 self.assertNotIn('__main__', module_names)704 self.assertEqual(len(frozenset(id(f) for f in converted_functions)), 3)705 def test_to_graph_caching_different_options(self):706 def called_fn():707 pass708 def test_fn():709 return called_fn()710 converted_recursive = api.to_graph(test_fn, recursive=True)711 converted_non_recursive = api.to_graph(test_fn, recursive=False)712 self.assertNotEqual(converted_recursive.ag_module,713 converted_non_recursive.ag_module)714 self.assertRegex(715 tf_inspect.getsource(converted_recursive),716 'FunctionScope(.*recursive=True.*)')717 self.assertRegex(718 tf_inspect.getsource(converted_non_recursive),719 'FunctionScope(.*recursive=False.*)')720 def test_to_graph_preserves_bindings(self):721 y = 3722 def test_fn():723 return y724 converted = api.to_graph(test_fn)725 self.assertEqual(converted(), 3)726 y = 7727 self.assertEqual(converted(), 7)728 def test_to_graph_source_map(self):729 def test_fn(y):730 return y**2731 self.assertTrue(hasattr(api.to_graph(test_fn), 'ag_source_map'))732 def test_to_graph_sets_conversion_context(self):733 def g():734 self.assertEqual(ag_ctx.control_status_ctx().status,735 ag_ctx.Status.ENABLED)736 return 0737 # Note: the autograph=False sets the connect to Status.DISABLED. The test738 # verifies that to_graph overrides that.739 @def_function.function(autograph=False)740 def f():741 converted_g = api.to_graph(g)...

Full Screen

Full Screen

Legacy.py

Source:Legacy.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-2# Copyright 2012 Vincent Jacques3# vincent@vincent-jacques.net4# This file is part of PyGithub. http://vincent-jacques.net/PyGithub5# PyGithub is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License6# as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.7# PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.9# You should have received a copy of the GNU Lesser General Public License along with PyGithub. If not, see <http://www.gnu.org/licenses/>.10import urlparse11from PaginatedList import PaginatedListBase12class PaginatedList(PaginatedListBase):13 def __init__(self, url, args, requester, key, convert, contentClass):14 PaginatedListBase.__init__(self)15 self.__url = url16 self.__args = args17 self.__requester = requester18 self.__key = key19 self.__convert = convert20 self.__contentClass = contentClass21 self.__nextPage = 022 self.__continue = True23 def _couldGrow(self):24 return self.__continue25 def _fetchNextPage(self):26 page = self.__nextPage27 self.__nextPage += 128 return self.get_page(page)29 def get_page(self, page):30 assert isinstance(page, (int, long)), page31 args = dict(self.__args)32 if page != 0:33 args["start_page"] = page + 134 headers, data = self.__requester.requestAndCheck(35 "GET",36 self.__url,37 args,38 None39 )40 self.__continue = len(data[self.__key]) > 041 return [42 self.__contentClass(self.__requester, self.__convert(element), completed=False)43 for element in data[self.__key]44 ]45def convertUser(attributes):46 convertedAttributes = {47 "login": attributes["login"],48 "url": "/users/" + attributes["login"],49 }50 if "gravatar_id" in attributes: # pragma no branch51 convertedAttributes["gravatar_id"] = attributes["gravatar_id"]52 if "followers" in attributes: # pragma no branch53 convertedAttributes["followers"] = attributes["followers"]54 if "repos" in attributes: # pragma no branch55 convertedAttributes["public_repos"] = attributes["repos"]56 if "name" in attributes: # pragma no branch57 convertedAttributes["name"] = attributes["name"]58 if "created_at" in attributes: # pragma no branch59 convertedAttributes["created_at"] = attributes["created_at"]60 if "location" in attributes: # pragma no branch61 convertedAttributes["location"] = attributes["location"]62 return convertedAttributes63def convertRepo(attributes):64 convertedAttributes = {65 "owner": {"login": attributes["owner"], "url": "/users/" + attributes["owner"]},66 "url": "/repos/" + attributes["owner"] + "/" + attributes["name"],67 }68 if "pushed_at" in attributes: # pragma no branch69 convertedAttributes["pushed_at"] = attributes["pushed_at"]70 if "homepage" in attributes: # pragma no branch71 convertedAttributes["homepage"] = attributes["homepage"]72 if "created_at" in attributes: # pragma no branch73 convertedAttributes["created_at"] = attributes["created_at"]74 if "watchers" in attributes: # pragma no branch75 convertedAttributes["watchers"] = attributes["watchers"]76 if "has_downloads" in attributes: # pragma no branch77 convertedAttributes["has_downloads"] = attributes["has_downloads"]78 if "fork" in attributes: # pragma no branch79 convertedAttributes["fork"] = attributes["fork"]80 if "has_issues" in attributes: # pragma no branch81 convertedAttributes["has_issues"] = attributes["has_issues"]82 if "has_wiki" in attributes: # pragma no branch83 convertedAttributes["has_wiki"] = attributes["has_wiki"]84 if "forks" in attributes: # pragma no branch85 convertedAttributes["forks"] = attributes["forks"]86 if "size" in attributes: # pragma no branch87 convertedAttributes["size"] = attributes["size"]88 if "private" in attributes: # pragma no branch89 convertedAttributes["private"] = attributes["private"]90 if "open_issues" in attributes: # pragma no branch91 convertedAttributes["open_issues"] = attributes["open_issues"]92 if "description" in attributes: # pragma no branch93 convertedAttributes["description"] = attributes["description"]94 if "language" in attributes: # pragma no branch95 convertedAttributes["language"] = attributes["language"]96 if "name" in attributes: # pragma no branch97 convertedAttributes["name"] = attributes["name"]98 return convertedAttributes99def convertIssue(attributes):100 convertedAttributes = {101 "number": attributes["number"],102 "url": "/repos" + urlparse.urlparse(attributes["html_url"]).path,103 "user": {"login": attributes["user"], "url": "/users/" + attributes["user"]},104 }105 if "labels" in attributes: # pragma no branch106 convertedAttributes["labels"] = [{"name": label} for label in attributes["labels"]]107 if "title" in attributes: # pragma no branch108 convertedAttributes["title"] = attributes["title"]109 if "created_at" in attributes: # pragma no branch110 convertedAttributes["created_at"] = attributes["created_at"]111 if "comments" in attributes: # pragma no branch112 convertedAttributes["comments"] = attributes["comments"]113 if "body" in attributes: # pragma no branch114 convertedAttributes["body"] = attributes["body"]115 if "updated_at" in attributes: # pragma no branch116 convertedAttributes["updated_at"] = attributes["updated_at"]117 if "state" in attributes: # pragma no branch118 convertedAttributes["state"] = attributes["state"]...

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 yandex-tank 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