Skip to content

Commit 5d10c72

Browse files
author
Antoine Le Calvez
committed
PEP8
1 parent b1c65a5 commit 5d10c72

File tree

9 files changed

+109
-75
lines changed

9 files changed

+109
-75
lines changed

blockchain/blockexplorer.py

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from . import util
77
import json
88

9-
def get_block(block_id, api_code = None):
9+
10+
def get_block(block_id, api_code=None):
1011
"""Get a single block based on a block index or hash.
1112
1213
:param str block_id: block hash or index to look up
@@ -21,7 +22,8 @@ def get_block(block_id, api_code = None):
2122
json_response = json.loads(response)
2223
return Block(json_response)
2324

24-
def get_tx(tx_id, api_code = None):
25+
26+
def get_tx(tx_id, api_code=None):
2527
"""Get a single transaction based on a transaction index or hash.
2628
2729
:param str block_id: transaction hash or index to look up
@@ -36,7 +38,8 @@ def get_tx(tx_id, api_code = None):
3638
json_response = json.loads(response)
3739
return Transaction(json_response)
3840

39-
def get_block_height(height, api_code = None):
41+
42+
def get_block_height(height, api_code=None):
4043
"""Get an array of blocks at the specified height.
4144
4245
:param int height: block height to look up
@@ -51,7 +54,8 @@ def get_block_height(height, api_code = None):
5154
json_response = json.loads(response)
5255
return [Block(b) for b in json_response['blocks']]
5356

54-
def get_address(address, api_code = None):
57+
58+
def get_address(address, api_code=None):
5559
"""Get data for a single address.
5660
5761
:param str address: address to look up
@@ -65,8 +69,9 @@ def get_address(address, api_code = None):
6569
response = util.call_api(resource)
6670
json_response = json.loads(response)
6771
return Address(json_response)
68-
69-
def get_unspent_outputs(address, api_code = None):
72+
73+
74+
def get_unspent_outputs(address, api_code=None):
7075
"""Get unspent outputs for a single address.
7176
7277
:param str address: address to look up
@@ -81,7 +86,8 @@ def get_unspent_outputs(address, api_code = None):
8186
json_response = json.loads(response)
8287
return [UnspentOutput(o) for o in json_response['unspent_outputs']]
8388

84-
def get_latest_block(api_code = None):
89+
90+
def get_latest_block(api_code=None):
8591
"""Get the latest block on the main chain.
8692
8793
:param str api_code: Blockchain.info API code (optional)
@@ -94,8 +100,9 @@ def get_latest_block(api_code = None):
94100
response = util.call_api(resource)
95101
json_response = json.loads(response)
96102
return LatestBlock(json_response)
97-
98-
def get_unconfirmed_tx(api_code = None):
103+
104+
105+
def get_unconfirmed_tx(api_code=None):
99106
"""Get a list of currently unconfirmed transactions.
100107
101108
:param str api_code: Blockchain.info API code (optional)
@@ -109,7 +116,8 @@ def get_unconfirmed_tx(api_code = None):
109116
json_response = json.loads(response)
110117
return [Transaction(t) for t in json_response['txs']]
111118

112-
def get_blocks(time = None, pool_name = None, api_code = None):
119+
120+
def get_blocks(time=None, pool_name=None, api_code=None):
113121
"""Get a list of blocks for a specific day or mining pool.
114122
Both parameters are optional but at least one is required.
115123
@@ -133,7 +141,8 @@ def get_blocks(time = None, pool_name = None, api_code = None):
133141
json_response = json.loads(response)
134142
return [SimpleBlock(b) for b in json_response['blocks']]
135143

