From c6c62bdcad6c79f90ca6c7e0558f3a2596103227 Mon Sep 17 00:00:00 2001 From: M2j Date: Sun, 29 Apr 2012 17:31:56 +0200 Subject: [PATCH] pythonifying code: - replace has_key() by key in dict - remove len() method from sequence and mapping types when mapping to bool --- openlp/core/theme/theme.py | 2 +- openlp/core/ui/mainwindow.py | 2 +- openlp/core/ui/media/mediacontroller.py | 4 +- openlp/core/ui/servicemanager.py | 6 +-- openlp/core/ui/shortcutlistform.py | 12 ++--- openlp/core/ui/slidecontroller.py | 4 +- openlp/core/ui/thememanager.py | 2 +- openlp/core/utils/__init__.py | 2 +- openlp/core/utils/actions.py | 4 +- openlp/plugins/alerts/forms/alertform.py | 2 +- openlp/plugins/bibles/bibleplugin.py | 4 +- .../plugins/bibles/forms/bibleupgradeform.py | 9 ++-- openlp/plugins/bibles/lib/__init__.py | 4 +- openlp/plugins/bibles/lib/http.py | 2 +- openlp/plugins/bibles/lib/mediaitem.py | 6 +-- openlp/plugins/custom/forms/editcustomform.py | 2 +- openlp/plugins/custom/lib/mediaitem.py | 2 +- openlp/plugins/songs/forms/editverseform.py | 6 +-- .../songs/forms/songmaintenanceform.py | 4 +- openlp/plugins/songs/lib/__init__.py | 2 +- openlp/plugins/songs/lib/cclifileimport.py | 2 +- openlp/plugins/songs/lib/easyslidesimport.py | 44 ++++++------------- openlp/plugins/songs/lib/ewimport.py | 10 ++--- openlp/plugins/songs/lib/mediaitem.py | 17 ++++--- openlp/plugins/songs/lib/olp1import.py | 5 +-- openlp/plugins/songs/lib/opensongimport.py | 24 ++++------ openlp/plugins/songs/lib/songimport.py | 15 +++---- .../plugins/songs/lib/songshowplusimport.py | 2 +- openlp/plugins/songs/lib/xml.py | 2 +- openlp/plugins/songs/songsplugin.py | 2 +- 30 files changed, 87 insertions(+), 117 deletions(-) diff --git a/openlp/core/theme/theme.py b/openlp/core/theme/theme.py index 90ca6cf6c..a77ab0c54 100644 --- a/openlp/core/theme/theme.py +++ b/openlp/core/theme/theme.py @@ -209,7 +209,7 @@ class Theme(object): val = int(element_text[1:], 16) except ValueError: # nope pass - elif DELPHI_COLORS.has_key(element_text): + elif element_text in DELPHI_COLORS: val = DELPHI_COLORS[element_text] delphi_color_change = True else: diff --git a/openlp/core/ui/mainwindow.py b/openlp/core/ui/mainwindow.py index 0ebf66aab..e4a4e1616 100644 --- a/openlp/core/ui/mainwindow.py +++ b/openlp/core/ui/mainwindow.py @@ -730,7 +730,7 @@ class MainWindow(QtGui.QMainWindow, Ui_MainWindow): if self.liveController.display.isVisible(): self.liveController.display.setFocus() self.activateWindow() - if len(self.arguments): + if self.arguments: args = [] for a in self.arguments: args.extend([a]) diff --git a/openlp/core/ui/media/mediacontroller.py b/openlp/core/ui/media/mediacontroller.py index 7ccbd9245..73283ea9b 100644 --- a/openlp/core/ui/media/mediacontroller.py +++ b/openlp/core/ui/media/mediacontroller.py @@ -127,7 +127,7 @@ class MediaController(object): invalidMediaPlayers = [mediaPlayer for mediaPlayer in savedPlayers \ if not mediaPlayer in self.mediaPlayers or \ not self.mediaPlayers[mediaPlayer].check_available()] - if len(invalidMediaPlayers) > 0: + if invalidMediaPlayers: for invalidPlayer in invalidMediaPlayers: savedPlayers.remove(invalidPlayer) set_media_players(savedPlayers, overriddenPlayer) @@ -141,7 +141,7 @@ class MediaController(object): Check if there is a running media Player and do updating stuff (e.g. update the UI) """ - if len(self.curDisplayMediaPlayer.keys()) == 0: + if not self.curDisplayMediaPlayer: self.timer.stop() else: for display in self.curDisplayMediaPlayer.keys(): diff --git a/openlp/core/ui/servicemanager.py b/openlp/core/ui/servicemanager.py index deb2b306f..c9dfeae50 100644 --- a/openlp/core/ui/servicemanager.py +++ b/openlp/core/ui/servicemanager.py @@ -483,7 +483,7 @@ class ServiceManager(QtGui.QWidget): service_item = item[u'service_item'].get_service_repr() # Get all the audio files, and ready them for embedding in the # service file. - if len(service_item[u'header'][u'background_audio']) > 0: + if service_item[u'header'][u'background_audio']: for i, filename in \ enumerate(service_item[u'header'][u'background_audio']): new_file = os.path.join(u'audio', @@ -822,7 +822,7 @@ class ServiceManager(QtGui.QWidget): """ Called by the SlideController to select the next service item. """ - if len(self.serviceManagerList.selectedItems()) == 0: + if not self.serviceManagerList.selectedItems(): return selected = self.serviceManagerList.selectedItems()[0] lookFor = 0 @@ -840,7 +840,7 @@ class ServiceManager(QtGui.QWidget): """ Called by the SlideController to select the previous service item. """ - if len(self.serviceManagerList.selectedItems()) == 0: + if not self.serviceManagerList.selectedItems(): return selected = self.serviceManagerList.selectedItems()[0] prevItem = None diff --git a/openlp/core/ui/shortcutlistform.py b/openlp/core/ui/shortcutlistform.py index 211946793..457525dc0 100644 --- a/openlp/core/ui/shortcutlistform.py +++ b/openlp/core/ui/shortcutlistform.py @@ -151,7 +151,7 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog): if action is None: continue shortcuts = self._actionShortcuts(action) - if len(shortcuts) == 0: + if not shortcuts: item.setText(1, u'') item.setText(2, u'') elif len(shortcuts) == 1: @@ -195,7 +195,7 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog): return shortcuts = self._actionShortcuts(action) new_shortcuts = [] - if len(shortcuts) != 0: + if shortcuts: new_shortcuts.append(shortcuts[0]) new_shortcuts.append( QtGui.QKeySequence(self.alternatePushButton.text())) @@ -241,7 +241,7 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog): self.primaryPushButton.setChecked(False) self.alternatePushButton.setChecked(False) else: - if len(action.defaultShortcuts) != 0: + if action.defaultShortcuts: primary_label_text = action.defaultShortcuts[0].toString() if len(action.defaultShortcuts) == 2: alternate_label_text = action.defaultShortcuts[1].toString() @@ -313,7 +313,7 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog): self.refreshShortcutList() primary_button_text = u'' alternate_button_text = u'' - if len(temp_shortcuts) != 0: + if temp_shortcuts: primary_button_text = temp_shortcuts[0].toString() if len(temp_shortcuts) == 2: alternate_button_text = temp_shortcuts[1].toString() @@ -363,7 +363,7 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog): return shortcuts = self._actionShortcuts(action) new_shortcuts = [] - if len(action.defaultShortcuts) != 0: + if action.defaultShortcuts: new_shortcuts.append(action.defaultShortcuts[0]) # We have to check if the primary default shortcut is available. But # we only have to check, if the action has a default primary @@ -391,7 +391,7 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog): return shortcuts = self._actionShortcuts(action) new_shortcuts = [] - if len(shortcuts) != 0: + if shortcuts: new_shortcuts.append(shortcuts[0]) if len(action.defaultShortcuts) == 2: new_shortcuts.append(action.defaultShortcuts[1]) diff --git a/openlp/core/ui/slidecontroller.py b/openlp/core/ui/slidecontroller.py index a1b3a84b2..9de6555f2 100644 --- a/openlp/core/ui/slidecontroller.py +++ b/openlp/core/ui/slidecontroller.py @@ -555,7 +555,7 @@ class SlideController(Controller): Process the service item request queue. The key presses can arrive faster than the processing so implement a FIFO queue. """ - if len(self.keypress_queue): + if self.keypress_queue: while len(self.keypress_queue) and not self.keypress_loop: self.keypress_loop = True keypressCommand = self.keypress_queue.popleft() @@ -694,7 +694,7 @@ class SlideController(Controller): if item.is_text(): if QtCore.QSettings().value( self.parent().songsSettingsSection + u'/display songbar', - QtCore.QVariant(True)).toBool() and len(self.slideList) > 0: + QtCore.QVariant(True)).toBool() and self.slideList: self.songMenu.show() if item.is_capable(ItemCapabilities.CanLoop) and \ len(item.get_frames()) > 1: diff --git a/openlp/core/ui/thememanager.py b/openlp/core/ui/thememanager.py index 421346ba9..665c435b9 100644 --- a/openlp/core/ui/thememanager.py +++ b/openlp/core/ui/thememanager.py @@ -444,7 +444,7 @@ class ThemeManager(QtGui.QWidget): self.firstTime() files = SettingsManager.get_files(self.settingsSection, u'.png') # No themes have been found so create one - if len(files) == 0: + if not files: theme = ThemeXML() theme.theme_name = UiStrings().Default self._writeTheme(theme, None, None) diff --git a/openlp/core/utils/__init__.py b/openlp/core/utils/__init__.py index 1fc75b6d8..f71ec25cd 100644 --- a/openlp/core/utils/__init__.py +++ b/openlp/core/utils/__init__.py @@ -267,7 +267,7 @@ def get_application_version(): if code != 0: raise Exception(u'Error running bzr tags') lines = output.splitlines() - if len(lines) == 0: + if not lines: tag = u'0.0.0' revision = u'0' else: diff --git a/openlp/core/utils/actions.py b/openlp/core/utils/actions.py index 1a7d99fa5..271a7c884 100644 --- a/openlp/core/utils/actions.py +++ b/openlp/core/utils/actions.py @@ -90,7 +90,7 @@ class CategoryActionList(object): def append(self, name): weight = 0 - if len(self.actions) > 0: + if self.actions: weight = self.actions[-1][0] + 1 self.add(name, weight) @@ -156,7 +156,7 @@ class CategoryList(object): def append(self, name, actions=None): weight = 0 - if len(self.categories) > 0: + if self.categories: weight = self.categories[-1].weight + 1 if actions: self.add(name, weight, actions) diff --git a/openlp/plugins/alerts/forms/alertform.py b/openlp/plugins/alerts/forms/alertform.py index d2d4813a1..4f7633062 100644 --- a/openlp/plugins/alerts/forms/alertform.py +++ b/openlp/plugins/alerts/forms/alertform.py @@ -101,7 +101,7 @@ class AlertForm(QtGui.QDialog, Ui_AlertDialog): self.alertTextEdit.setText(u'') def onNewClick(self): - if len(self.alertTextEdit.text()) == 0: + if not self.alertTextEdit.text(): QtGui.QMessageBox.information(self, translate('AlertsPlugin.AlertForm', 'New Alert'), translate('AlertsPlugin.AlertForm', 'You haven\'t specified ' diff --git a/openlp/plugins/bibles/bibleplugin.py b/openlp/plugins/bibles/bibleplugin.py index 06d8b1c98..0dddab214 100644 --- a/openlp/plugins/bibles/bibleplugin.py +++ b/openlp/plugins/bibles/bibleplugin.py @@ -62,7 +62,7 @@ class BiblePlugin(Plugin): # unicode(UiStrings().Export)) # Set to invisible until we can export bibles self.exportBibleItem.setVisible(False) - if len(self.manager.old_bible_databases): + if self.manager.old_bible_databases: self.toolsUpgradeItem.setVisible(True) def finalise(self): @@ -83,7 +83,7 @@ class BiblePlugin(Plugin): """ Perform tasks on application startup """ - if len(self.manager.old_bible_databases): + if self.manager.old_bible_databases: if QtGui.QMessageBox.information(self.formParent, translate('OpenLP', 'Information'), translate('OpenLP', 'Bible format has changed.\nYou have to upgrade your ' diff --git a/openlp/plugins/bibles/forms/bibleupgradeform.py b/openlp/plugins/bibles/forms/bibleupgradeform.py index 0ef418de8..88bbd8c63 100644 --- a/openlp/plugins/bibles/forms/bibleupgradeform.py +++ b/openlp/plugins/bibles/forms/bibleupgradeform.py @@ -426,8 +426,7 @@ class BibleUpgradeForm(OpenLPWizard): if meta[u'key'] == u'download_source': web_bible = True self.includeWebBible = True - if meta.has_key(u'proxy_server'): - proxy_server = meta[u'proxy_server'] + proxy_server = meta.get(u'proxy_server') if web_bible: if meta_data[u'download_source'].lower() == u'crosswalk': handler = CWExtract(proxy_server) @@ -572,7 +571,7 @@ class BibleUpgradeForm(OpenLPWizard): int(verse[u'verse']), unicode(verse[u'text'])) Receiver.send_message(u'openlp_process_events') self.newbibles[number].session.commit() - if self.success.has_key(number) and not self.success[number]: + if not self.success.get(number, True): self.incrementProgressBar(unicode(translate( 'BiblesPlugin.UpgradeWizardForm', 'Upgrading Bible %s of %s: "%s"\nFailed')) % @@ -586,7 +585,7 @@ class BibleUpgradeForm(OpenLPWizard): 'Upgrading Bible %s of %s: "%s"\n' 'Complete')) % (number + 1, max_bibles, name)) - if self.newbibles.has_key(number): + if number in self.newbibles: self.newbibles[number].session.close() # Close the last bible's connection if possible. if old_bible is not None: @@ -599,7 +598,7 @@ class BibleUpgradeForm(OpenLPWizard): successful_import = 0 failed_import = 0 for number, filename in enumerate(self.files): - if self.success.has_key(number) and self.success[number]: + if self.success.get(number): successful_import += 1 elif self.checkBox[number].checkState() == QtCore.Qt.Checked: failed_import += 1 diff --git a/openlp/plugins/bibles/lib/__init__.py b/openlp/plugins/bibles/lib/__init__.py index 4f8582bdf..6cd8b8d8e 100644 --- a/openlp/plugins/bibles/lib/__init__.py +++ b/openlp/plugins/bibles/lib/__init__.py @@ -236,7 +236,7 @@ def get_reference_separator(separator_type): ``separator_type`` The role and format of the separator. """ - if len(REFERENCE_SEPARATORS) == 0: + if not REFERENCE_SEPARATORS: update_reference_separators() return REFERENCE_SEPARATORS[separator_type] @@ -247,7 +247,7 @@ def get_reference_match(match_type): ``match_type`` The type of match is ``range_separator``, ``range`` or ``full``. """ - if len(REFERENCE_MATCHES) == 0: + if not REFERENCE_MATCHES: update_reference_separators() return REFERENCE_MATCHES[match_type] diff --git a/openlp/plugins/bibles/lib/http.py b/openlp/plugins/bibles/lib/http.py index 302bdf999..fb79b26e1 100644 --- a/openlp/plugins/bibles/lib/http.py +++ b/openlp/plugins/bibles/lib/http.py @@ -106,7 +106,7 @@ class BGExtract(object): verse_list = {} # Cater for inconsistent mark up in the first verse of a chapter. first_verse = verses.find(u'versenum') - if first_verse and len(first_verse.contents): + if first_verse and first_verse.contents: verse_list[1] = unicode(first_verse.contents[0]) for verse in verses(u'sup', u'versenum'): raw_verse_num = verse.next diff --git a/openlp/plugins/bibles/lib/mediaitem.py b/openlp/plugins/bibles/lib/mediaitem.py index e3d173fad..bc456ee69 100644 --- a/openlp/plugins/bibles/lib/mediaitem.py +++ b/openlp/plugins/bibles/lib/mediaitem.py @@ -392,7 +392,7 @@ class BibleMediaItem(MediaManagerItem): if bible in bibles: find_and_set_in_combo_box(self.advancedVersionComboBox, bible) self.initialiseAdvancedBible(unicode(bible)) - elif len(bibles): + elif bibles: self.initialiseAdvancedBible(bibles[0]) bible = QtCore.QSettings().value( self.settingsSection + u'/quick bible', QtCore.QVariant( @@ -878,7 +878,7 @@ class BibleMediaItem(MediaManagerItem): items = item else: items = self.listView.selectedItems() - if len(items) == 0: + if not items: return False bible_text = u'' old_item = None @@ -949,7 +949,7 @@ class BibleMediaItem(MediaManagerItem): # Service Item: Title service_item.title = create_separated_list(raw_title) # Service Item: Theme - if len(self.settings.bible_theme) == 0: + if not self.settings.bible_theme: service_item.theme = None else: service_item.theme = self.settings.bible_theme diff --git a/openlp/plugins/custom/forms/editcustomform.py b/openlp/plugins/custom/forms/editcustomform.py index 7c2da3bb0..a113a9e1b 100644 --- a/openlp/plugins/custom/forms/editcustomform.py +++ b/openlp/plugins/custom/forms/editcustomform.py @@ -254,7 +254,7 @@ class EditCustomForm(QtGui.QDialog, Ui_CustomEditDialog): Checks whether a custom is valid or not. """ # We must have a title. - if len(self.titleEdit.displayText()) == 0: + if not self.titleEdit.displayText(): self.titleEdit.setFocus() critical_error_message_box( message=translate('CustomPlugin.EditCustomForm', diff --git a/openlp/plugins/custom/lib/mediaitem.py b/openlp/plugins/custom/lib/mediaitem.py index 541403cda..af2f261ca 100644 --- a/openlp/plugins/custom/lib/mediaitem.py +++ b/openlp/plugins/custom/lib/mediaitem.py @@ -258,7 +258,7 @@ class CustomMediaItem(MediaManagerItem): search_length = 2 if len(text) > search_length: self.onSearchTextButtonClicked() - elif len(text) == 0: + elif not text: self.onClearTextButtonClick() def onClearTextButtonClick(self): diff --git a/openlp/plugins/songs/forms/editverseform.py b/openlp/plugins/songs/forms/editverseform.py index 1e7bf4375..21285f39d 100644 --- a/openlp/plugins/songs/forms/editverseform.py +++ b/openlp/plugins/songs/forms/editverseform.py @@ -191,13 +191,13 @@ class EditVerseForm(QtGui.QDialog, Ui_EditVerseDialog): else: log.debug(unicode(self.getVerse()[0]).split(u'\n')) value = unicode(self.getVerse()[0]).split(u'\n')[1] - if len(value) == 0: + if not value: lines = unicode(self.getVerse()[0]).split(u'\n') index = 2 - while index < len(lines) and len(value) == 0: + while index < len(lines) and not value: value = lines[index] index += 1 - if len(value) == 0: + if not value: critical_error_message_box( message=translate('SongsPlugin.EditSongForm', 'You need to type some text in to the verse.')) diff --git a/openlp/plugins/songs/forms/songmaintenanceform.py b/openlp/plugins/songs/forms/songmaintenanceform.py index 697bcb4fe..7ba49a102 100644 --- a/openlp/plugins/songs/forms/songmaintenanceform.py +++ b/openlp/plugins/songs/forms/songmaintenanceform.py @@ -108,7 +108,7 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog): item_id = self._getCurrentItemId(list_widget) if item_id != -1: item = self.manager.get_object(item_class, item_id) - if item and len(item.songs) == 0: + if item and not item.songs: if critical_error_message_box(dlg_title, del_text, self, True) == QtGui.QMessageBox.Yes: self.manager.delete_object(item_class, item.id) @@ -191,7 +191,7 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog): ``edit`` If we edit an item, this should be *True*. """ - if len(objects) > 0: + if objects: # If we edit an existing object, we need to make sure that we do # not return False when nothing has changed. if edit: diff --git a/openlp/plugins/songs/lib/__init__.py b/openlp/plugins/songs/lib/__init__.py index 91e21bb19..44cf8e113 100644 --- a/openlp/plugins/songs/lib/__init__.py +++ b/openlp/plugins/songs/lib/__init__.py @@ -316,7 +316,7 @@ def clean_song(manager, song): verse_type, verse[0][u'label'], verse[1], - verse[0][u'lang'] if verse[0].has_key(u'lang') else None + verse[0].get(u'lang') ) compare_order.append((u'%s%s' % (verse_type, verse[0][u'label']) ).upper()) diff --git a/openlp/plugins/songs/lib/cclifileimport.py b/openlp/plugins/songs/lib/cclifileimport.py index 6b99f9a16..c2df6ab4e 100644 --- a/openlp/plugins/songs/lib/cclifileimport.py +++ b/openlp/plugins/songs/lib/cclifileimport.py @@ -211,7 +211,7 @@ class CCLIFileImport(SongImport): elif verse_lines[0].startswith(u'('): verse_type = VerseType.Tags[VerseType.Other] verse_text = verse_lines[1] - if len(verse_text) > 0: + if verse_text: self.addVerse(verse_text, verse_type) check_first_verse_line = False # Handle multiple authors diff --git a/openlp/plugins/songs/lib/easyslidesimport.py b/openlp/plugins/songs/lib/easyslidesimport.py index 206c127f5..eb21e6fc2 100644 --- a/openlp/plugins/songs/lib/easyslidesimport.py +++ b/openlp/plugins/songs/lib/easyslidesimport.py @@ -162,15 +162,12 @@ class EasySlidesImport(SongImport): separatorlines = 0 for line in lines: line = line.strip() - if len(line) == 0: + if not line: continue elif line[1:7] == u'region': # this is region separator, probably [region 2] region = self._extractRegion(line) - if regionlines.has_key(region): - regionlines[region] = regionlines[region] + 1 - else: - regionlines[region] = 1 + regionlines[region] = 1 + regionlines.get(region, 0) elif line[0] == u'[': separatorlines = separatorlines + 1 # if the song has separators @@ -206,7 +203,7 @@ class EasySlidesImport(SongImport): for line in lines: line = line.strip() - if len(line) == 0: + if not line: if separators: # separators are used, so empty line means slide break # inside verse @@ -215,15 +212,11 @@ class EasySlidesImport(SongImport): else: # separators are not used, so empty line starts a new verse vt = u'V' - if verses[reg].has_key(vt): - vn = len(verses[reg][vt].keys())+1 - else: - vn = u'1' + vn = len(verses[reg].get(vt, {})) + 1 inst = 1 elif line[0:7] == u'[region': reg = self._extractRegion(line) - if not verses.has_key(reg): - verses[reg] = {} + verses.setdefault(reg, {}) if not regionsInVerses: vt = u'V' vn = u'1' @@ -238,12 +231,7 @@ class EasySlidesImport(SongImport): if match: marker = match.group(1).strip() vn = match.group(2) - if len(marker) == 0: - vt = u'V' - elif MarkTypes.has_key(marker): - vt = MarkTypes[marker] - else: - vt = u'O' + vt = MarkTypes.get(marker, u'O') if marker else u'V' if regionsInVerses: region = defaultregion inst = 1 @@ -252,14 +240,10 @@ class EasySlidesImport(SongImport): else: if not [reg, vt, vn, inst] in our_verse_order: our_verse_order.append([reg, vt, vn, inst]) - if not verses[reg].has_key(vt): - verses[reg][vt] = {} - if not verses[reg][vt].has_key(vn): - verses[reg][vt][vn] = {} - if not verses[reg][vt][vn].has_key(inst): - verses[reg][vt][vn][inst] = [] - words = self.tidyText(line) - verses[reg][vt][vn][inst].append(words) + verses[reg].setdefault(vt, {}) + verses[reg][vt].setdefault(vn, {}) + verses[reg][vt][vn].setdefault(inst, []) + verses[reg][vt][vn][inst].append(self.tidyText(line)) # done parsing versetags = [] @@ -286,11 +270,11 @@ class EasySlidesImport(SongImport): try: order = unicode(song.Sequence).strip().split(u',') for tag in order: - if len(tag) == 0: + if not tag: continue elif tag[0].isdigit(): tag = u'V' + tag - elif SeqTypes.has_key(tag.lower()): + elif tag.lower() in SeqTypes: tag = SeqTypes[tag.lower()] else: continue @@ -307,9 +291,7 @@ class EasySlidesImport(SongImport): def _listHas(self, lst, subitems): for subitem in subitems: - if isinstance(lst, dict) and lst.has_key(subitem): - lst = lst[subitem] - elif isinstance(lst, list) and subitem in lst: + if subitem in lst: lst = lst[subitem] else: return False diff --git a/openlp/plugins/songs/lib/ewimport.py b/openlp/plugins/songs/lib/ewimport.py index f17900fd5..d58734610 100644 --- a/openlp/plugins/songs/lib/ewimport.py +++ b/openlp/plugins/songs/lib/ewimport.py @@ -62,15 +62,15 @@ def strip_rtf(blob, encoding): if control: # for delimiters, set control to False if c == '{': - if len(control_word) > 0: + if control_word: depth += 1 control = False elif c == '}': - if len(control_word) > 0: + if control_word: depth -= 1 control = False elif c == '\\': - new_control = (len(control_word) > 0) + new_control = bool(control_word) control = False elif c.isspace(): control = False @@ -79,7 +79,7 @@ def strip_rtf(blob, encoding): if len(control_word) == 3 and control_word[0] == '\'': control = False if not control: - if len(control_word) == 0: + if not control_word: if c == '{' or c == '}' or c == '\\': clear_text.append(c) else: @@ -360,7 +360,7 @@ class EasyWorshipSongImport(SongImport): field_desc = self.fieldDescs[field_desc_index] # Return None in case of 'blank' entries if isinstance(field, str): - if len(field.rstrip('\0')) == 0: + if not field.rstrip('\0'): return None elif field == 0: return None diff --git a/openlp/plugins/songs/lib/mediaitem.py b/openlp/plugins/songs/lib/mediaitem.py index 2836e98ad..51158a954 100644 --- a/openlp/plugins/songs/lib/mediaitem.py +++ b/openlp/plugins/songs/lib/mediaitem.py @@ -295,9 +295,8 @@ class SongMediaItem(MediaManagerItem): log.debug(u'display results Book') self.listView.clear() for book in searchresults: - songs = sorted(book.songs, key=lambda song: int( - re.sub(r'[^0-9]', u' ', song.song_number).partition(' ')[0]) - if len(re.sub(r'[^\w]', ' ', song.song_number)) else 0) + songs = sorted(book.songs, key=lambda song: + int(re.match(r'[0-9]+', u'0' + song.song_number).group())) for song in songs: # Do not display temporary songs if song.temporary: @@ -331,7 +330,7 @@ class SongMediaItem(MediaManagerItem): search_length = 3 if len(text) > search_length: self.onSearchTextButtonClicked() - elif len(text) == 0: + elif not text: self.onClearTextButtonClick() def onImportClick(self): @@ -491,7 +490,7 @@ class SongMediaItem(MediaManagerItem): else: # Loop through the verse list and expand the song accordingly. for order in song.verse_order.lower().split(): - if len(order) == 0: + if not order: break for verse in verseList: if verse[0][u'type'][0].lower() == order[0] and \ @@ -530,7 +529,7 @@ class SongMediaItem(MediaManagerItem): u'authors': u', '.join(author_list)} service_item.xml_version = self.openLyrics.song_to_xml(song) # Add the audio file to the service item. - if len(song.media_files) > 0: + if song.media_files: service_item.add_capability(ItemCapabilities.HasBackgroundAudio) service_item.background_audio = \ [m.file_name for m in song.media_files] @@ -575,12 +574,12 @@ class SongMediaItem(MediaManagerItem): editId = song.id break # If there's any backing tracks, copy them over. - if len(item.background_audio) > 0: + if item.background_audio: self._updateBackgroundAudio(song, item) if add_song and self.addSongFromService: song = self.openLyrics.xml_to_song(item.xml_version) # If there's any backing tracks, copy them over. - if len(item.background_audio) > 0: + if item.background_audio: self._updateBackgroundAudio(song, item) editId = song.id self.onSearchTextButtonClicked() @@ -588,7 +587,7 @@ class SongMediaItem(MediaManagerItem): # Make sure we temporary import formatting tags. song = self.openLyrics.xml_to_song(item.xml_version, True) # If there's any backing tracks, copy them over. - if len(item.background_audio) > 0: + if item.background_audio: self._updateBackgroundAudio(song, item) editId = song.id temporary = True diff --git a/openlp/plugins/songs/lib/olp1import.py b/openlp/plugins/songs/lib/olp1import.py index 05095aa31..2d744bd2a 100644 --- a/openlp/plugins/songs/lib/olp1import.py +++ b/openlp/plugins/songs/lib/olp1import.py @@ -122,8 +122,7 @@ class OpenLP1SongImport(SongImport): cursor.execute( u'SELECT settingsid FROM songs WHERE songid = %s' % song_id) theme_id = cursor.fetchone()[0] - if themes.has_key(theme_id): - self.themeName = themes[theme_id] + self.themeName = themes.get(theme_id, u'') verses = lyrics.split(u'\n\n') for verse in verses: if verse.strip(): @@ -191,7 +190,7 @@ class OpenLP1SongImport(SongImport): # Detect charset by songs. cursor.execute(u'SELECT name FROM sqlite_master ' u'WHERE type = \'table\' AND name = \'tracks\'') - if len(cursor.fetchall()) > 0: + if cursor.fetchall(): cursor.execute(u'SELECT fulltrackname FROM tracks') tracks = cursor.fetchall() for track in tracks: diff --git a/openlp/plugins/songs/lib/opensongimport.py b/openlp/plugins/songs/lib/opensongimport.py index ad45ef22e..f78ba474b 100644 --- a/openlp/plugins/songs/lib/opensongimport.py +++ b/openlp/plugins/songs/lib/opensongimport.py @@ -174,7 +174,7 @@ class OpenSongImport(SongImport): if semicolon >= 0: this_line = this_line[:semicolon] this_line = this_line.strip() - if not len(this_line): + if not this_line: continue # skip guitar chords and page and column breaks if this_line.startswith(u'.') or this_line.startswith(u'---') \ @@ -197,15 +197,12 @@ class OpenSongImport(SongImport): # the verse tag verse_tag = content verse_num = u'1' - if len(verse_tag) == 0: - verse_index = 0 - else: - verse_index = VerseType.from_loose_input(verse_tag) + verse_index = VerseType.from_loose_input(verse_tag) \ + if verse_tag else 0 verse_tag = VerseType.Tags[verse_index] inst = 1 if [verse_tag, verse_num, inst] in our_verse_order \ - and verses.has_key(verse_tag) \ - and verses[verse_tag].has_key(verse_num): + and verse_num in verses.get(verse_tag, {}): inst = len(verses[verse_tag][verse_num]) + 1 continue # number at start of line.. it's verse number @@ -213,11 +210,9 @@ class OpenSongImport(SongImport): verse_num = this_line[0] this_line = this_line[1:].strip() our_verse_order.append([verse_tag, verse_num, inst]) - if not verses.has_key(verse_tag): - verses[verse_tag] = {} - if not verses[verse_tag].has_key(verse_num): - verses[verse_tag][verse_num] = {} - if not verses[verse_tag][verse_num].has_key(inst): + verses.setdefault(verse_tag, {}) + verses[verse_tag].setdefault(verse_num, {}) + if inst not in verses[verse_tag][verse_num]: verses[verse_tag][verse_num][inst] = [] our_verse_order.append([verse_tag, verse_num, inst]) # Tidy text and remove the ____s from extended words @@ -252,15 +247,14 @@ class OpenSongImport(SongImport): if match is not None: verse_tag = match.group(1) verse_num = match.group(2) - if not len(verse_tag): + if not verse_tag: verse_tag = VerseType.Tags[VerseType.Verse] else: # Assume it's no.1 if there are no digits verse_tag = verse_def verse_num = u'1' verse_def = u'%s%s' % (verse_tag, verse_num) - if verses.has_key(verse_tag) and \ - verses[verse_tag].has_key(verse_num): + if verse_num in verses.get(verse_tag, {}): self.verseOrderList.append(verse_def) else: log.info(u'Got order %s but not in verse tags, dropping' diff --git a/openlp/plugins/songs/lib/songimport.py b/openlp/plugins/songs/lib/songimport.py index 6fd9dd403..74767d793 100644 --- a/openlp/plugins/songs/lib/songimport.py +++ b/openlp/plugins/songs/lib/songimport.py @@ -61,9 +61,9 @@ class SongImport(QtCore.QObject): """ self.manager = manager QtCore.QObject.__init__(self) - if kwargs.has_key(u'filename'): + if u'filename' in kwargs: self.importSource = kwargs[u'filename'] - elif kwargs.has_key(u'filenames'): + elif u'filenames' in kwargs: self.importSource = kwargs[u'filenames'] else: raise KeyError(u'Keyword arguments "filename[s]" not supplied.') @@ -273,7 +273,7 @@ class SongImport(QtCore.QObject): Author not checked here, if no author then "Author unknown" is automatically added """ - if not self.title or not len(self.verses): + if not self.title or not self.verses: return False else: return True @@ -314,13 +314,10 @@ class SongImport(QtCore.QObject): verse_def = new_verse_def sxml.add_verse_to_lyrics(verse_tag, verse_def[1:], verse_text, lang) song.lyrics = unicode(sxml.extract_xml(), u'utf-8') - if not len(self.verseOrderList) and \ - self.verseOrderListGeneratedUseful: + if not self.verseOrderList and self.verseOrderListGeneratedUseful: self.verseOrderList = self.verseOrderListGenerated - for i, current_verse_def in enumerate(self.verseOrderList): - if verses_changed_to_other.has_key(current_verse_def): - self.verseOrderList[i] = \ - verses_changed_to_other[current_verse_def] + self.verseOrderList = map(lambda v: verses_changed_to_other.get(v, v), + self.verseOrderList) song.verse_order = u' '.join(self.verseOrderList) song.copyright = self.copyright song.comments = self.comments diff --git a/openlp/plugins/songs/lib/songshowplusimport.py b/openlp/plugins/songs/lib/songshowplusimport.py index 8d2af5514..f86e061ca 100644 --- a/openlp/plugins/songs/lib/songshowplusimport.py +++ b/openlp/plugins/songs/lib/songshowplusimport.py @@ -204,7 +204,7 @@ class SongShowPlusImport(SongImport): elif verse_type == "pre-chorus": verse_tag = VerseType.Tags[VerseType.PreChorus] else: - if not self.otherList.has_key(verse_name): + if verse_name not in self.otherList: if ignore_unique: return None self.otherCount = self.otherCount + 1 diff --git a/openlp/plugins/songs/lib/xml.py b/openlp/plugins/songs/lib/xml.py index 816742d11..fdcb1dd60 100644 --- a/openlp/plugins/songs/lib/xml.py +++ b/openlp/plugins/songs/lib/xml.py @@ -611,7 +611,7 @@ class OpenLyrics(object): text += u'{%s}' % element.get(u'name') # Some formattings may have only start tag. # Handle this case if element has no children and contains no text. - if len(element) == 0 and not element.text: + if not element and not element.text: use_endtag = False # Append text from element. if element.text: diff --git a/openlp/plugins/songs/songsplugin.py b/openlp/plugins/songs/songsplugin.py index f55ac7a5f..4d59186e5 100644 --- a/openlp/plugins/songs/songsplugin.py +++ b/openlp/plugins/songs/songsplugin.py @@ -239,7 +239,7 @@ class SongsPlugin(Plugin): for sfile in os.listdir(db_dir): if sfile.startswith(u'songs_') and sfile.endswith(u'.sqlite'): song_dbs.append(os.path.join(db_dir, sfile)) - if len(song_dbs) == 0: + if not song_dbs: return progress = QtGui.QProgressDialog(self.formParent) progress.setWindowModality(QtCore.Qt.WindowModal)