From 2978b1824555038cb2a393ae06c1b237377f9b35 Mon Sep 17 00:00:00 2001 From: Kristaps Kaupe Date: Tue, 2 Jan 2024 17:34:30 +0200 Subject: [PATCH] De-duplicate and change dict_factory --- src/jmbase/__init__.py | 2 +- src/jmbase/support.py | 5 +++++ src/jmclient/wallet_utils.py | 9 +-------- src/jmdaemon/orderbookwatch.py | 9 +-------- 4 files changed, 8 insertions(+), 17 deletions(-) diff --git a/src/jmbase/__init__.py b/src/jmbase/__init__.py index 7f16d8e..abc8cdf 100644 --- a/src/jmbase/__init__.py +++ b/src/jmbase/__init__.py @@ -8,7 +8,7 @@ from .support import (get_log, chunks, debug_silence, jmprint, EXIT_SUCCESS, hexbin, dictchanger, listchanger, JM_WALLET_NAME_PREFIX, JM_APP_NAME, IndentedHelpFormatterWithNL, wrapped_urlparse, - bdict_sdict_convert, random_insert) + bdict_sdict_convert, random_insert, dict_factory) from .proof_of_work import get_pow, verify_pow from .twisted_utils import (stop_reactor, is_hs_uri, get_tor_agent, get_nontor_agent, JMHiddenService, diff --git a/src/jmbase/support.py b/src/jmbase/support.py index e97bf2c..6c60b3d 100644 --- a/src/jmbase/support.py +++ b/src/jmbase/support.py @@ -7,6 +7,7 @@ from getpass import getpass from os import path, environ from functools import wraps from optparse import IndentedHelpFormatter +from sqlite3 import Cursor, Row from typing import List import urllib.parse as urlparse @@ -356,3 +357,7 @@ def get_free_tcp_ports(num_ports: int) -> List[int]: for s in sockets: s.close() return ports + +def dict_factory(cursor: Cursor, row: Row) -> dict: + fields = [column[0] for column in cursor.description] + return {key: value for key, value in zip(fields, row)} diff --git a/src/jmclient/wallet_utils.py b/src/jmclient/wallet_utils.py index 686ab16..5dccab5 100644 --- a/src/jmclient/wallet_utils.py +++ b/src/jmclient/wallet_utils.py @@ -20,7 +20,7 @@ from jmclient.blockchaininterface import (BitcoinCoreInterface, from jmclient.wallet_service import WalletService from jmbase.support import (get_password, jmprint, EXIT_FAILURE, EXIT_ARGERROR, utxo_to_utxostr, hextobin, bintohex, - IndentedHelpFormatterWithNL) + IndentedHelpFormatterWithNL, dict_factory) from .cryptoengine import TYPE_P2PKH, TYPE_P2SH_P2WPKH, TYPE_P2WPKH, \ TYPE_SEGWIT_WALLET_FIDELITY_BONDS @@ -815,13 +815,6 @@ def wallet_change_passphrase(walletservice, return True -def dict_factory(cursor, row): - d = {} - for idx, col in enumerate(cursor.description): - d[col[0]] = row[idx] - return d - - def wallet_fetch_history(wallet, options): # sort txes in a db because python can be really bad with large lists con = sqlite3.connect(":memory:") diff --git a/src/jmdaemon/orderbookwatch.py b/src/jmdaemon/orderbookwatch.py index 7966759..ba62087 100644 --- a/src/jmdaemon/orderbookwatch.py +++ b/src/jmdaemon/orderbookwatch.py @@ -8,17 +8,10 @@ from numbers import Integral from jmdaemon.protocol import JM_VERSION from jmdaemon import fidelity_bond_sanity_check -from jmbase.support import get_log, joinmarket_alert +from jmbase.support import dict_factory, get_log, joinmarket_alert log = get_log() -def dict_factory(cursor, row): - d = {} - for idx, col in enumerate(cursor.description): - d[col[0]] = row[idx] - return d - - class JMTakerError(Exception): pass