From bc20c4169639c85b1dba2055ec6d9d749363c4d3 Mon Sep 17 00:00:00 2001 From: Daniel Gonzalez Gasull Date: Wed, 24 Mar 2021 07:46:43 +0000 Subject: [PATCH] Style changes (from backport), small refactorings Backport from Electrum for small style changes. Also refactored two long lines and changed two lists to sets. --- contrib/sign_packages | 5 +-- electroncash/coinchooser.py | 2 +- electroncash/mnemonic.py | 2 +- electroncash/old_mnemonic.py | 6 ++-- electroncash/plot.py | 2 +- electroncash/tests/test_bitcoin.py | 2 +- electroncash_gui/qt/main_window.py | 12 +++---- electroncash_gui/qt/network_dialog.py | 12 +++---- electroncash_gui/qt/password_dialog.py | 2 +- electroncash_gui/qt/qrcodewidget.py | 2 +- electroncash_gui/qt/util.py | 4 +-- electroncash_gui/stdio.py | 12 +++---- electroncash_gui/text.py | 38 +++++++++++------------ electroncash_plugins/hw_wallet/cmdline.py | 2 +- electroncash_plugins/keepkey/keepkey.py | 8 ++--- electroncash_plugins/ledger/auth2fa.py | 4 +-- electroncash_plugins/ledger/ledger.py | 2 +- 17 files changed, 57 insertions(+), 60 deletions(-) diff --git a/contrib/sign_packages b/contrib/sign_packages index d11ef5fc31f2..b7c3a37c59b4 100755 --- a/contrib/sign_packages +++ b/contrib/sign_packages @@ -10,9 +10,6 @@ if __name__ == '__main__': for f in os.listdir('.'): if f.endswith('asc'): continue - os.system( "gpg --sign --armor --detach --passphrase \"%s\" %s"%(password, f) ) + os.system("gpg --sign --armor --detach --passphrase \"%s\" %s"%(password, f)) os.chdir("..") - - - diff --git a/electroncash/coinchooser.py b/electroncash/coinchooser.py index 798c11a7858c..51a05c1ccf3c 100644 --- a/electroncash/coinchooser.py +++ b/electroncash/coinchooser.py @@ -221,7 +221,7 @@ def bucket_candidates(self, buckets, sufficient_funds): # Add all singletons for n, bucket in enumerate(buckets): if sufficient_funds([bucket]): - candidates.add((n, )) + candidates.add((n,)) # And now some random ones attempts = min(100, (len(buckets) - 1) * 10 + 1) diff --git a/electroncash/mnemonic.py b/electroncash/mnemonic.py index 077160b2b641..0800c2a93177 100644 --- a/electroncash/mnemonic.py +++ b/electroncash/mnemonic.py @@ -52,7 +52,7 @@ (0x2B740, 0x2B81F, 'CJK Unified Ideographs Extension D'), (0xF900, 0xFAFF, 'CJK Compatibility Ideographs'), (0x2F800, 0x2FA1D, 'CJK Compatibility Ideographs Supplement'), - (0x3190, 0x319F , 'Kanbun'), + (0x3190, 0x319F, 'Kanbun'), (0x2E80, 0x2EFF, 'CJK Radicals Supplement'), (0x2F00, 0x2FDF, 'CJK Radicals'), (0x31C0, 0x31EF, 'CJK Strokes'), diff --git a/electroncash/old_mnemonic.py b/electroncash/old_mnemonic.py index e2102b45223b..ef793dd1ddff 100644 --- a/electroncash/old_mnemonic.py +++ b/electroncash/old_mnemonic.py @@ -1661,7 +1661,7 @@ # Note about US patent no 5892470: Here each word does not represent a given digit. # Instead, the digit represented by a word is variable, it depends on the previous word. -def mn_encode( message ): +def mn_encode(message): assert len(message) % 8 == 0 out = [] for i in range(len(message)//8): @@ -1670,11 +1670,11 @@ def mn_encode( message ): w1 = (x%n) w2 = ((x//n) + w1)%n w3 = ((x//n//n) + w2)%n - out += [ words[w1], words[w2], words[w3] ] + out += [words[w1], words[w2], words[w3]] return out -def mn_decode( wlist ): +def mn_decode(wlist): out = '' for i in range(len(wlist)//3): word1, word2, word3 = wlist[3*i:3*i+3] diff --git a/electroncash/plot.py b/electroncash/plot.py index d0ffc5f0d4e3..59304c53a47e 100644 --- a/electroncash/plot.py +++ b/electroncash/plot.py @@ -33,7 +33,7 @@ def plot_history(wallet, history): f, axarr = plt.subplots(2, sharex=True) plt.subplots_adjust(bottom=0.2) - plt.xticks( rotation=25 ) + plt.xticks(rotation=25) ax = plt.gca() plt.ylabel('BCH') plt.xlabel('Month') diff --git a/electroncash/tests/test_bitcoin.py b/electroncash/tests/test_bitcoin.py index 1e8cb3320c7c..cc96dfb5f60a 100644 --- a/electroncash/tests/test_bitcoin.py +++ b/electroncash/tests/test_bitcoin.py @@ -154,7 +154,7 @@ def test_hash(self): def test_var_int(self): for i in range(0xfd): - self.assertEqual(var_int(i), "{:02x}".format(i) ) + self.assertEqual(var_int(i), "{:02x}".format(i)) self.assertEqual(var_int(0xfd), "fdfd00") self.assertEqual(var_int(0xfe), "fdfe00") diff --git a/electroncash_gui/qt/main_window.py b/electroncash_gui/qt/main_window.py index 19409222a3ca..24b3b2d9497d 100644 --- a/electroncash_gui/qt/main_window.py +++ b/electroncash_gui/qt/main_window.py @@ -968,7 +968,6 @@ def update_status(self): _("Balance: {amount_and_unit}").format( amount_and_unit=self.format_amount_and_units(c)) ] - if u: text_items.append(_("[{amount} unconfirmed]").format( amount=self.format_amount(u, True).strip())) @@ -1006,11 +1005,10 @@ def update_status(self): self.tray.setToolTip("%s (%s)" % (text, self.wallet.basename())) self.balance_label.setText(text) - self.status_button.setIcon( icon ) + self.status_button.setIcon(icon) self.status_button.setStatusTip( status_tip ) run_hook('window_update_status', self) - def update_wallet(self): self.need_update.set() # will enqueue an _update_wallet() call in at most 0.5 seconds from now. @@ -2876,7 +2874,7 @@ def create_status_bar(self): self.update_available_button.setVisible(bool(self.gui_object.new_version_available)) # if hidden now gets unhidden by on_update_available when a new version comes in self.lock_icon = QIcon() - self.password_button = StatusBarButton(self.lock_icon, _("Password"), self.change_password_dialog ) + self.password_button = StatusBarButton(self.lock_icon, _("Password"), self.change_password_dialog) sb.addPermanentWidget(self.password_button) self.addr_converter_button = StatusBarButton( @@ -2889,8 +2887,10 @@ def create_status_bar(self): self.addr_converter_button.setHidden(self.gui_object.is_cashaddr_status_button_hidden()) self.gui_object.cashaddr_status_button_hidden_signal.connect(self.addr_converter_button.setHidden) - sb.addPermanentWidget(StatusBarButton(QIcon(":icons/preferences.svg"), _("Preferences"), self.settings_dialog ) ) - self.seed_button = StatusBarButton(QIcon(":icons/seed.png"), _("Seed"), self.show_seed_dialog ) + q_icon_prefs = QIcon(":icons/preferences.svg"), _("Preferences"), self.settings_dialog) + sb.addPermanentWidget(StatusBarButton(q_icon_prefs)) + q_icon_seed = QIcon(":icons/seed.png"), _("Seed"), self.show_seed_dialog) + self.seed_button = StatusBarButton(q_icon_seed) sb.addPermanentWidget(self.seed_button) weakSelf = Weak.ref(self) gui_object = self.gui_object diff --git a/electroncash_gui/qt/network_dialog.py b/electroncash_gui/qt/network_dialog.py index 342cde824950..9d7b127aa225 100644 --- a/electroncash_gui/qt/network_dialog.py +++ b/electroncash_gui/qt/network_dialog.py @@ -159,7 +159,7 @@ def create_menu(self, position): menu.exec_(self.viewport().mapToGlobal(position)) def keyPressEvent(self, event): - if event.key() in [ Qt.Key_F2, Qt.Key_Return ]: + if event.key() in {Qt.Key_F2, Qt.Key_Return}: item, col = self.currentItem(), self.currentColumn() if item and col > -1: self.on_activated(item, col) @@ -927,11 +927,11 @@ def set_server(self, onion_hack=False): def set_proxy(self): host, port, protocol, proxy, auto_connect = self.network.get_parameters() if self.proxy_cb.isChecked(): - proxy = { 'mode':str(self.proxy_mode.currentText()).lower(), - 'host':str(self.proxy_host.text()), - 'port':str(self.proxy_port.text()), - 'user':str(self.proxy_user.text()), - 'password':str(self.proxy_password.text())} + proxy = {'mode':str(self.proxy_mode.currentText()).lower(), + 'host':str(self.proxy_host.text()), + 'port':str(self.proxy_port.text()), + 'user':str(self.proxy_user.text()), + 'password':str(self.proxy_password.text())} else: proxy = None self.network.set_parameters(host, port, protocol, proxy, auto_connect) diff --git a/electroncash_gui/qt/password_dialog.py b/electroncash_gui/qt/password_dialog.py index d47c42297775..fa07786c90e1 100644 --- a/electroncash_gui/qt/password_dialog.py +++ b/electroncash_gui/qt/password_dialog.py @@ -48,7 +48,7 @@ def check_password_strength(password): num = re.search("[0-9]", password) is not None and re.match("^[0-9]*$", password) is None caps = password != password.upper() and password != password.lower() extra = re.match("^[a-zA-Z0-9]*$", password) is None - score = len(password)*( n + caps + num + extra)/20 + score = len(password)*(n + caps + num + extra)/20 password_strength = {0:"Weak",1:"Medium",2:"Strong",3:"Very Strong"} return password_strength[min(3, int(score))] diff --git a/electroncash_gui/qt/qrcodewidget.py b/electroncash_gui/qt/qrcodewidget.py index 7c5968430084..fb60555660b9 100644 --- a/electroncash_gui/qt/qrcodewidget.py +++ b/electroncash_gui/qt/qrcodewidget.py @@ -80,7 +80,7 @@ def paintEvent(self, e): margin = 5 framesize = min(r.width(), r.height()) - boxsize = int( (framesize - 2*margin)/k ) + boxsize = int((framesize - 2*margin)/k) size = k*boxsize left = (r.width() - size)/2 top = (r.height() - size)/2 diff --git a/electroncash_gui/qt/util.py b/electroncash_gui/qt/util.py index 1e9da39ffac1..4e734de49d61 100644 --- a/electroncash_gui/qt/util.py +++ b/electroncash_gui/qt/util.py @@ -531,7 +531,7 @@ def filename_field(config, defaultname, select_msg): hbox = QHBoxLayout() directory = config.get('io_dir', os.path.expanduser('~')) - path = os.path.join( directory, defaultname ) + path = os.path.join(directory, defaultname) filename_e = QLineEdit() filename_e.setText(path) @@ -660,7 +660,7 @@ def editItem(self, item, column): item.setFlags(item.flags() & ~Qt.ItemIsEditable) def keyPressEvent(self, event): - if event.key() in [ Qt.Key_F2, Qt.Key_Return ] and self.editor is None: + if event.key() in {Qt.Key_F2, Qt.Key_Return} and self.editor is None: item, col = self.currentItem(), self.currentColumn() if item and col > -1: self.on_activated(item, col) diff --git a/electroncash_gui/stdio.py b/electroncash_gui/stdio.py index 3e4bcaa47c2b..90b7bb48fd4c 100644 --- a/electroncash_gui/stdio.py +++ b/electroncash_gui/stdio.py @@ -46,7 +46,7 @@ def __init__(self, config, daemon, plugins): _("[r] - show own receipt addresses"), \ _("[c] - display contacts"), \ _("[b] - print server banner"), \ - _("[q] - quit") ] + _("[q] - quit")] self.num_commands = len(self.commands) def on_network(self, event, *args): @@ -101,7 +101,7 @@ def print_history(self): label = self.wallet.get_label(tx_hash) messages.append( format_str%( time_str, label, format_satoshis(delta, whitespaces=True), format_satoshis(balance, whitespaces=True) ) ) - self.print_list(messages[::-1], format_str%( _("Date"), _("Description"), _("Amount"), _("Balance"))) + self.print_list(messages[::-1], format_str%(_("Date"), _("Description"), _("Amount"), _("Balance"))) def print_balance(self): @@ -110,7 +110,7 @@ def print_balance(self): def get_balance(self): if self.wallet.network.is_connected(): if not self.wallet.up_to_date: - msg = _( "Synchronizing..." ) + msg = _("Synchronizing...") else: c, u, x = self.wallet.get_balance() msg = _("Balance")+": %f "%(PyDecimal(c) / COIN) @@ -119,7 +119,7 @@ def get_balance(self): if x: msg += " [%f unmatured]"%(PyDecimal(x) / COIN) else: - msg = _( "Not connected" ) + msg = _("Not connected") return(msg) @@ -148,8 +148,8 @@ def send_order(self): self.do_send() def print_banner(self): - for i, x in enumerate( self.wallet.network.banner.split('\n') ): - print( x ) + for i, x in enumerate(self.wallet.network.banner.split('\n')): + print(x) def print_list(self, lst, firstline): lst = list(lst) diff --git a/electroncash_gui/text.py b/electroncash_gui/text.py index 58c817b67d04..57b354f71980 100644 --- a/electroncash_gui/text.py +++ b/electroncash_gui/text.py @@ -79,7 +79,7 @@ def verify_seed(self): def get_string(self, y, x): self.set_cursor(1) curses.echo() - self.stdscr.addstr( y, x, " "*20, curses.A_REVERSE) + self.stdscr.addstr(y, x, " "*20, curses.A_REVERSE) s = self.stdscr.getstr(y,x) curses.noecho() self.set_cursor(0) @@ -100,7 +100,7 @@ def print_history(self): if self.history is None: self.update_history() - self.print_list(self.history[::-1], format_str%( _("Date"), _("Description"), _("Amount"), _("Balance"))) + self.print_list(self.history[::-1], format_str%(_("Date"), _("Description"), _("Amount"), _("Balance"))) def update_history(self): width = [20, 40, 14, 14] @@ -141,10 +141,10 @@ def print_balance(self): else: msg = _("Not connected") - self.stdscr.addstr( self.maxy -1, 3, msg) + self.stdscr.addstr(self.maxy -1, 3, msg) for i in range(self.num_tabs): - self.stdscr.addstr( 0, 2 + 2*i + len(''.join(self.tab_names[0:i])), ' '+self.tab_names[i]+' ', curses.A_BOLD if self.tab == i else 0) + self.stdscr.addstr(0, 2 + 2*i + len(''.join(self.tab_names[0:i])), ' '+self.tab_names[i]+' ', curses.A_BOLD if self.tab == i else 0) self.stdscr.addstr(self.maxy -1, self.maxx-30, ' '.join([_("Settings"), _("Network"), _("Quit")])) @@ -165,9 +165,9 @@ def print_addresses(self): self.print_list(messages, fmt % ("Address", "Label")) def print_edit_line(self, y, label, text, index, size): - text += " "*(size - len(text) ) - self.stdscr.addstr( y, 2, label) - self.stdscr.addstr( y, 15, text, curses.A_REVERSE if self.pos%6==index else curses.color_pair(1)) + text += " "*(size - len(text)) + self.stdscr.addstr(y, 2, label) + self.stdscr.addstr(y, 15, text, curses.A_REVERSE if self.pos%6==index else curses.color_pair(1)) def print_send_tab(self): self.stdscr.clear() @@ -175,8 +175,8 @@ def print_send_tab(self): self.print_edit_line(5, _("Description"), self.str_description, 1, 40) self.print_edit_line(7, _("Amount"), self.str_amount, 2, 15) self.print_edit_line(9, _("Fee"), self.str_fee, 3, 15) - self.stdscr.addstr( 12, 15, _("[Send]"), curses.A_REVERSE if self.pos%6==4 else curses.color_pair(2)) - self.stdscr.addstr( 12, 25, _("[Clear]"), curses.A_REVERSE if self.pos%6==5 else curses.color_pair(2)) + self.stdscr.addstr(12, 15, _("[Send]"), curses.A_REVERSE if self.pos%6==4 else curses.color_pair(2)) + self.stdscr.addstr(12, 25, _("[Clear]"), curses.A_REVERSE if self.pos%6==5 else curses.color_pair(2)) self.maxpos = 6 def print_banner(self): @@ -206,13 +206,13 @@ def print_list(self, lst, firstline = None): if not self.maxpos: return if firstline: firstline += " "*(self.maxx -2 - len(firstline)) - self.stdscr.addstr( 1, 1, firstline ) + self.stdscr.addstr(1, 1, firstline) for i in range(self.maxy-4): msg = lst[i] if i < len(lst) else "" msg += " "*(self.maxx - 2 - len(msg)) m = msg[0:self.maxx - 2] m = m.encode(self.encoding) - self.stdscr.addstr( i+2, 1, m, curses.A_REVERSE if i == (self.pos % self.maxpos) else 0) + self.stdscr.addstr(i+2, 1, m, curses.A_REVERSE if i == (self.pos % self.maxpos) else 0) def refresh(self): if self.tab == -1: return @@ -401,7 +401,7 @@ def network_dialog(self): def settings_dialog(self): fee = str(PyDecimal(self.config.fee_per_kb()) / COIN) out = self.run_dialog('Settings', [ - {'label':'Default fee', 'type':'satoshis', 'value': fee } + {'label':'Default fee', 'type':'satoshis', 'value': fee} ], buttons = 1) if out: if out.get('Default fee'): @@ -419,13 +419,13 @@ def password_dialog(self): def run_dialog(self, title, items, interval=2, buttons=None, y_pos=3): self.popup_pos = 0 - self.w = curses.newwin( 5 + len(list(items))*interval + (2 if buttons else 0), 50, y_pos, 5) + self.w = curses.newwin(5 + len(list(items))*interval + (2 if buttons else 0), 50, y_pos, 5) w = self.w out = {} while True: w.clear() w.border(0) - w.addstr( 0, 2, title) + w.addstr(0, 2, title) num = len(list(items)) @@ -451,14 +451,14 @@ def run_dialog(self, title, items, interval=2, buttons=None, y_pos=3): value += ' '*(20-len(value)) if 'value' in item: - w.addstr( 2+interval*i, 2, label) - w.addstr( 2+interval*i, 15, value, curses.A_REVERSE if self.popup_pos%numpos==i else curses.color_pair(1) ) + w.addstr(2+interval*i, 2, label) + w.addstr(2+interval*i, 15, value, curses.A_REVERSE if self.popup_pos%numpos==i else curses.color_pair(1)) else: - w.addstr( 2+interval*i, 2, label, curses.A_REVERSE if self.popup_pos%numpos==i else 0) + w.addstr(2+interval*i, 2, label, curses.A_REVERSE if self.popup_pos%numpos==i else 0) if buttons: - w.addstr( 5+interval*i, 10, "[ ok ]", curses.A_REVERSE if self.popup_pos%numpos==(numpos-2) else curses.color_pair(2)) - w.addstr( 5+interval*i, 25, "[cancel]", curses.A_REVERSE if self.popup_pos%numpos==(numpos-1) else curses.color_pair(2)) + w.addstr(5+interval*i, 10, "[ ok ]", curses.A_REVERSE if self.popup_pos%numpos==(numpos-2) else curses.color_pair(2)) + w.addstr(5+interval*i, 25, "[cancel]", curses.A_REVERSE if self.popup_pos%numpos==(numpos-1) else curses.color_pair(2)) w.refresh() diff --git a/electroncash_plugins/hw_wallet/cmdline.py b/electroncash_plugins/hw_wallet/cmdline.py index 83c1929217a5..114348bfbd51 100644 --- a/electroncash_plugins/hw_wallet/cmdline.py +++ b/electroncash_plugins/hw_wallet/cmdline.py @@ -8,7 +8,7 @@ def get_passphrase(self, msg, confirm): return getpass.getpass('') def get_pin(self, msg): - t = { 'a':'7', 'b':'8', 'c':'9', 'd':'4', 'e':'5', 'f':'6', 'g':'1', 'h':'2', 'i':'3'} + t = {'a':'7', 'b':'8', 'c':'9', 'd':'4', 'e':'5', 'f':'6', 'g':'1', 'h':'2', 'i':'3'} print_msg(msg) print_msg("a b c\nd e f\ng h i\n-----") o = raw_input() diff --git a/electroncash_plugins/keepkey/keepkey.py b/electroncash_plugins/keepkey/keepkey.py index cb631271cf73..9abdcb1dbf51 100644 --- a/electroncash_plugins/keepkey/keepkey.py +++ b/electroncash_plugins/keepkey/keepkey.py @@ -312,16 +312,16 @@ def get_xpub(self, device_id, derivation, xtype, wizard): return xpub def get_keepkey_input_script_type(self, electrum_txin_type: str): - if electrum_txin_type in ('p2pkh', ): + if electrum_txin_type in ('p2pkh',): return self.types.SPENDADDRESS - if electrum_txin_type in ('p2sh', ): + if electrum_txin_type in ('p2sh',): return self.types.SPENDMULTISIG raise ValueError('unexpected txin type: {}'.format(electrum_txin_type)) def get_keepkey_output_script_type(self, electrum_txin_type: str): - if electrum_txin_type in ('p2pkh', ): + if electrum_txin_type in ('p2pkh',): return self.types.PAYTOADDRESS - if electrum_txin_type in ('p2sh', ): + if electrum_txin_type in ('p2sh',): return self.types.PAYTOMULTISIG raise ValueError('unexpected txin type: {}'.format(electrum_txin_type)) diff --git a/electroncash_plugins/ledger/auth2fa.py b/electroncash_plugins/ledger/auth2fa.py index d7a8a5f0d892..e2bc13786e30 100644 --- a/electroncash_plugins/ledger/auth2fa.py +++ b/electroncash_plugins/ledger/auth2fa.py @@ -15,7 +15,7 @@ "put your cursor into it, and plug your device into that computer. " \ "It will output a summary of the transaction being signed and a one-time PIN.

" \ "Verify the transaction summary and type the PIN code here.

" \ - "Before pressing enter, plug the device back into this computer.
" ), + "Before pressing enter, plug the device back into this computer.
"), _("Verify the address below.
Type the character from your security card corresponding to the BOLD character.") ] @@ -150,7 +150,7 @@ def update_dlg(self): def getDevice2FAMode(self): apdu = [0xe0, 0x24, 0x01, 0x00, 0x00, 0x01] # get 2fa mode try: - mode = self.dongle.exchange( bytearray(apdu) ) + mode = self.dongle.exchange(bytearray(apdu)) return mode except BTChipException as e: print_error('Device getMode Failed') diff --git a/electroncash_plugins/ledger/ledger.py b/electroncash_plugins/ledger/ledger.py index 5d2b5a7e3dec..6ab90db4164c 100644 --- a/electroncash_plugins/ledger/ledger.py +++ b/electroncash_plugins/ledger/ledger.py @@ -180,7 +180,7 @@ def perform_hw1_preflight(self): except BTChipException as e: if (e.sw == 0x6985): self.dongleObject.dongle.close() - self.handler.get_setup( ) + self.handler.get_setup() # Acquire the new client on the next run else: raise e