Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: db on July 08, 2010, 11:50:43 PM



Title: Python wrapper for bitcoind
Post by: db on July 08, 2010, 11:50:43 PM
While creating the Bitcoin automation for mullvad.net I made a python wrapper around bitcoind. Should anyone else find such a thing useful it is available here:

https://www.mullvad.net/download/bitcoin.py

Usage example:

>>> import bitcoin
>>> bitcoin.getaddressesbylabel("foo")
['16au2i9ny5pnwCPnoekov2hHMhtS7qcFjR', '19BkB63mo3NQRPNeHkc3bWnKGBU7oL8Dp7']
>>> bitcoin.getreceivedbylabel("Your Address")
6.0
>>> bitcoin.listreceivedbyaddress()
{'16au2i9ny5pnwCPnoekov2hHMhtS7qcFjR': {'amount': 2000.0, 'label': 'foo', 'confirmations': 226, 'address': '16au2i9ny5pnwCPnoekov2hHMhtS7qcFjR'}, '1FLD8wCNNBh2wg2mnGjD9ogoLaRxpSz7TW': {'amount': 6.0, 'label': 'Your Address', 'confirmations': 343, 'address': '1FLD8wCNNBh2wg2mnGjD9ogoLaRxpSz7TW'}}
>>> bitcoin.sendtoaddress("1Nsq3itZULUZjtZGcjNrtZtwT8aMsHu1R1", 1234.5)
>>> bitcoin.sendtoaddress("1Nsq3itZULUZjtZGcjNrtZtwT8aMsHu1R1", 1000000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "bitcoin.py", line 73, in sendtoaddress
    _cmd("sendtoaddress %s %d %s %s" % (bitcoinaddress, amount, comment, comment_to))
  File "bitcoin.py", line 86, in _cmd
    return _parse(_cmd_raw(args))[0]
  File "bitcoin.py", line 92, in _cmd_raw
    raise BitcoinError(errors)
bitcoin.BitcoinError: error: Insufficient funds

>>>



Title: Re: Python wrapper for bitcoind
Post by: NewLibertyStandard on July 09, 2010, 03:37:21 AM
That's great! Thanks db!


Title: Re: Python wrapper for bitcoind
Post by: phelix on August 14, 2011, 09:45:28 AM
That's great! Thanks db!
+1


Title: Re: Python wrapper for bitcoind
Post by: etotheipi on August 14, 2011, 09:07:35 PM
Looks great, db.

Just a minor recommendation: given the ease with which you can catch errors in python, you might consider an except(BitcoinError) clause to gracefully fail when you have insufficient funds, or other error.   Your original post showed this:

Code:
>>> bitcoin.sendtoaddress("1Nsq3itZULUZjtZGcjNrtZtwT8aMsHu1R1", 1000000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "bitcoin.py", line 73, in sendtoaddress
    _cmd("sendtoaddress %s %d %s %s" % (bitcoinaddress, amount, comment, comment_to))
  File "bitcoin.py", line 86, in _cmd
    return _parse(_cmd_raw(args))[0]
  File "bitcoin.py", line 92, in _cmd_raw
    raise BitcoinError(errors)
bitcoin.BitcoinError: error: Insufficient funds

It should be pretty easy to produce something like this, instead:

Code:
>>> bitcoin.sendtoaddress("1Nsq3itZULUZjtZGcjNrtZtwT8aMsHu1R1", 1000000)
***ERROR:  Insufficient funds***
   Attempted Transaction:  1000000
   Available Balance:       922100

Or whatever info/format-string you want, but those Traceback messages tend to scare users.


Title: Re: Python wrapper for bitcoind
Post by: phelix on August 14, 2011, 11:16:55 PM
[...]
Or whatever info/format-string you want, but those Traceback messages tend to scare users.

lol, for me it's the other way round.

it may be true but for me tracebacks are much more useful. custom error messages make finding bugs much harder and it can be a pain to reinstall the tracebacks.



Title: Re: Python wrapper for bitcoind
Post by: etotheipi on August 15, 2011, 12:13:46 AM
I'm differentiating users from developers, here.  I recognize the Tracebacks are useful for developers, but as a user, I don't plan to be debugging his code.  Plus, this isn't the result of a bug--it's a legitimate validation failure response.   Not a big deal, it was just a recommendation...



Title: Re: Python wrapper for bitcoind
Post by: phelix on April 08, 2012, 03:39:36 PM
fyi: namecoind wrapper forked: http://dot-bit.org/forum/viewtopic.php?f=2&t=482