136-
def get_inventory_data(hash, api_code = None):
144+
145+
def get_inventory_data(hash, api_code=None):
137146
"""Get inventory data.
138147
139148
:param str hash: tx or block hash
@@ -146,22 +155,25 @@ def get_inventory_data(hash, api_code = None):
146155
response = util.call_api(resource)
147156
json_response = json.loads(response)
148157
return InventoryData(json_response)
149-
158+
159+
150160
class SimpleBlock:
151161
def __init__(self, b):
152162
self.height = b['height']
153163
self.hash = b['hash']
154164
self.time = b['time']
155165
self.main_chain = b['main_chain']
156-
166+
167+
157168
class LatestBlock:
158169
def __init__(self, b):
159170
self.hash = b['hash']
160171
self.time = b['time']
161172
self.block_index = b['block_index']
162173
self.height = b['height']
163174
self.tx_indexes = [i for i in b['txIndexes']]
164-
175+
176+
165177
class UnspentOutput:
166178
def __init__(self, o):
167179
self.tx_hash = o['tx_hash']
@@ -171,7 +183,8 @@ def __init__(self, o):
171183
self.value = o['value']
172184
self.value_hex = o['value_hex']
173185
self.confirmations = o['confirmations']
174-
186+
187+
175188
class Address:
176189
def __init__(self, a):
177190
self.hash160 = a['hash160']
@@ -181,12 +194,13 @@ def __init__(self, a):
181194
self.total_sent = a['total_sent']
182195
self.final_balance = a['final_balance']
183196
self.transactions = [Transaction(tx) for tx in a['txs']]
184-
197+
198+
185199
class Input:
186200
def __init__(self, i):
187201
obj = i.get('prev_out')
188202
if obj is not None:
189-
# regular TX
203+
# regular TX
190204
self.n = obj['n']
191205
self.value = obj['value']
192206
self.address = obj['addr']
@@ -196,10 +210,11 @@ def __init__(self, i):
196210
self.script_sig = i['script']
197211
self.sequence = i['sequence']
198212
else:
199-
# coinbase TX
213+
# coinbase TX
200214
self.script_sig = i['script']
201215
self.sequence = i['sequence']
202216

217+
203218
class Output:
204219
def __init__(self, o):
205220
self.n = o['n']
@@ -209,6 +224,7 @@ def __init__(self, o):
209224
self.script = o['script']
210225
self.spent = o['spent']
211226

227+
212228
class Transaction:
213229
def __init__(self, t):
214230
self.double_spend = t.get('double_spend', False)
@@ -224,7 +240,8 @@ def __init__(self, t):
224240

225241
if self.block_height is None:
226242
self.block_height = -1
227-
243+
244+
228245
class Block:
229246
def __init__(self, b):
230247
self.hash = b['hash']
@@ -246,6 +263,7 @@ def __init__(self, b):
246263
for tx in self.transactions:
247264
tx.block_height = self.height
248265

266+
249267
class InventoryData:
250268
def __init__(self, i):
251269
self.hash = i['hash']

blockchain/createwallet.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from . import util
77
import json
88

9-
def create_wallet(password, api_code, priv = None, label = None, email = None):
9+
10+
def create_wallet(password, api_code, priv=None, label=None, email=None):
1011
"""Create a new Blockchain.info wallet. It can be created containing a
1112
pre-generated private key or will otherwise generate a new private key.
1213
@@ -18,7 +19,7 @@ def create_wallet(password, api_code, priv = None, label = None, email = None):
1819
:return: an instance of :class:`WalletResponse` class
1920
"""
2021

21-
params = { 'password': password, 'api_code': api_code }
22+
params = {'password': password, 'api_code': api_code}
2223
if priv is not None:
2324
params['priv'] = priv
2425
if label is not None:
@@ -31,7 +32,8 @@ def create_wallet(password, api_code, priv = None, label = None, email = None):
3132
return CreateWalletResponse(json_response['guid'],
3233
json_response['address'],
3334
json_response['link'])
34-
35+
36+
3537
class CreateWalletResponse:
3638

3739
def __init__(self, identifier, address, link):

blockchain/exchangerates.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import json
66
from . import util
77

8-
def get_ticker(api_code = None):
8+
9+
def get_ticker(api_code=None):
910
"""Call the 'ticker' method and return a dictionary
1011
of :class:`Currency` objects.
1112
@@ -19,14 +20,15 @@ def get_ticker(api_code = None):
1920
for key in json_response:
2021
json_ccy = json_response[key]
2122
ccy = Currency(json_ccy['last'],
22-
json_ccy['buy'],
23-
json_ccy['sell'],
24-
json_ccy['symbol'],
25-
json_ccy['15m'])
23+
json_ccy['buy'],
24+
json_ccy['sell'],
25+
json_ccy['symbol'],
26+
json_ccy['15m'])
2627
ticker[key] = ccy
2728
return ticker
2829

