Best Python code snippet using grail_python
hosts.py
Source:hosts.py  
...30        '''31        hosts.list_hosts32        '''33        self.__clean_hosts()34        hosts = self.run_function('hosts.list_hosts')35        self.assertEqual(len(hosts), 6)36        self.assertEqual(hosts['::1'], ['ip6-localhost', 'ip6-loopback'])37        self.assertEqual(hosts['127.0.0.1'], ['localhost', 'myname'])38    def test_list_hosts_nofile(self):39        '''40        hosts.list_hosts41        without a hosts file42        '''43        if os.path.isfile(HFN):44            os.remove(HFN)45        hosts = self.run_function('hosts.list_hosts')46        self.assertEqual(hosts, {})47    def test_get_ip(self):48        '''49        hosts.get_ip50        '''51        self.__clean_hosts()52        self.assertEqual(53            self.run_function('hosts.get_ip', ['myname']), '127.0.0.1'54        )55        self.assertEqual(self.run_function('hosts.get_ip', ['othername']), '')56        self.__clear_hosts()57        self.assertEqual(self.run_function('hosts.get_ip', ['othername']), '')58    def test_get_alias(self):59        '''60        hosts.get_alias61        '''62        self.__clean_hosts()63        self.assertEqual(64            self.run_function('hosts.get_alias', ['127.0.0.1']),65            ['localhost', 'myname']66        )67        self.assertEqual(68            self.run_function('hosts.get_alias', ['127.0.0.2']),69            []70        )71        self.__clear_hosts()72        self.assertEqual(73            self.run_function('hosts.get_alias', ['127.0.0.1']),74            []75        )76    def test_has_pair(self):77        '''78        hosts.has_pair79        '''80        self.__clean_hosts()81        self.assertTrue(82            self.run_function('hosts.has_pair', ['127.0.0.1', 'myname'])83        )84        self.assertFalse(85            self.run_function('hosts.has_pair', ['127.0.0.1', 'othername'])86        )87    def test_set_host(self):88        '''89        hosts.set_hosts90        '''91        self.__clean_hosts()92        assert self.run_function('hosts.set_host', ['192.168.1.123', 'newip'])93        self.assertTrue(94            self.run_function('hosts.has_pair', ['192.168.1.123', 'newip'])95        )96        self.assertEqual(len(self.run_function('hosts.list_hosts')), 7)97        assert self.run_function('hosts.set_host', ['127.0.0.1', 'localhost'])98        self.assertFalse(99            self.run_function('hosts.has_pair', ['127.0.0.1', 'myname']),100            'should remove second entry'101        )102    def test_add_host(self):103        '''104        hosts.add_host105        '''106        self.__clean_hosts()107        assert self.run_function('hosts.add_host', ['192.168.1.123', 'newip'])108        self.assertTrue(109            self.run_function('hosts.has_pair', ['192.168.1.123', 'newip'])110        )111        self.assertEqual(len(self.run_function('hosts.list_hosts')), 7)112        assert self.run_function(113            'hosts.add_host', ['127.0.0.1', 'othernameip']114        )115        self.assertEqual(len(self.run_function('hosts.list_hosts')), 7)116    def test_rm_host(self):117        self.__clean_hosts()118        assert self.run_function('hosts.has_pair', ['127.0.0.1', 'myname'])119        assert self.run_function('hosts.rm_host', ['127.0.0.1', 'myname'])120        assert not self.run_function('hosts.has_pair', ['127.0.0.1', 'myname'])121        assert self.run_function('hosts.rm_host', ['127.0.0.1', 'unknown'])122    def test_add_host_formatting(self):123        '''124        Ensure that hosts.add_host isn't adding duplicates and that125        it's formatting the output correctly126        '''127        # instead of using the "clean" hosts file we're going to128        # use an empty one so we can prove the syntax of the entries129        # being added by the hosts module130        self.__clear_hosts()131        f = open(HFN, 'w')132        f.close()133        assert self.run_function(134            'hosts.add_host', ['192.168.1.1', 'host1.fqdn.com']135        )136        assert self.run_function('hosts.add_host', ['192.168.1.1', 'host1'])137        assert self.run_function(138            'hosts.add_host', ['192.168.1.2', 'host2.fqdn.com']139        )140        assert self.run_function('hosts.add_host', ['192.168.1.2', 'host2'])141        assert self.run_function('hosts.add_host', ['192.168.1.2', 'oldhost2'])142        assert self.run_function(143            'hosts.add_host', ['192.168.1.3', 'host3.fqdn.com']144        )145        assert self.run_function(146            'hosts.add_host', ['192.168.1.2', 'host2-reorder']147        )148        assert self.run_function(149            'hosts.add_host', ['192.168.1.1', 'host1-reorder']150        )151        # now read the lines and ensure they're formatted correctly152        lines = open(HFN, 'r').readlines()153        self.assertEqual(lines, [154            "192.168.1.3\t\thost3.fqdn.com\n",155            "192.168.1.2\t\thost2.fqdn.com\thost2\toldhost2\thost2-reorder\n",156            "192.168.1.1\t\thost1.fqdn.com\thost1\thost1-reorder\n",157            ])158if __name__ == '__main__':159    from integration import run_tests...test_simple.py
Source:test_simple.py  
...21    ca = CLArrayContext(device_type=DEVICE_TYPE)22    23    print ca.devices24class TestBinaryOp(unittest.TestCase):25    def run_function(self, func, b, c):26        27        fmt = cl.type_formats.type_format(type(b))28        a = ca.empty([1], ctype=fmt)29        30        @cly.task31        def foo(a, b, c, function):32            a[0] = function(b, c)33            34        foo(ca, a, b, c, func)35        36        37        d = func(b, c)38        39        self.assertAlmostEqual(a[0].item().value, d)40     41    def test_add(self):42        43        self.run_function(lambda a, b: a + b, 1.0, 2.0)44         45    def test_sub(self):46        47        self.run_function(lambda a, b: a - b, 1.0, 2.0) 48        49    def test_mul(self):50        51        self.run_function(lambda a, b: a * b, 1.0, 2.0) 52        53    def test_pow(self):54        55        self.run_function(lambda a, b: a ** b, 2.0, 2.0) 56        57    def test_div(self):58        59        self.run_function(lambda a, b: a / b, 2.0, 5.0) 60class TestCompOp(unittest.TestCase):61    def run_function(self, func, b, c):62        63        a = ca.empty([1], ctype='B')64        65        @cly.task66        def foo(a, b, c, function):67            a[0] = function(b, c)68            69        foo(ca, a, b, c, func)70        71        72        d = func(b, c)73        74        self.assertAlmostEqual(a[0].item().value, d)75    def test_lt(self):76        77        self.run_function(lambda a, b: a < b, 2.0, 5.0) 78        self.run_function(lambda a, b: a < b, 5.0, 2.0) 79    def test_gt(self):80        self.run_function(lambda a, b: a > b, 2.0, 5.0) 81        self.run_function(lambda a, b: a > b, 5.0, 2.0) 82    def test_gtEq(self):83        self.run_function(lambda a, b: a >= b, 2.0, 5.0) 84        self.run_function(lambda a, b: a >= b, 5.0, 2.0) 85        self.run_function(lambda a, b: a >= b, 5.0, 5.0) 86        87    def test_ltEq(self):88        self.run_function(lambda a, b: a <= b, 2.0, 5.0) 89        self.run_function(lambda a, b: a <= b, 5.0, 2.0) 90        self.run_function(lambda a, b: a <= b, 5.0, 5.0) 91        92    def test_eq(self):93        self.run_function(lambda a, b: a == b, 2.0, 5.0) 94        self.run_function(lambda a, b: a == b, 5.0, 5.0) 95    def test_neq(self):96        self.run_function(lambda a, b: a != b, 2.0, 5.0) 97        self.run_function(lambda a, b: a != b, 5.0, 5.0) 98        99class TestStatements(unittest.TestCase):100    pass101    102if __name__ == "__main__":103    #import sys;sys.argv = ['', 'Test.test_add']...time_complexity.py
Source:time_complexity.py  
...46  i = 047  while(i<n):48    function6(n-1)49    i+=150def run_function(function, n, ratio):51  start_time = round(time.time() * 1000)52  function(int(n)/int(ratio))53  end_time = round(time.time() * 1000)54  time_used = f"total time used for {function.name} with n={n}: {(end_time-start_time)*ratio} ms"55  print(time_used)56def run_functions():57  n = input("enter n: ")58  run_function(function0, n, 1)59  run_function(function1, n, 1)60  run_function(function2, n, 1)61  run_function(function3, n, 1)62  run_function(function4, n, 1)63  run_function(function5, n, 40000)64  run_function(function6, n, 100000)...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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
