Best Python code snippet using autotest_python
command.py
Source:command.py
...52 self.handle_main_task()53 def handle_main_task(self):54 items = self.retrieve_library_items()55 if not items:56 self._say("Your query did not produce any results.", log_only=False)57 return58 for item in items:59 self.process_item(item)60 item.try_write()61 item.store()62 def process_item(self, item: Item):63 self._say("Fixing item: {}".format(item), log_only=True)64 year = item.get("year")65 original_year = item.get("original_year")66 if not original_year or self.cfg_force:67 mbdata = self._get_mb_data(item)68 if mbdata:69 extracted = common.extract_original_year_from_mb_data(mbdata)70 if extracted:71 original_year = extracted72 self._say("Got (MusicBrainz) recording `original_year`: {}"73 .format(original_year))74 if not original_year:75 original_year = self.get_mean_value_for_album(item, "original_year")76 self._say("Got (mean-album) `original_year`: {}".format(original_year))77 if not original_year:78 original_year = self.get_mean_value_for_artist(item, "original_year")79 self._say("Got (mean-artist) `original_year`: {}".format(original_year))80 if not year or self.cfg_force:81 year = self.get_mean_value_for_album(item, "year")82 self._say("Got (mean-album) `year`: {}".format(year))83 if not year:84 year = self.get_mean_value_for_artist(item, "year")85 self._say("Got (mean-artist) `year`: {}".format(year))86 if original_year:87 setattr(item, "original_year", original_year)88 if year:89 setattr(item, "year", year)90 if original_year and not year:91 setattr(item, "year", original_year)92 if year and not original_year:93 setattr(item, "original_year", year)94 if not year and not original_year:95 self._say("Cannot find info!")96 def get_mean_value_for_album(self, item: Item, field_name):97 answer = None98 query = MatchQuery('mb_albumid', item.get("mb_albumid"))99 items = self.lib.items(query)100 values = []101 for it in items:102 if it.get(field_name):103 val = int(it.get(field_name))104 if 0 < val < 2100:105 values.append(val)106 if values:107 answer = int(round(sum(values) / len(values)))108 return answer109 def get_mean_value_for_artist(self, item: Item, field_name):110 answer = None111 query = MatchQuery('mb_artistid', item.get("mb_artistid"))112 items = self.lib.items(query)113 values = []114 for it in items:115 if it.get(field_name):116 val = int(it.get(field_name))117 if 0 < val < 2100:118 values.append(val)119 if values:120 answer = int(round(sum(values) / len(values)))121 return answer122 def _get_mb_data(self, item: Item):123 data = {}124 try:125 url = common.get_mb_search_url(item)126 except AttributeError as err:127 self._say(err, is_error=True)128 return data129 # self._say(u'fetching URL: {}'.format(url))130 headers = {131 'User-Agent': '{pt}/{ver} ( {url} )'.format(132 pt=common.plg_ns['__PACKAGE_TITLE__'],133 ver=common.plg_ns['__version__'],134 url=common.plg_ns['__PACKAGE_URL__'],135 ),136 }137 max_retries = 5138 sleep_time = 3139 retries = 0140 while not data:141 retries += 1142 # self._say('Retry #{}'.format(retries))143 if retries > max_retries:144 self._say("Maximum({}) retries reached. Abandoning.".format(max_retries), is_error=True)145 break146 try:147 res = requests.get(url, headers=headers)148 except requests.RequestException as err:149 self._say(err, is_error=True)150 break151 if res.status_code == 503:152 # we hit the query limit -153 # https://musicbrainz.org/doc/XML_Web_Service/Rate_Limiting154 self._say('Retry #{} - Query LIMIT Hit! sleeping {}s.'155 .format(retries, sleep_time))156 time.sleep(sleep_time)157 continue158 if res.status_code == 404:159 self._say('404 - Not found.', is_error=True)160 break161 try:162 data = res.json()163 except ValueError as err:164 self._say('Invalid Response: {}'.format(err), is_error=True)165 break166 return data167 def retrieve_library_items(self):168 cmd_query = self.query169 parsed_cmd_query, parsed_ordering = parse_query_parts(cmd_query, Item)170 if self.cfg_force:171 full_query = parsed_cmd_query172 else:173 parsed_plg_query = OrQuery([174 NumericQuery('year', '0'),175 MatchQuery('year', ''),176 NoneQuery('year'),177 NumericQuery('original_year', '0'),178 MatchQuery('original_year', ''),179 NoneQuery('original_year'),180 ])181 full_query = AndQuery([parsed_cmd_query, parsed_plg_query])182 self._say("Selection query: {}".format(full_query))183 return self.lib.items(full_query, parsed_ordering)184 def show_version_information(self):185 self._say("{pt}({pn}) plugin for Beets: v{ver}".format(186 pt=common.plg_ns['__PACKAGE_TITLE__'],187 pn=common.plg_ns['__PACKAGE_NAME__'],188 ver=common.plg_ns['__version__']189 ), log_only=False)190 @staticmethod191 def _say(msg, log_only=True, is_error=False):...
match.py
Source:match.py
...35 self._sets = []36 self._points = []37 self._sets_history = []38 self._screen.set_score((0, 0), (0, 0))39 self._say('begin_match', reset=True)40 self._screen.can_undo = False41 if serving:42 self._serving = serving - 143 self._screen.set_serving(self._serving + 1)44 self._say('serving', self.players[self._serving].name)45 else:46 self._serving = None47 self._screen.set_serving(None)48 self._say('serve_play')49 def set_tts(self, tts):50 if tts and self.tts is None:51 self.tts = TTS(txt.lang)52 elif not tts and self.tts is not None:53 self.tts = None54 def _select_serving(self):55 points = len(self._points)56 if self._points[0] is None: points -= 157 if points < 2 * self._last_point:58 serving = ((points // 2) % 2) ^ self._serving59 else:60 serving = ((points - self._last_point) % 2) ^ self._serving61 self._screen.set_serving(serving + 1)62 return serving63 def _process_points(self):64 points = [self._points.count(p) for p in (0, 1)]65 serving = self._select_serving()66 reset_tts_on_win = False67 try:68 if points[0] >= self._last_point and points[1] >= self._last_point:69 # Tie break70 delta = points[1] - points[0]71 if delta <= -2:72 reset_tts_on_win = True73 raise SetOver(0)74 elif delta >= 2:75 reset_tts_on_win = True76 raise SetOver(1)77 else:78 if self._show_advantages:79 if delta == -1:80 self._screen.set_score((f'{self._last_point}+', self._last_point))81 self._say('advantage', self.players[0].name, reset=True)82 elif delta == 1:83 self._screen.set_score((self._last_point, f'{self._last_point}+'))84 self._say('advantage', self.players[1].name, reset=True)85 else:86 self._screen.set_score((self._last_point, self._last_point))87 self._say('deuce', reset=True)88 else:89 self._screen.set_score(points)90 self._say_score(points, serving)91 else:92 self._screen.set_score(points)93 self._say_score(points, serving)94 for i in 0, 1:95 if points[i] > self._last_point: raise SetOver(i)96 self._say('serving', self.players[serving].name)97 except SetOver as set:98 self._sets_history.append(self._points)99 self._sets.append(set.winner)100 self._points = []101 sets = [self._sets.count(p) for p in (0, 1)]102 self._screen.set_score(sets=sets)103 winner = self.players[set.winner]104 if self._sets.count(set.winner) >= self._match_sets:105 self._say('wins_match', winner.name, reset=reset_tts_on_win)106 stats = [[sp.count(p) for p in (0, 1)] for sp in self._sets_history]107 self._app.game_over(winner, sets, stats)108 else:109 self._say('wins_set', winner.name, reset=reset_tts_on_win)110 self._screen.set_score((0, 0))111 self._say('change_places')112 self._say('score', 0, 0)113 self._serving = 1 - self._serving114 self._screen.set_serving(self._serving + 1)115 self._say('serving', self.players[self._serving].name)116 def score(self, player):117 player -= 1118 if not (self._points or self._sets):119 self._screen.can_undo = True120 if self._serving is None:121 self._points.append(None)122 self._serving = player123 self._screen.set_serving(player + 1)124 self._say('serving', self.players[player].name)125 else:126 self._points.append(player)127 self._process_points()128 def undo(self):129 if self._points:130 p = self._points.pop()131 if p is None:132 self._serving = None133 self._say('serve_play')134 self._screen.set_serving(None)135 else:136 self._process_points()137 elif self._sets:138 self._sets.pop()139 self._points = self._sets_history.pop()140 self._points.pop()141 self._say('change_places')142 self._serving = 1 - self._serving143 self._screen.set_score(sets=[self._sets.count(p) for p in (0, 1)])144 self._process_points()145 if not (self._points or self._sets):146 self._screen.can_undo = False147 @property148 def current_stats(self):149 stats = [[sp.count(p) for p in (0, 1)] for sp in (self._sets_history + [self._points])]150 sets = [self._sets.count(p) for p in (0, 1)]151 return sets, stats152 def _say_score(self, points, serving, reset=True):153 if self._tts_order == 'first_serve':154 if self._serving == 1: points = points[1], points[0]155 elif self._tts_order == 'current_serve':156 if serving == 1: points = points[1], points[0]157 self._say('score', *points, reset=reset)158 def _say(self, what, *args, reset=False):159 text = getattr(txt, 'tts_' + what).format(*args)160 if self.tts:...
nxt.py
Source:nxt.py
...57class Say():58 def __init__(self,brick):59 self._brick = brick60 61 def _say(self,str):62 self._brick.play_sound_file(False, bytes((str+'.rso').encode('ascii')))63 64 def hello(self):65 self._say('Hello')66 67 def youre_good(self):68 self._say("You're Good")69 70 def good_job(self):71 self._say('Good Job')72 73 def have_a_nice_day(self):74 self._say('Have A Nice Day')75 76 def hot(self):77 self._say('Hot')78 79 def thank_you(self):80 self._say('Thank You')81 82 def woops(self):83 self._say('Woops')84 85 def game_over(self):86 self._say('Game Over')87 88 def sorry(self):89 self._say('Sorry')90 91 def object_detected(self):92 self._say('Object Detected')93 94 def music(self):95 self._say('Music')96 97 def system_overload(self):98 self._say('System Overload')99 100 def buuuhh(self):101 self._say('Buuuhh')102 103 def green(self):104 self._say('Green')105 106import math107import time108def Musica(brick):109 global _brick110 global _tempo111 _brick = brick112 _tempo = 0.5113 114def _freq(fbase,octava):115 if octava == 0:116 return int(fbase)117 else:118 return int(fbase*math.pow(2,octava))...
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!!