Best Python code snippet using Testify_python
client.py
Source:client.py
...20 #Start tasks21 for ext in self.extensions:22 for task in ext.tasks:23 await task.start(self, ext)24 await self.fire_event(None, "on_ready")25 async def execute(self, message, profile=False):26 if message.author.id == self.user.id:27 return28 if message.content.startswith(self.profile.prefix):29 raw = message.content[len(self.profile.prefix):].split()30 else:31 raw = message.content.split(" ", 2)[1:]32 cmd = raw[0].lower()33 args = " ".join(raw[1:])34 args = re.compile(r'''((?:[^\s"']|"[^"]*"|'[^']*')+)''').split(args)[1::2]35 new = []36 for arg in args:37 new.append(arg.strip("\""))38 args = new39 for ext in self.extensions:40 for c in ext.commands:41 if c.name == cmd:42 if bot.in_role_list(message.author, c.roles):43 if profile:44 import pprofile45 profiler = pprofile.Profile()46 with profiler():47 await self.command(c, bot.Context(self, ext, message), args)48 out = io.StringIO()49 with redirect_stdout(out):50 profiler.print_stats()51 resp = out.getvalue().split("\n")52 await bot.send_stats(c, self.profile.prefix, resp, raw, message.channel)53 else:54 await self.command(c, bot.Context(self, ext, message), args)55 else:56 await message.channel.send("You are not allowed to run that command.")57 async def command(self, c, ctx, args):58 try:59 logger.debug("Attempting to run {}".format(c.name))60 await c.run(ctx, args)61 except Exception:62 #exc_type, exc_obj, exc_tb = sys.exc_info()63 tb = traceback.format_exc()64 if self.profile.mode == "live":65 await ctx.message.channel.send("Oh no! :cry: An error occurred. It has been reported to the Code Contributers.")66 channel = discord.utils.find(lambda c: c.name == "errors", ctx.message.guild.channels)67 else:68 channel = ctx.message.channel69 await channel.send(70 "An error occured during the execution of `{}` by {} in {}\n\n```py\n{}\n```"71 .format(ctx.message.content, ctx.message.author.display_name, ctx.message.channel.mention, tb)72 )73 logger.error("An error occured: "+str(tb))74 async def on_message(self, message):75 await self.wait_until_ready()76 if message.author.id == self.user.id:77 return78 if (message.content.startswith(self.profile.prefix) and message.content.replace(self.profile.prefix, "").strip() != "") or (message.content.startswith(self.user.mention)):79 await self.execute(message)80 elif message.content.startswith("profile{}".format(self.profile.prefix)):81 message.content = message.content[7:]82 await self.execute(message, profile = True)83 else:84 await self.fire_event(message, "on_message")85 async def on_message_delete(self, message):86 await self.fire_event(message, "on_message_delete")87 async def on_message_edit(self, before, after):88 await self.fire_event([before, after], "on_message_edit")89 async def on_reaction_add(self, reaction, user):90 await self.fire_event([reaction, user], "on_reaction_add")91 async def on_reaction_remove(self, reaction, user):92 await self.fire_event([reaction, user], "on_reaction_remove")93 async def on_reaction_clear(self, message, reactions):94 await self.fire_event([message, reactions], "on_reaction_clear")95 async def on_member_join(self, member):96 await self.fire_event(member, "on_member_join")97 async def on_member_remove(self, member):98 await self.fire_event(member, "on_member_remove")99 async def on_member_update(self, before, after):100 await self.fire_event([before, after], "on_member_update")101 async def fire_event(self, args, event):102 await self.wait_until_ready()103 for ext in self.extensions:104 for h in ext.handlers:105 if h.event == event:106 try:107 if h.dev and self.profile.mode == "test":108 await h.run(bot.Context(self, ext, args))109 else:110 if h.live:111 if self.profile.mode == "live":112 await h.run(bot.Context(self, ext, args))113 else:114 await h.run(bot.Context(self, ext, args))115 except Exception:...
deconz_magic_cube.py
Source:deconz_magic_cube.py
...29 if data['event'] in [1000, 2000, 3000, 4000, 5000, 6000]:30 self.log('Magic move')31 to_side = data['event'] // 100032 self.set_state(self.sensor_name, state = to_side)33 self.fire_event(self.event_name, entity_id = self.args['id'], action_type = Action.MOVE)34 elif data['event'] in [1001, 2002, 3003, 4004, 5005, 6006]:35 to_side = data['event'] % 100036 self.set_state(self.sensor_name, state = to_side)37 self.fire_event(self.event_name, entity_id = self.args['id'], action_type = Action.TAP_TWICE)38 self.log('Magic tap twice to ' + str(to_side))39 elif data['event'] in [1006, 2005, 3004, 4003, 5002, 6001]:40 self.log('Magic flip 180')41 from_side = data['event'] % 100042 to_side = data['event'] // 100043 self.set_state(self.sensor_name, state = to_side, attributes={'from_side': from_side})44 self.fire_event(self.event_name, entity_id = self.args['id'], action_type = Action.FLIP180)45 elif data['event'] in [1002, 1003, 1004, 1005, 2001, 2003, 2004, 2006, 3001, 3002, 3005, 3006, 4001, 4002, 4005, 4006, 5001, 5003, 5004, 5006, 6002, 6003, 6004, 6005]:46 self.log('Magic flip 90')47 from_side = data['event'] % 100048 to_side = data['event'] // 100049 self.set_state(self.sensor_name, state = to_side, attributes={'from_side': from_side})50 self.fire_event(self.event_name, entity_id = self.args['id'], action_type = Action.FLIP90)51 elif data['event'] == 7007:52 self.log('Magic shake')53 self.fire_event(self.event_name, entity_id = self.args['id'], action_type = Action.SHAKE_AIR)54 elif data['event'] == 7008:55 self.log('Magic drop')56 self.fire_event(self.event_name, entity_id = self.args['id'], action_type = Action.FREE_FALL)57 elif data['event'] == 7000:58 self.log('Magic alert')59 self.fire_event(self.event_name, entity_id = self.args['id'], action_type = Action.ALERT)60 else:61 degrees = data['event'] / 10062 self.fire_event(self.event_name, entity_id = self.args['id'], action_type = Action.ROTATE, action_value = degrees)
...
deconz_hue_dimmer.py
Source:deconz_hue_dimmer.py
...11 if data['id'] == self.args['id']:12 self.log(data['event'])13 if data['event'] == 1002:14 self.log('Button on short release')15 self.fire_event(self.event_name, entity_id = self.args['id'], state="on", detail="sr")16 if data['event'] == 1001:17 self.log('Button on hold')18 self.fire_event(self.event_name, entity_id = self.args['id'], state="on", detail="hold")19 if data['event'] == 1003:20 self.log('Button on long release')21 self.fire_event(self.event_name, entity_id = self.args['id'], state="on", detail="lr")22 elif data['event'] == 2002:23 self.log('Button dim up short release')24 self.fire_event(self.event_name, entity_id = self.args['id'], state="dimup", detail="sr")25 elif data['event'] == 2001:26 self.log('Button dim up hold')27 self.fire_event(self.event_name, entity_id = self.args['id'], state="dimup", detail="hold")28 elif data['event'] == 2003:29 self.log('Button dim up long release')30 self.fire_event(self.event_name, entity_id = self.args['id'], state="dimup", detail="lr")31 elif data['event'] == 3002:32 self.log('Button dim down short release')33 self.fire_event(self.event_name, entity_id = self.args['id'], state="dimdown", detail="sr")34 elif data['event'] == 3001:35 self.log('Button dim down hold')36 self.fire_event(self.event_name, entity_id = self.args['id'], state="dimdown", detail="hold")37 elif data['event'] == 3003:38 self.log('Button dim down long release')39 self.fire_event(self.event_name, entity_id = self.args['id'], state="dimdown", detail="lr")40 elif data['event'] == 4002:41 self.log('Button off short release')42 self.fire_event(self.event_name, entity_id = self.args['id'], state="off", detail="sr")43 elif data['event'] == 4001:44 self.log('Button off hold')45 self.fire_event(self.event_name, entity_id = self.args['id'], state="off", detail="hold")46 elif data['event'] == 4003:47 self.log('Button off long release')
...
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!!