Best Python code snippet using keyboard
_mouse_tests.py
Source:_mouse_tests.py
...40 # Make sure there's no spill over between tests.41 self.wait_for_events_queue()42 def wait_for_events_queue(self):43 mouse._listener.queue.join()44 def flush_events(self):45 self.wait_for_events_queue()46 events = list(self.events)47 # Ugly, but requried to work in Python2. Python3 has list.clear48 del self.events[:]49 return events50 def press(self, button=LEFT):51 mouse._os_mouse.queue.put(ButtonEvent(DOWN, button, time.time()))52 self.wait_for_events_queue()53 def release(self, button=LEFT):54 mouse._os_mouse.queue.put(ButtonEvent(UP, button, time.time()))55 self.wait_for_events_queue()56 def double_click(self, button=LEFT):57 mouse._os_mouse.queue.put(ButtonEvent(DOUBLE, button, time.time()))58 self.wait_for_events_queue()59 def click(self, button=LEFT):60 self.press(button)61 self.release(button)62 def wheel(self, delta=1):63 mouse._os_mouse.queue.put(WheelEvent(delta, time.time()))64 self.wait_for_events_queue()65 def move(self, x=0, y=0):66 mouse._os_mouse.queue.put(MoveEvent(x, y, time.time()))67 self.wait_for_events_queue()68 def test_hook(self):69 events = []70 self.press()71 mouse.hook(events.append)72 self.press()73 mouse.unhook(events.append)74 self.press()75 self.assertEqual(len(events), 1)76 def test_is_pressed(self):77 self.assertFalse(mouse.is_pressed())78 self.press()79 self.assertTrue(mouse.is_pressed())80 self.release()81 self.press(X2)82 self.assertFalse(mouse.is_pressed())83 self.assertTrue(mouse.is_pressed(X2))84 self.press(X2)85 self.assertTrue(mouse.is_pressed(X2))86 self.release(X2)87 self.release(X2)88 self.assertFalse(mouse.is_pressed(X2))89 def test_buttons(self):90 mouse.press()91 self.assertEqual(self.flush_events(), [(DOWN, LEFT)])92 mouse.release()93 self.assertEqual(self.flush_events(), [(UP, LEFT)])94 mouse.click()95 self.assertEqual(self.flush_events(), [(DOWN, LEFT), (UP, LEFT)])96 mouse.double_click()97 self.assertEqual(self.flush_events(), [(DOWN, LEFT), (UP, LEFT), (DOWN, LEFT), (UP, LEFT)])98 mouse.right_click()99 self.assertEqual(self.flush_events(), [(DOWN, RIGHT), (UP, RIGHT)])100 mouse.click(RIGHT)101 self.assertEqual(self.flush_events(), [(DOWN, RIGHT), (UP, RIGHT)])102 mouse.press(X2)103 self.assertEqual(self.flush_events(), [(DOWN, X2)])104 def test_position(self):105 self.assertEqual(mouse.get_position(), mouse._os_mouse.get_position())106 def test_move(self):107 mouse.move(0, 0)108 self.assertEqual(mouse._os_mouse.get_position(), (0, 0))109 mouse.move(100, 500)110 self.assertEqual(mouse._os_mouse.get_position(), (100, 500))111 mouse.move(1, 2, False)112 self.assertEqual(mouse._os_mouse.get_position(), (101, 502))113 mouse.move(0, 0)114 mouse.move(100, 499, True, duration=0.01)115 self.assertEqual(mouse._os_mouse.get_position(), (100, 499))116 mouse.move(100, 1, False, duration=0.01)117 self.assertEqual(mouse._os_mouse.get_position(), (200, 500))118 mouse.move(0, 0, False, duration=0.01)119 self.assertEqual(mouse._os_mouse.get_position(), (200, 500))120 def triggers(self, fn, events, **kwargs):121 self.triggered = False122 def callback():123 self.triggered = True124 handler = fn(callback, **kwargs)125 for event_type, arg in events:126 if event_type == DOWN:127 self.press(arg)128 elif event_type == UP:129 self.release(arg)130 elif event_type == DOUBLE:131 self.double_click(arg)132 elif event_type == 'WHEEL':133 self.wheel()134 mouse._listener.remove_handler(handler)135 return self.triggered136 def test_on_button(self):137 self.assertTrue(self.triggers(mouse.on_button, [(DOWN, LEFT)]))138 self.assertTrue(self.triggers(mouse.on_button, [(DOWN, RIGHT)]))139 self.assertTrue(self.triggers(mouse.on_button, [(DOWN, X)]))140 self.assertFalse(self.triggers(mouse.on_button, [('WHEEL', '')]))141 self.assertFalse(self.triggers(mouse.on_button, [(DOWN, X)], buttons=MIDDLE))142 self.assertTrue(self.triggers(mouse.on_button, [(DOWN, MIDDLE)], buttons=MIDDLE))143 self.assertTrue(self.triggers(mouse.on_button, [(DOWN, MIDDLE)], buttons=MIDDLE))144 self.assertFalse(self.triggers(mouse.on_button, [(DOWN, MIDDLE)], buttons=MIDDLE, types=UP))145 self.assertTrue(self.triggers(mouse.on_button, [(UP, MIDDLE)], buttons=MIDDLE, types=UP))146 self.assertTrue(self.triggers(mouse.on_button, [(UP, MIDDLE)], buttons=[MIDDLE, LEFT], types=[UP, DOWN]))147 self.assertTrue(self.triggers(mouse.on_button, [(DOWN, LEFT)], buttons=[MIDDLE, LEFT], types=[UP, DOWN]))148 self.assertFalse(self.triggers(mouse.on_button, [(UP, X)], buttons=[MIDDLE, LEFT], types=[UP, DOWN]))149 def test_ons(self):150 self.assertTrue(self.triggers(mouse.on_click, [(UP, LEFT)]))151 self.assertFalse(self.triggers(mouse.on_click, [(UP, RIGHT)]))152 self.assertFalse(self.triggers(mouse.on_click, [(DOWN, LEFT)]))153 self.assertFalse(self.triggers(mouse.on_click, [(DOWN, RIGHT)]))154 self.assertTrue(self.triggers(mouse.on_double_click, [(DOUBLE, LEFT)]))155 self.assertFalse(self.triggers(mouse.on_double_click, [(DOUBLE, RIGHT)]))156 self.assertFalse(self.triggers(mouse.on_double_click, [(DOWN, RIGHT)]))157 self.assertTrue(self.triggers(mouse.on_right_click, [(UP, RIGHT)]))158 self.assertTrue(self.triggers(mouse.on_middle_click, [(UP, MIDDLE)]))159 def test_wait(self):160 # If this fails it blocks. Unfortunately, but I see no other way of testing.161 from threading import Thread, Lock162 lock = Lock()163 lock.acquire()164 def t():165 mouse.wait()166 lock.release()167 Thread(target=t).start()168 self.press()169 lock.acquire()170 def test_record_play(self):171 from threading import Thread, Lock172 lock = Lock()173 lock.acquire()174 def t():175 self.recorded = mouse.record(RIGHT)176 lock.release()177 Thread(target=t).start()178 self.click()179 self.wheel(5)180 self.move(100, 50)181 self.press(RIGHT)182 lock.acquire()183 self.assertEqual(len(self.recorded), 5)184 self.assertEqual(self.recorded[0]._replace(time=None), ButtonEvent(DOWN, LEFT, None))185 self.assertEqual(self.recorded[1]._replace(time=None), ButtonEvent(UP, LEFT, None))186 self.assertEqual(self.recorded[2]._replace(time=None), WheelEvent(5, None))187 self.assertEqual(self.recorded[3]._replace(time=None), MoveEvent(100, 50, None))188 self.assertEqual(self.recorded[4]._replace(time=None), ButtonEvent(DOWN, RIGHT, None))189 mouse.play(self.recorded, speed_factor=0)190 events = self.flush_events()191 self.assertEqual(len(events), 5)192 self.assertEqual(events[0], (DOWN, LEFT))193 self.assertEqual(events[1], (UP, LEFT))194 self.assertEqual(events[2], ('wheel', 5))195 self.assertEqual(events[3], ('move', (100, 50)))196 self.assertEqual(events[4], (DOWN, RIGHT))197 mouse.play(self.recorded)198 events = self.flush_events()199 self.assertEqual(len(events), 5)200 self.assertEqual(events[0], (DOWN, LEFT))201 self.assertEqual(events[1], (UP, LEFT))202 self.assertEqual(events[2], ('wheel', 5))203 self.assertEqual(events[3], ('move', (100, 50)))204 self.assertEqual(events[4], (DOWN, RIGHT))205 mouse.play(self.recorded, include_clicks=False)206 events = self.flush_events()207 self.assertEqual(len(events), 2)208 self.assertEqual(events[0], ('wheel', 5))209 self.assertEqual(events[1], ('move', (100, 50)))210 mouse.play(self.recorded, include_moves=False)211 events = self.flush_events()212 self.assertEqual(len(events), 4)213 self.assertEqual(events[0], (DOWN, LEFT))214 self.assertEqual(events[1], (UP, LEFT))215 self.assertEqual(events[2], ('wheel', 5))216 self.assertEqual(events[3], (DOWN, RIGHT))217 mouse.play(self.recorded, include_wheel=False)218 events = self.flush_events()219 self.assertEqual(len(events), 4)220 self.assertEqual(events[0], (DOWN, LEFT))221 self.assertEqual(events[1], (UP, LEFT))222 self.assertEqual(events[2], ('move', (100, 50)))223 self.assertEqual(events[3], (DOWN, RIGHT))224if __name__ == '__main__':...
mapeo.py
Source:mapeo.py
...74 75 if sala[i]=='Salon_2':76 plt.plot(59,55,'s')77 fig.canvas.draw()78 fig.canvas.flush_events()79 # plt.show()80 # sleep(1)81 elif sala[i] == 'Salon_4':82 plt.plot(59,43,'s')83 fig.canvas.draw()84 fig.canvas.flush_events()85 # plt.show()86 # sleep(1)87 elif sala[i] == 'Pasillo_2':88 plt.plot(16,68,'s')89 fig.canvas.draw()90 fig.canvas.flush_events()91 #plt.show()92 # sleep(1)93 elif sala[i] == 'Cuarto3_2':94 plt.plot(81,15,'s')95 fig.canvas.draw()96 fig.canvas.flush_events()97 #plt.show()98 # sleep(1)99 elif sala[i] == 'Cuarto3_1':100 plt.plot(66,15,'s')101 fig.canvas.draw()102 fig.canvas.flush_events()103 #plt.show()104 # sleep(1)105 elif sala[i] == 'Cuarto2':106 plt.plot(39,15,'s')107 fig.canvas.draw()108 fig.canvas.flush_events()109 #plt.show()110 # sleep(1)111 elif sala[i] == 'Salon_3':112 plt.plot(37,43,'s')113 fig.canvas.draw()114 fig.canvas.flush_events()115 #plt.show()116 # sleep(1)117 elif sala[i] == 'Baño':118 plt.plot(5,60,'s')119 fig.canvas.draw()120 fig.canvas.flush_events()121 #plt.show()122 # sleep(1)123 elif sala[i] == 'Salon_1':124 plt.plot(37,55,'s')125 fig.canvas.draw()126 fig.canvas.flush_events()127 # plt.show()128 # sleep(1)129 elif sala[i] == 'Cuarto1_1':130 plt.plot(11,13,'s')131 fig.canvas.draw()132 fig.canvas.flush_events()133 #plt.show()134 # sleep(1)135 elif sala[i] == 'Cocina_2':136 plt.plot(59,79,'s')137 fig.canvas.draw()138 fig.canvas.flush_events()139 #plt.show()140 # sleep(1)141 elif sala[i] == 'Entrada':142 plt.plot(11,91,'s')143 fig.canvas.draw()144 fig.canvas.flush_events()145 #plt.show()146 # sleep(1)147 elif sala[i] == 'Balcon':148 plt.plot(82,43,'s')149 fig.canvas.draw()150 fig.canvas.flush_events()151 #plt.show()152 # sleep(1)153 elif sala[i] == 'Cuarto1_2':154 plt.plot(11,27,'s')155 fig.canvas.draw()156 fig.canvas.flush_events()157 #plt.show()158 # sleep(1)159 elif sala[i] == 'Cocina_1':160 plt.plot(39,79,'s')161 fig.canvas.draw()162 fig.canvas.flush_events()163 # plt.show()164 # sleep(1)165 elif sala[i] == 'Pasillo_1':166 plt.plot(16,53,'s')167 fig.canvas.draw()168 fig.canvas.flush_events()169 # plt.show()170 # sleep(1)171 172 sala_anterior = sala173 174# 175# 176# 177# 178# 179# ...
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!!