29-
def to_btc(ccy, value, api_code = None):
30+
31+
def to_btc(ccy, value, api_code=None):
3032
"""Call the 'tobtc' method and convert x value in the provided currency to BTC.
3133
3234
:param str ccy: currency code
@@ -39,7 +41,8 @@ def to_btc(ccy, value, api_code = None):
3941
if api_code is not None:
4042
res += '&api_code=' + api_code
4143
return float(util.call_api(res))
42-
44+
45+
4346
class Currency:
4447
def __init__(self, last, buy, sell, symbol, p15min):
4548
self.last = last

blockchain/pushtx.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
"""This module allows users to push hex encoded transactions to the bitcoin network. Corresponds to https://blockchain.info/pushtx
1+
"""This module allows users to push hex encoded transactions to the bitcoin network.
2+
Corresponds to https://blockchain.info/pushtx
23
"""
34

45
from . import util
56

6-
def pushtx(tx, api_code = None):
7+
8+
def pushtx(tx, api_code=None):
79
"""Push a hex encoded transaction to the network.
810
911
:param str tx: hex encoded transaction
1012
:param str api_code: Blockchain.info API code (optional)
1113
"""
12-
params = { 'tx': tx }
14+
params = {'tx': tx}
1315
if api_code is not None:
1416
params['api_code'] = api_code
1517
util.call_api('pushtx', params)

blockchain/receive.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from . import util
66
import json
77

8+
89
class ReceiveResponse:
910

1011
def __init__(self, fee_percent, dest, input, callback):
@@ -13,7 +14,8 @@ def __init__(self, fee_percent, dest, input, callback):
1314
self.input_address = input
1415
self.callback_url = callback
1516

16-
def receive(dest_addr, callback, api_code = None):
17+
18+
def receive(dest_addr, callback, api_code=None):
1719
"""Call the 'api/receive' endpoint and create a forwarding address.
1820
1921
:param str dest_addr: destination address where the payment should be sent
@@ -22,13 +24,13 @@ def receive(dest_addr, callback, api_code = None):
2224
:return: an instance of :class:`ReceiveResponse` class
2325
"""
2426

25-
params = { 'method': 'create', 'address': dest_addr, 'callback': callback }
27+
params = {'method': 'create', 'address': dest_addr, 'callback': callback}
2628
if api_code is not None:
2729
params['api_code'] = api_code
2830
resp = util.call_api('api/receive', params)
2931
json_resp = json.loads(resp)
3032
payment_response = ReceiveResponse(json_resp['fee_percent'],
31-
json_resp['destination'],
32-
json_resp['input_address'],
33-
json_resp['callback_url'])
33+
json_resp['destination'],
34+
json_resp['input_address'],
35+
json_resp['callback_url'])
3436
return payment_response

blockchain/statistics.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from . import util
77
import json
88

9-
def get(api_code = None):
9+
10+
def get(api_code=None):
1011
"""Get network statistics.
1112
1213
:param str api_code: Blockchain.info API code (optional)
@@ -19,7 +20,8 @@ def get(api_code = None):
1920
response = util.call_api(resource)
2021
json_response = json.loads(response)
2122
return Stats(json_response)
22-
23+
24+
2325
class Stats:
2426
def __init__(self, s):
2527
self.trade_volume_btc = s['trade_volume_btc']

blockchain/util.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,22 @@
1616
from urllib2 import HTTPError
1717
from urllib import urlencode
1818

19-
def call_api(resource, data = None, base_url = BASE_URL):
19+
20+
def call_api(resource, data=None, base_url=BASE_URL):
2021
try:
2122
payload = None if data is None else urlencode(data)
22-
if py_version >= 3 and payload is not None: payload = payload.encode('UTF-8')
23-
response = urlopen(base_url + resource, payload, timeout = TIMEOUT).read()
23+
if py_version >= 3 and payload is not None:
24+
payload = payload.encode('UTF-8')
25+
response = urlopen(base_url + resource, payload, timeout=TIMEOUT).read()
2426
return handle_response(response)
2527

2628
except HTTPError as e:
2729
raise APIException(handle_response(e.read()), e.code)
2830

31+
2932
def handle_response(response):
30-
#urllib returns different types in Python 2 and 3 (str vs bytes)
33+
# urllib returns different types in Python 2 and 3 (str vs bytes)
3134
if isinstance(response, str):
3235
return response
3336
else:
34-
return response.decode('utf-8')
37+
return response.decode('utf-8')

0 commit comments

Comments
 (0)