Best Python code snippet using keyboard
example.py
Source:example.py
...31 loop.call_later(1, functools.partial(print_current_date, endtime=endtime, loop=loop))32 # loop.call_later(1, print_current_date, endtime, loop)33 else:34 loop.stop()35def test_call_later():36 loop = asyncio.get_event_loop()37 endtime = loop.time() + 538 loop.call_soon(print_current_date, endtime, loop)39 loop.run_forever()40 print('2')41 loop.close()42async def compute(x, y):43 print("Compute {} + {} at {}".format(x, y, time.time()))44 # éå° await, å½åç¨åº(compute)æèµ·, çå¾
asyncio.sleep ç»æ45 await asyncio.sleep(3.0)46 print("continue at {}".format(time.time()))47 return x + y48async def print_sum(x, y):49 print("print in at {}".format(time.time()))50 # éå° await, å½åç¨åº(print_sum)æèµ·, çå¾
compute è¿å51 result = await compute(x, y)52 print("%s + %s = %s" % (x, y, result))53 return result54def test_async_await():55 loop = asyncio.get_event_loop() # <=> asyncio.get_event_loop_policy().get_event_loop()56 print("start...{}".format(time.time()))57 # loop.run_until_complete(print_sum(1, 2))58 # çä»·äº59 obj_coro = print_sum(1, 2)60 task = loop.create_task(obj_coro)61 # task = asyncio.ensure_future(obj_coro)62 # ç»å®åè°å½æ°å¤çç»æ63 def callback_1(future):64 print("print sum callback return value is {}".format(future.result()))65 def callback_2(x, future):66 print("print sum callback mutil param return values is {}={}".format(x, future.result()))67 task.add_done_callback(callback_1)68 # å¤åæ°69 task.add_done_callback(functools.partial(callback_2, "i am x"))70 loop.run_until_complete(task)71 print("end ...{}".format(time.time()))72 # ä¸ç»å®åè°å½æ°,å¤æç¶æåç»æ73 if task.done():74 print("result is :{}".format(task.result()))75 loop.close()76def test_file_watch():77 try:78 from socket import socketpair79 except ImportError:80 from asyncio.windows_utils import socketpair81 rsocket, wsocket = socketpair()82 loop = asyncio.get_event_loop()83 def reader():84 data = rsocket.recv(100)85 print("receive data:", data.decode())86 loop.remove_reader(rsocket)87 loop.stop()88 loop.add_reader(rsocket, reader)89 loop.call_later(3, wsocket.send, "abcd".encode())90 loop.run_forever()91 rsocket.close()92 wsocket.close()93 loop.close()94def test_add_signal():95 def on_signal(signame):96 print('receive signal:{}'.format(signame))97 loop = asyncio.get_event_loop()98 signalnamelist = ['SIGTERM', 'SIGINT']99 for signame in signalnamelist:100 loop.add_signal_handler(getattr(signal, signame), functools.partial(on_signal, signame))101 try:102 loop.run_forever()103 finally:104 loop.close()105def main():106 # æµè¯ çå¾
å¼æ¥ä»»å¡æ§è¡ç»æ107 # test_run_until_complete()108 # æµè¯ å¾ªç¯ è®¡åä»»å¡æ§è¡109 test_call_later()110 # æµè¯ await111 # test_async_await()112 print("done")113 # watch file descriptor114 # test_file_watch()115 # set signal116 # test_add_signal()117if __name__ == '__main__':...
test_eventloop.py
Source:test_eventloop.py
...25 loop.call_soon(partial(action, 3))26 await asyncio.sleep(10)27 assert result == [1, 2, 3]28@pytest.mark.asyncio29async def test_call_later():30 result = []31 def action(value: int) -> None:32 result.append(value)33 loop = asyncio.get_event_loop()34 loop.call_later(10, partial(action, 1))35 loop.call_later(1, partial(action, 2))36 loop.call_later(5, partial(action, 3))37 await asyncio.sleep(10)38 assert result == [2, 3, 1]39@pytest.mark.asyncio40async def test_call_at():41 result = []42 def action(value: int) -> None:43 result.append(value)...
test_event_loop.py
Source:test_event_loop.py
...15 loop.call_soon(lambda: ret.append('d'))16 loop.start()17 self.assertListEqual(['a', 'a', 'd', 'b', 'b',18 'c', 'c', 'finish', 'finish'], ret)19 def test_call_later(self):20 loop = self.eventloop21 ret = []22 a = lambda: ret.append('a') or loop.call_soon(finish)23 b = lambda: ret.append('b')24 c = lambda: ret.append('c')25 d = lambda: ret.append('d')26 finish = lambda: ret.append('finish') or loop.stop()27 loop.call_later(.1, a)28 loop.call_later(.04, b)29 loop.call_later(.02, d)30 loop.call_later(.07, c)31 loop.start()32 self.assertListEqual(['d', 'b', 'c', 'a', 'finish'], ret)33 def test_cancel(self):...
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!!