Best Python code snippet using avocado_python
album.py
Source:album.py
1# -*- coding: utf-8 -*-2#3# Picard, the next-generation MusicBrainz tagger4#5# Copyright (C) 2016 Rahul Raturi6# Copyright (C) 2018-2019 Laurent Monin7# Copyright (C) 2018-2019 Philipp Wolfer8#9# This program is free software; you can redistribute it and/or10# modify it under the terms of the GNU General Public License11# as published by the Free Software Foundation; either version 212# of the License, or (at your option) any later version.13#14# This program is distributed in the hope that it will be useful,15# but WITHOUT ANY WARRANTY; without even the implied warranty of16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the17# GNU General Public License for more details.18#19# You should have received a copy of the GNU General Public License20# along with this program; if not, write to the Free Software21# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.22from functools import partial23from PyQt5 import (24 QtCore,25 QtGui,26 QtWidgets,27)28from PyQt5.QtCore import pyqtSignal29from picard import (30 config,31 log,32)33from picard.const import (34 CAA_HOST,35 CAA_PORT,36 QUERY_LIMIT,37)38from picard.coverart.image import CaaThumbnailCoverArtImage39from picard.mbjson import (40 countries_from_node,41 media_formats_from_node,42 release_group_to_metadata,43 release_to_metadata,44)45from picard.metadata import Metadata46from picard.webservice.api_helpers import escape_lucene_query47from picard.ui.searchdialog import (48 Retry,49 SearchDialog,50)51class CoverWidget(QtWidgets.QWidget):52 shown = pyqtSignal()53 def __init__(self, parent, width=100, height=100):54 super().__init__(parent)55 self.layout = QtWidgets.QVBoxLayout(self)56 self.layout.setContentsMargins(0, 0, 0, 0)57 self.layout.setAlignment(QtCore.Qt.AlignCenter)58 self.loading_gif_label = QtWidgets.QLabel(self)59 self.loading_gif_label.setAlignment(QtCore.Qt.AlignCenter)60 loading_gif = QtGui.QMovie(":/images/loader.gif")61 self.loading_gif_label.setMovie(loading_gif)62 loading_gif.start()63 self.layout.addWidget(self.loading_gif_label)64 self.__sizehint = self.__size = QtCore.QSize(width, height)65 self.setStyleSheet("padding: 0")66 def set_pixmap(self, pixmap):67 wid = self.layout.takeAt(0)68 if wid:69 wid.widget().deleteLater()70 cover_label = QtWidgets.QLabel(self)71 pixmap = pixmap.scaled(self.__size, QtCore.Qt.KeepAspectRatio,72 QtCore.Qt.SmoothTransformation)73 self.__sizehint = pixmap.size()74 cover_label.setPixmap(pixmap)75 self.layout.addWidget(cover_label)76 def not_found(self):77 """Update the widget with a blank image."""78 shadow = QtGui.QPixmap(":/images/CoverArtShadow.png")79 self.set_pixmap(shadow)80 def sizeHint(self):81 return self.__sizehint82 def showEvent(self, event):83 super().showEvent(event)84 self.shown.emit()85class CoverCell:86 def __init__(self, parent, release, row, colname, on_show=None):87 self.parent = parent88 self.release = release89 self.fetched = False90 self.fetch_task = None91 self.row = row92 self.column = self.parent.colpos(colname)93 widget = CoverWidget(self.parent.table)94 if on_show is not None:95 widget.shown.connect(partial(on_show, self))96 self.parent.table.setCellWidget(row, self.column, widget)97 def widget(self):98 if not self.parent.table:99 return None100 return self.parent.table.cellWidget(self.row, self.column)101 def is_visible(self):102 widget = self.widget()103 if not widget:104 return False105 return not widget.visibleRegion().isEmpty()106 def set_pixmap(self, pixmap):107 widget = self.widget()108 if widget:109 widget.set_pixmap(pixmap)110 def not_found(self):111 widget = self.widget()112 if widget:113 widget.not_found()114class AlbumSearchDialog(SearchDialog):115 dialog_header_state = "albumsearchdialog_header_state"116 options = [117 config.Option("persist", dialog_header_state, QtCore.QByteArray())118 ]119 def __init__(self, parent):120 super().__init__(121 parent,122 accept_button_title=_("Load into Picard"),123 search_type="album")124 self.cluster = None125 self.setWindowTitle(_("Album Search Results"))126 self.columns = [127 ('name', _("Name")),128 ('artist', _("Artist")),129 ('format', _("Format")),130 ('tracks', _("Tracks")),131 ('date', _("Date")),132 ('country', _("Country")),133 ('labels', _("Labels")),134 ('catnums', _("Catalog #s")),135 ('barcode', _("Barcode")),136 ('language', _("Language")),137 ('type', _("Type")),138 ('status', _("Status")),139 ('cover', _("Cover")),140 ('score', _("Score")),141 ]142 self.cover_cells = []143 self.fetching = False144 self.scrolled.connect(self.fetch_coverarts)145 def search(self, text):146 """Perform search using query provided by the user."""147 self.retry_params = Retry(self.search, text)148 self.search_box_text(text)149 self.show_progress()150 self.tagger.mb_api.find_releases(self.handle_reply,151 query=text,152 search=True,153 limit=QUERY_LIMIT)154 def show_similar_albums(self, cluster):155 """Perform search by using existing metadata information156 from the cluster as query."""157 self.retry_params = Retry(self.show_similar_albums, cluster)158 self.cluster = cluster159 metadata = cluster.metadata160 query = {161 "artist": metadata["albumartist"],162 "release": metadata["album"],163 "tracks": str(len(cluster.files))164 }165 # Generate query to be displayed to the user (in search box).166 # If advanced query syntax setting is enabled by user, display query in167 # advanced syntax style. Otherwise display only album title.168 if config.setting["use_adv_search_syntax"]:169 query_str = ' '.join(['%s:(%s)' % (item, escape_lucene_query(value))170 for item, value in query.items() if value])171 else:172 query_str = query["release"]173 query["limit"] = QUERY_LIMIT174 self.search_box_text(query_str)175 self.show_progress()176 self.tagger.mb_api.find_releases(177 self.handle_reply,178 **query)179 def retry(self):180 self.retry_params.function(self.retry_params.query)181 def handle_reply(self, document, http, error):182 if error:183 self.network_error(http, error)184 return185 try:186 releases = document['releases']187 except (KeyError, TypeError):188 self.no_results_found()189 return190 del self.search_results[:]191 self.parse_releases(releases)192 self.display_results()193 self.fetch_coverarts()194 def fetch_coverarts(self):195 if self.fetching:196 return197 self.fetching = True198 for cell in self.cover_cells:199 self.fetch_coverart(cell)200 self.fetching = False201 def fetch_coverart(self, cell):202 """Queue cover art jsons from CAA server for each album in search203 results.204 """205 if cell.fetched:206 return207 if not cell.is_visible():208 return209 cell.fetched = True210 caa_path = "/release/%s" % cell.release["musicbrainz_albumid"]211 cell.fetch_task = self.tagger.webservice.get(212 CAA_HOST,213 CAA_PORT,214 caa_path,215 partial(self._caa_json_downloaded, cell)216 )217 def _caa_json_downloaded(self, cover_cell, data, http, error):218 """Handle json reply from CAA server.219 If server replies without error, try to get small thumbnail of front220 coverart of the release.221 """222 cover_cell.fetch_task = None223 if error:224 cover_cell.not_found()225 return226 front = None227 try:228 for image in data["images"]:229 if image["front"]:230 front = image231 break232 if front:233 url = front["thumbnails"]["small"]234 coverartimage = CaaThumbnailCoverArtImage(url=url)235 cover_cell.fetch_task = self.tagger.webservice.download(236 coverartimage.host,237 coverartimage.port,238 coverartimage.path,239 partial(self._cover_downloaded, cover_cell)240 )241 else:242 cover_cell.not_found()243 except (AttributeError, KeyError, TypeError):244 log.error("Error reading CAA response", exc_info=True)245 cover_cell.not_found()246 def _cover_downloaded(self, cover_cell, data, http, error):247 """Handle cover art query reply from CAA server.248 If server returns the cover image successfully, update the cover art249 cell of particular release.250 Args:251 row -- Album's row in results table252 """253 cover_cell.fetch_task = None254 if error:255 cover_cell.not_found()256 else:257 pixmap = QtGui.QPixmap()258 try:259 pixmap.loadFromData(data)260 cover_cell.set_pixmap(pixmap)261 except Exception as e:262 cover_cell.not_found()263 log.error(e)264 def fetch_cleanup(self):265 for cell in self.cover_cells:266 if cell.fetch_task is not None:267 log.debug("Removing cover art fetch task for %s",268 cell.release['musicbrainz_albumid'])269 self.tagger.webservice.remove_task(cell.fetch_task)270 def closeEvent(self, event):271 if self.cover_cells:272 self.fetch_cleanup()273 super().closeEvent(event)274 def parse_releases(self, releases):275 for node in releases:276 release = Metadata()277 release_to_metadata(node, release)278 release['score'] = node['score']279 rg_node = node['release-group']280 release_group_to_metadata(rg_node, release)281 if "media" in node:282 media = node['media']283 release["format"] = media_formats_from_node(media)284 release["tracks"] = node['track-count']285 countries = countries_from_node(node)286 if countries:287 release["country"] = ", ".join(countries)288 self.search_results.append(release)289 def display_results(self):290 self.prepare_table()291 self.cover_cells = []292 for row, release in enumerate(self.search_results):293 self.table.insertRow(row)294 self.set_table_item(row, 'name', release, "album")295 self.set_table_item(row, 'artist', release, "albumartist")296 self.set_table_item(row, 'format', release, "format")297 self.set_table_item(row, 'tracks', release, "tracks")298 self.set_table_item(row, 'date', release, "date")299 self.set_table_item(row, 'country', release, "country")300 self.set_table_item(row, 'labels', release, "label")301 self.set_table_item(row, 'catnums', release, "catalognumber")302 self.set_table_item(row, 'barcode', release, "barcode")303 self.set_table_item(row, 'language', release, "~releaselanguage")304 self.set_table_item(row, 'type', release, "releasetype")305 self.set_table_item(row, 'status', release, "releasestatus")306 self.set_table_item(row, 'score', release, "score")307 self.cover_cells.append(CoverCell(self, release, row, 'cover',308 on_show=self.fetch_coverart))309 self.show_table(sort_column='score')310 def accept_event(self, rows):311 for row in rows:312 self.load_selection(row)313 def load_selection(self, row):314 release = self.search_results[row]315 self.tagger.get_release_group_by_id(316 release["musicbrainz_releasegroupid"]).loaded_albums.add(317 release["musicbrainz_albumid"])318 album = self.tagger.load_album(release["musicbrainz_albumid"])319 if self.cluster:320 files = self.tagger.get_files_from_objects([self.cluster])321 self.tagger.move_files_to_album(files, release["musicbrainz_albumid"],...
test_combinationMatrix.py
Source:test_combinationMatrix.py
...44 solution_row = [1, 0, 2, 3]45 excepted_uncovered = 5746 excepted_covered_more_than_ones = 047 excepted_uncovered_row_size = 648 self.excepted_hash_table[0, 1].cover_cell((1, 0))49 self.excepted_hash_table[0, 2].cover_cell((1, 2))50 self.excepted_hash_table[0, 3].cover_cell((1, 3))51 self.excepted_hash_table[1, 2].cover_cell((0, 2))52 self.excepted_hash_table[1, 3].cover_cell((0, 3))53 self.excepted_hash_table[2, 3].cover_cell((2, 3))54 self.matrix.cover_solution_row(solution_row)55 self.assertEqual(excepted_uncovered, self.matrix.total_uncovered, "Total uncovered number is wrong.")56 self.assertEqual(excepted_covered_more_than_ones, self.matrix.total_covered_more_than_ones,57 "Total uovered_more_than_ones number is wrong.")58 self.assertEqual(excepted_uncovered_row_size, len(self.matrix.uncovered_rows),59 "Matrix has wrong uncovered row size")60 for key in self.matrix.hash_table:61 with self.subTest(combination=key):62 self.assertTrue(combination_row_equals(self.matrix.hash_table[key], self.excepted_hash_table[key]))63 solution_row = [0, 0, 2, 3]64 self.matrix.cover_solution_row(solution_row)65 solution_row = [0, 1, 2, 3]66 self.matrix.cover_solution_row(solution_row)67 solution_row = [0, 2, 2, 3]68 self.matrix.cover_solution_row(solution_row)69 solution_row = [1, 1, 2, 3]70 self.matrix.cover_solution_row(solution_row)71 solution_row = [1, 2, 2, 3]72 self.matrix.cover_solution_row(solution_row)73 solution_row = [2, 0, 2, 3]74 self.matrix.cover_solution_row(solution_row)75 solution_row = [2, 1, 2, 3]76 self.matrix.cover_solution_row(solution_row)77 solution_row = [2, 2, 2, 3]78 self.matrix.cover_solution_row(solution_row)79 excepted_uncovered_row_size = 580 excepted_uncovered = 4181 excepted_covered_more_than_ones = 1382 self.assertEqual(excepted_uncovered, self.matrix.total_uncovered, "Total uncovered number is wrong.")83 self.assertEqual(excepted_covered_more_than_ones, self.matrix.total_covered_more_than_ones,84 "Total uovered_more_than_ones number is wrong.")85 self.assertEqual(excepted_uncovered_row_size, len(self.matrix.uncovered_rows),86 "Matrix has wrong uncovered row size")87 def test_cover_combination(self):88 solution_row = [1, 0, 2, 3]89 self.matrix.cover_combination(solution_row, (1, 0))90 excepted_uncovered_row_size = 691 excepted_uncovered = 5892 excepted_covered_more_than_ones = 093 self.excepted_hash_table[0, 1].cover_cell((1, 0))94 self.excepted_hash_table[0, 2].cover_cell((1, 2))95 self.excepted_hash_table[0, 3].cover_cell((1, 3))96 self.excepted_hash_table[1, 2].cover_cell((0, 2))97 self.excepted_hash_table[1, 3].cover_cell((0, 3))98 self.assertEqual(excepted_uncovered, self.matrix.total_uncovered, "Total uncovered number is wrong.")99 self.assertEqual(excepted_covered_more_than_ones, self.matrix.total_covered_more_than_ones,100 "Total uovered_more_than_ones number is wrong.")101 self.assertEqual(excepted_uncovered_row_size, len(self.matrix.uncovered_rows),102 "Matrix has wrong uncovered row size")103 for key in self.matrix.hash_table:104 with self.subTest(combination=key):105 self.assertTrue(combination_row_equals(self.matrix.hash_table[key], self.excepted_hash_table[key]))106 self.matrix.cover_combination(solution_row, (1, 0))107 excepted_covered_more_than_ones = 5108 self.assertEqual(excepted_uncovered, self.matrix.total_uncovered, "Total uncovered number is wrong.")109 self.assertEqual(excepted_covered_more_than_ones, self.matrix.total_covered_more_than_ones,110 "Total uovered_more_than_ones number is wrong.")111 self.assertEqual(excepted_uncovered_row_size, len(self.matrix.uncovered_rows),112 "Matrix has wrong uncovered row size")113 def test_uncover_solution_row(self):114 solution_row = [1, 0, 2, 3]115 self.matrix.cover_solution_row(solution_row)116 self.matrix.uncover_solution_row(solution_row)117 excepted_uncovered_row_size = 6118 excepted_uncovered = 63119 excepted_covered_more_than_ones = 0120 self.assertEqual(excepted_uncovered, self.matrix.total_uncovered, "Total uncovered number is wrong.")121 self.assertEqual(excepted_covered_more_than_ones, self.matrix.total_covered_more_than_ones,122 "Total uovered_more_than_ones number is wrong.")123 self.assertEqual(excepted_uncovered_row_size, len(self.matrix.uncovered_rows),124 "Matrix has wrong uncovered row size")125 for key in self.matrix.hash_table:126 with self.subTest(combination=key):127 self.assertTrue(combination_row_equals(self.matrix.hash_table[key], self.excepted_hash_table[key]))128 self.matrix.cover_solution_row(solution_row)129 self.matrix.cover_solution_row(solution_row)130 self.excepted_hash_table[0, 1].cover_cell((1, 0))131 self.excepted_hash_table[0, 2].cover_cell((1, 2))132 self.excepted_hash_table[0, 3].cover_cell((1, 3))133 self.excepted_hash_table[1, 2].cover_cell((0, 2))134 self.excepted_hash_table[1, 3].cover_cell((0, 3))135 self.excepted_hash_table[2, 3].cover_cell((2, 3))136 self.matrix.uncover_solution_row(solution_row)137 excepted_uncovered_row_size = 6138 excepted_uncovered = 57139 excepted_covered_more_than_ones = 0140 self.assertEqual(excepted_uncovered, self.matrix.total_uncovered, "Total uncovered number is wrong.")141 self.assertEqual(excepted_covered_more_than_ones, self.matrix.total_covered_more_than_ones,142 "Total uovered_more_than_ones number is wrong.")143 self.assertEqual(excepted_uncovered_row_size, len(self.matrix.uncovered_rows),144 "Matrix has wrong uncovered row size")145 for key in self.matrix.hash_table:146 with self.subTest(combination=key):147 self.assertTrue(combination_row_equals(self.matrix.hash_table[key], self.excepted_hash_table[key]))148 def test_uncover_combination(self):149 solution_row = [1, 0, 2, 3]150 self.matrix.cover_solution_row(solution_row)151 excepted_uncovered_row_size = 6152 excepted_uncovered = 62153 excepted_covered_more_than_ones = 0154 self.excepted_hash_table[2, 3].cover_cell((2, 3))155 self.matrix.uncover_combination(solution_row, (0, 1))156 self.assertEqual(excepted_uncovered, self.matrix.total_uncovered, "Total uncovered number is wrong.")157 self.assertEqual(excepted_covered_more_than_ones, self.matrix.total_covered_more_than_ones,158 "Total uovered_more_than_ones number is wrong.")159 self.assertEqual(excepted_uncovered_row_size, len(self.matrix.uncovered_rows),160 "Matrix has wrong uncovered row size")161 for key in self.matrix.hash_table:162 with self.subTest(combination=key):163 self.assertTrue(combination_row_equals(self.matrix.hash_table[key], self.excepted_hash_table[key]))164 def test_uncover(self):165 solution_row = [1, 0, 2, 3]166 self.matrix.cover_solution_row(solution_row)167 self.matrix.cover_solution_row(solution_row)168 excepted_uncovered_row_size = 6...
test_combinationRow.py
Source:test_combinationRow.py
...25 self.t_value = 226 self.row = CombinationRow(self.data, self.t_value, self.parameters)27 # Tests of cover_cell function28 def test_cover_cell_uncovered_value(self):29 self.assertEqual(self.row.cover_cell((0, 0)), (-1, 0), "cover_cell return wrong values")30 self.assertEqual(self.row.uncovered, 11, "cover_cell create wrong uncovered value")31 self.assertEqual(self.row.covered_more_than_ones, 0, "cover_cell create wrong covered_more_than_ones value")32 self.assertEqual(self.row.hash_table[(0, 0)], 1, "cover_cell don't cover values")33 def test_cover_cell_covered_value(self):34 self.row.hash_table[(0, 0)] = 135 self.row.uncovered = 1136 self.assertEqual(self.row.cover_cell((0, 0)), (0, 1), "cover_cell return wrong values")37 self.assertEqual(self.row.uncovered, 11, "cover_cell create wrong uncovered value")38 self.assertEqual(self.row.covered_more_than_ones, 1, "cover_cell create wrong covered_more_than_ones value")39 self.assertEqual(self.row.hash_table[(0, 0)], 2, "cover_cell don't cover values")40 def test_cover_cell_cover_disabled_value(self):41 self.row.hash_table[(0, 0)] = None42 self.assertEqual(self.row.cover_cell((0, 0)), (0, 0), "cover_cell return wrong values")43 self.assertEqual(self.row.uncovered, 12, "cover_cell create wrong uncovered value")44 self.assertEqual(self.row.covered_more_than_ones, 0, "cover_cell create wrong covered_more_than_ones value")45 self.assertEqual(self.row.hash_table[(0, 0)], None, "cover_cell change disabled value")46 # Tests of uncover_cell function47 def test_uncover_cell_uncovered_value(self):48 self.assertEqual(self.row.uncover_cell((0, 0)), (0, 0), "uncover_cell return wrong values")49 self.assertEqual(self.row.uncovered, 12, "uncover_cell create wrong uncovered value")50 self.assertEqual(self.row.covered_more_than_ones, 0, "uncover_cell create wrong covered_more_than_ones value")51 self.assertEqual(self.row.hash_table[(0, 0)], 0, "uncover_cell change uncovered value")52 def test_uncover_cell_covered_value(self):53 self.row.hash_table[(0, 0)] = 154 self.row.uncovered = 1155 self.assertEqual(self.row.uncover_cell((0, 0)), (1, 0), "uncover_cell return wrong values")56 self.assertEqual(self.row.uncovered, 12, "uncover_cell create wrong uncovered value")57 self.assertEqual(self.row.covered_more_than_ones, 0, "uncover_cell create wrong covered_more_than_ones value")58 self.assertEqual(self.row.hash_table[(0, 0)], 0, "uncover_cell change uncovered value")59 def test_uncover_cell_covered_more_than_one_value(self):60 self.row.hash_table[(0, 0)] = 261 self.row.uncovered = 1162 self.row.covered_more_than_ones = 163 self.assertEqual(self.row.uncover_cell((0, 0)), (0, -1), "uncover_cell return wrong values")64 self.assertEqual(self.row.uncovered, 11, "uncover_cell create wrong uncovered value")65 self.assertEqual(self.row.covered_more_than_ones, 0, "uncover_cell create wrong covered_more_than_ones value")66 self.assertEqual(self.row.hash_table[(0, 0)], 1, "uncover_cell change uncovered value")67 def test_uncover_cell_disabled_value(self):68 self.row.hash_table[(0, 0)] = None69 self.assertEqual(self.row.uncover_cell((0, 0)), (0, 0), "uncover_cell return wrong values")70 self.assertEqual(self.row.uncovered, 12, "uncover_cell create wrong uncovered value")71 self.assertEqual(self.row.covered_more_than_ones, 0, "uncover_cell create wrong covered_more_than_ones value")72 self.assertEqual(self.row.hash_table[(0, 0)], None, "uncover_cell change disabled value")73 # Test of completely_uncover function74 def test_completely_uncover(self):75 self.row.hash_table[(0, 0)] = 176 self.row.hash_table[(0, 1)] = 277 self.row.hash_table[(0, 2)] = None78 self.row.completely_uncover()79 self.assertEqual(self.row.uncovered, 11, "completely_uncover create wrong uncovered value")80 self.assertEqual(self.row.covered_more_than_ones, 0,81 "completely_uncover create wrong covered_more_than_ones value")82 self.assertEqual(self.row.hash_table[(0, 0)], 0, "completely_uncover don't uncover value")83 self.assertEqual(self.row.hash_table[(0, 1)], 0, "completely_uncover don't uncover value")...
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!!