Browse Source

Merge JoinMarket-Org/joinmarket-clientserver#1629: De-duplicate and change `dict_factory`

8f382d0d5e Add test for dict_factory() (Kristaps Kaupe)
2978b18245 De-duplicate and change dict_factory (Kristaps Kaupe)

Pull request description:

  Move to `jmbase` and switch to implementation from [Python docs](https://docs.python.org/3/library/sqlite3.html#sqlite3-howto-row-factory), which seems nicer.

Top commit has no ACKs.

Tree-SHA512: 02ca21fea0eb45cc2f05d47e85b4150c300ad895aaf6102c90c433b38b95f8fb7b1e04292542afc6073fc22b14b07c38c01957f1380bed080585d9b67c0ee4bf
master
Kristaps Kaupe 2 years ago
parent
commit
80d6ea6519
No known key found for this signature in database
GPG Key ID: 33E472FE870C7E5D
  1. 2
      src/jmbase/__init__.py
  2. 5
      src/jmbase/support.py
  3. 9
      src/jmclient/wallet_utils.py
  4. 9
      src/jmdaemon/orderbookwatch.py
  5. 17
      test/jmbase/test_base_support.py

2
src/jmbase/__init__.py

@ -8,7 +8,7 @@ from .support import (get_log, chunks, debug_silence, jmprint,
EXIT_SUCCESS, hexbin, dictchanger, listchanger, EXIT_SUCCESS, hexbin, dictchanger, listchanger,
JM_WALLET_NAME_PREFIX, JM_APP_NAME, JM_WALLET_NAME_PREFIX, JM_APP_NAME,
IndentedHelpFormatterWithNL, wrapped_urlparse, 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 .proof_of_work import get_pow, verify_pow
from .twisted_utils import (stop_reactor, is_hs_uri, get_tor_agent, from .twisted_utils import (stop_reactor, is_hs_uri, get_tor_agent,
get_nontor_agent, JMHiddenService, get_nontor_agent, JMHiddenService,

5
src/jmbase/support.py

@ -7,6 +7,7 @@ from getpass import getpass
from os import path, environ from os import path, environ
from functools import wraps from functools import wraps
from optparse import IndentedHelpFormatter from optparse import IndentedHelpFormatter
from sqlite3 import Cursor, Row
from typing import List from typing import List
import urllib.parse as urlparse import urllib.parse as urlparse
@ -356,3 +357,7 @@ def get_free_tcp_ports(num_ports: int) -> List[int]:
for s in sockets: for s in sockets:
s.close() s.close()
return ports 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)}

9
src/jmclient/wallet_utils.py

@ -20,7 +20,7 @@ from jmclient.blockchaininterface import (BitcoinCoreInterface,
from jmclient.wallet_service import WalletService from jmclient.wallet_service import WalletService
from jmbase.support import (get_password, jmprint, EXIT_FAILURE, from jmbase.support import (get_password, jmprint, EXIT_FAILURE,
EXIT_ARGERROR, utxo_to_utxostr, hextobin, bintohex, EXIT_ARGERROR, utxo_to_utxostr, hextobin, bintohex,
IndentedHelpFormatterWithNL) IndentedHelpFormatterWithNL, dict_factory)
from .cryptoengine import TYPE_P2PKH, TYPE_P2SH_P2WPKH, TYPE_P2WPKH, \ from .cryptoengine import TYPE_P2PKH, TYPE_P2SH_P2WPKH, TYPE_P2WPKH, \
TYPE_SEGWIT_WALLET_FIDELITY_BONDS TYPE_SEGWIT_WALLET_FIDELITY_BONDS
@ -815,13 +815,6 @@ def wallet_change_passphrase(walletservice,
return True 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): def wallet_fetch_history(wallet, options):
# sort txes in a db because python can be really bad with large lists # sort txes in a db because python can be really bad with large lists
con = sqlite3.connect(":memory:") con = sqlite3.connect(":memory:")

9
src/jmdaemon/orderbookwatch.py

@ -8,17 +8,10 @@ from numbers import Integral
from jmdaemon.protocol import JM_VERSION from jmdaemon.protocol import JM_VERSION
from jmdaemon import fidelity_bond_sanity_check 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() 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): class JMTakerError(Exception):
pass pass

17
test/jmbase/test_base_support.py

@ -1,7 +1,9 @@
#! /usr/bin/env python #! /usr/bin/env python
import pytest
import copy import copy
from jmbase import random_insert import pytest
import sqlite3
from jmbase import dict_factory, random_insert
def test_color_coded_logging(): def test_color_coded_logging():
# TODO # TODO
@ -30,3 +32,14 @@ def test_random_insert(list1, list2):
i_x = list1.index(x) i_x = list1.index(x)
i_y = list1.index(y) i_y = list1.index(y)
assert i_y > i_x assert i_y > i_x
def test_dict_factory():
con = sqlite3.connect(":memory:")
con.row_factory = dict_factory
db = con.cursor()
db.execute("CREATE TABLE test (one TEXT, two TEXT)")
db.execute("INSERT INTO test VALUES (?, ?)", [ "one", "two" ])
res = db.execute("SELECT * FROM test")
row = res.fetchone()
assert row["one"] == "one"
assert row["two"] == "two"

Loading…
Cancel
Save