CodeReclaimers / btce-api

Python wrapper around the public and trading APIs of BTC-e.com
MIT License
191 stars 123 forks source link

Allow subtypes for btceapi.keyhandler.KeyHandler #25

Closed zku closed 11 years ago

zku commented 11 years ago

This would also allow subtyped btceapi.keyhandler.KeyHandler instances. An example of a subtyped KeyHandler class might be one that defines a close() function so the KeyHandler can be used with the 'with' syntax.

#!/usr/bin/env python

from contextlib import closing
import btceapi
import mybtceconfig

class ClosableKeyHandler(btceapi.KeyHandler):
    def __init__(self, filename):
        super(ClosableKeyHandler, self).__init__(filename, True)

    def close(self):
        self.save(self.filename)

with closing(ClosableKeyHandler(mybtceconfig.keyFile())) as keyHandler:
    tapi = btceapi.TradeAPI(mybtceconfig.key(), keyHandler)
    info = tapi.getInfo()
    print 'Current balance (USD): %s' % info.balance_usd
alanmcintyre commented 11 years ago

Thanks! I'm going to also go ahead and add close, enter, and exit to the KeyHandler class, because being able to use them in a with statement and having an explicit close method seems like a good idea.