Skip to content

Commit cf147e6

Browse files
authored
Merge pull request #40 from taylorjdawson/linting
Cleaned up code to comply with flake8 linter
2 parents b6ad1a0 + bb2cf96 commit cf147e6

24 files changed

+96
-78
lines changed

etherscan/accounts.py

+24-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44

55
class Account(Client):
6+
PAGE_NUM_PATTERN = re.compile(
7+
'[1-9](?:\d{0,2})(?:,\d{3})*(?:\.\d*[1-9])?|0?\.\d*[1-9]|0')
8+
69
def __init__(self, address=Client.dao_address, api_key='YourApiKeyToken'):
710
Client.__init__(self, address=address, api_key=api_key)
811
self.url_dict[self.MODULE] = 'account'
@@ -21,9 +24,11 @@ def get_balance_multiple(self):
2124
req = self.connect()
2225
return req['result']
2326

24-
def get_transaction_page(self, page=1, offset=10000, sort='asc', internal=False) -> list:
27+
def get_transaction_page(self, page=1, offset=10000, sort='asc',
28+
internal=False) -> list:
2529
"""
26-
Get a page of transactions, each transaction returns list of dict with keys:
30+
Get a page of transactions, each transaction
31+
returns list of dict with keys:
2732
nonce
2833
hash
2934
cumulativeGasUsed
@@ -62,7 +67,8 @@ def get_transaction_page(self, page=1, offset=10000, sort='asc', internal=False)
6267
req = self.connect()
6368
return req['result']
6469

65-
def get_all_transactions(self, offset=10000, sort='asc', internal=False) -> list:
70+
def get_all_transactions(self, offset=10000, sort='asc',
71+
internal=False) -> list:
6672
if internal:
6773
self.url_dict[self.ACTION] = 'txlistinternal'
6874
else:
@@ -77,19 +83,23 @@ def get_all_transactions(self, offset=10000, sort='asc', internal=False) -> list
7783
self.build_url()
7884
req = self.connect()
7985
if "No transactions found" in req['message']:
80-
print("Total number of transactions: {}".format(len(trans_list)))
86+
print(
87+
"Total number of transactions: {}".format(len(trans_list)))
8188
self.page = ''
8289
return trans_list
8390
else:
8491
trans_list += req['result']
8592
# Find any character block that is a integer of any length
86-
page_number = re.findall(r'[1-9](?:\d{0,2})(?:,\d{3})*(?:\.\d*[1-9])?|0?\.\d*[1-9]|0', self.url_dict[self.PAGE])
93+
page_number = re.findall(Account.PAGE_NUM_PATTERN,
94+
self.url_dict[self.PAGE])
8795
print("page {} added".format(page_number[0]))
8896
self.url_dict[self.PAGE] = str(int(page_number[0]) + 1)
8997

90-
def get_blocks_mined_page(self, blocktype='blocks', page=1, offset=10000) -> list:
98+
def get_blocks_mined_page(self, blocktype='blocks', page=1,
99+
offset=10000) -> list:
91100
"""
92-
Get a page of blocks mined by given address, returns list of dict with keys:
101+
Get a page of blocks mined by given address,
102+
returns list of dict with keys:
93103
blockReward (in wei)
94104
blockNumber
95105
timeStamp
@@ -117,12 +127,15 @@ def get_all_blocks_mined(self, blocktype='blocks', offset=10000) -> list:
117127
req = self.connect()
118128
print(req['message'])
119129
if "No transactions found" in req['message']:
120-
print("Total number of blocks mined: {}".format(len(blocks_list)))
130+
print(
131+
"Total number of blocks mined: {}".format(
132+
len(blocks_list)))
121133
return blocks_list
122134
else:
123135
blocks_list += req['result']
124136
# Find any character block that is a integer of any length
125-
page_number = re.findall(r'[1-9](?:\d{0,2})(?:,\d{3})*(?:\.\d*[1-9])?|0?\.\d*[1-9]|0', self.url_dict[self.PAGE])
137+
page_number = re.findall(Account.PAGE_NUM_PATTERN,
138+
self.url_dict[self.PAGE])
126139
print("page {} added".format(page_number[0]))
127140
self.url_dict[self.PAGE] = str(int(page_number[0]) + 1)
128141

@@ -135,6 +148,7 @@ def get_internal_by_hash(self, tx_hash=''):
135148

136149
def update_transactions(self, address, trans):
137150
"""
138-
Gets last page of transactions (last 10k trans) and updates current trans book (book)
151+
Gets last page of transactions (last 10k trans)
152+
and updates current trans book (book)
139153
"""
140154
pass

etherscan/client.py

+16-12
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class BadRequest(ClientException):
3030
"""Invalid request passed"""
3131

3232

33-
# Assume user puts his API key in the api_key.json file under variable name "key"
33+
# Assume user puts his API key in the api_key.json
34+
# file under variable name "key"
3435
class Client(object):
3536
dao_address = '0xbb9bc244d798123fde783fcc1c72d3bb8c189413'
3637

@@ -65,7 +66,6 @@ class Client(object):
6566
def __init__(self, address, api_key=''):
6667
self.http = requests.session()
6768
self.url_dict = collections.OrderedDict([
68-
6969
(self.MODULE, ''),
7070
(self.ADDRESS, ''),
7171
(self.OFFSET, ''),
@@ -86,12 +86,12 @@ def __init__(self, address, api_key=''):
8686
(self.TAG, ''),
8787
(self.BOOLEAN, ''),
8888
(self.INDEX, ''),
89-
(self.API_KEY, api_key)]
90-
)
89+
(self.API_KEY, api_key)])
90+
91+
# Var initialization should take place within init
92+
self.url = None
9193

92-
# self.url_dict[API_KEY] = str(api_key)
9394
self.check_and_get_api()
94-
# self.key = self.URL_BASES['key'] + self.API_KEY
9595

9696
if (len(address) > 20) and (type(address) == list):
9797
raise BadRequest("Etherscan only takes 20 addresses at a time")
@@ -101,7 +101,9 @@ def __init__(self, address, api_key=''):
101101
self.url_dict[self.ADDRESS] = address
102102

103103
def build_url(self):
104-
self.url = self.PREFIX + ''.join([param + val if val else '' for param, val in self.url_dict.items()])
104+
self.url = self.PREFIX + ''.join(
105+
[param + val if val else '' for param, val in
106+
self.url_dict.items()])
105107

106108
def connect(self):
107109
# TODO: deal with "unknown exception" error
@@ -119,14 +121,16 @@ def connect(self):
119121
return data
120122
else:
121123
raise EmptyResponse(data.get('message', 'no message'))
122-
raise BadRequest("Problem with connection, status code: %s" % req.status_code)
124+
raise BadRequest(
125+
"Problem with connection, status code: %s" % req.status_code)
123126

124127
def check_and_get_api(self):
125128
if self.url_dict[self.API_KEY]: # Check if api_key is empty string
126129
pass
127130
else:
128-
self.url_dict[self.API_KEY] = input('Please type your EtherScan.io API key: ')
131+
self.url_dict[self.API_KEY] = input(
132+
'Please type your EtherScan.io API key: ')
129133

130-
def check_keys_api(self, data):
131-
return all (k in data for k in ('jsonrpc', 'id', 'result'))
132-
134+
@staticmethod
135+
def check_keys_api(data):
136+
return all(k in data for k in ('jsonrpc', 'id', 'result'))

etherscan/proxies.py

+15-12
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,32 @@ def get_most_recent_block(self):
1212
self.build_url()
1313
req = self.connect()
1414
return req['result']
15-
15+
1616
def get_block_by_number(self, block_number: Union[str, int]):
1717
self.url_dict[self.ACTION] = 'eth_getBlockByNumber'
18-
self.url_dict[self.TAG] = block_number if type(block_number) is str else hex(block_number)
18+
self.url_dict[self.TAG] = block_number if type(
19+
block_number) is str else hex(block_number)
1920
self.url_dict[self.BOOLEAN] = 'true'
2021
self.build_url()
2122
req = self.connect()
2223
return req['result']
2324

2425
def get_uncle_by_blocknumber_index(self,
25-
block_number: Union[str, int],
26-
index: Union[str, int]):
26+
block_number: Union[str, int],
27+
index: Union[str, int]):
2728
self.url_dict[self.ACTION] = 'eth_getUncleByBlockNumberAndIndex'
28-
self.url_dict[self.TAG] = block_number if type(block_number) is str else hex(block_number)
29+
self.url_dict[self.TAG] = block_number if type(
30+
block_number) is str else hex(block_number)
2931
self.url_dict[self.INDEX] = index if type(index) is str else hex(index)
3032
self.build_url()
3133
req = self.connect()
3234
return req['result']
3335

34-
def get_block_transaction_count_by_number(self, block_number: Union[str, int]):
36+
def get_block_transaction_count_by_number(self,
37+
block_number: Union[str, int]):
3538
self.url_dict[self.ACTION] = 'eth_getBlockTransactionCountByNumber'
36-
self.url_dict[self.TAG] = block_number if type(block_number) is str else hex(block_number)
39+
self.url_dict[self.TAG] = block_number if type(
40+
block_number) is str else hex(block_number)
3741
self.build_url()
3842
req = self.connect()
3943
return req['result']
@@ -46,10 +50,11 @@ def get_transaction_by_hash(self, tx_hash: str):
4650
return req['result']
4751

4852
def get_transaction_by_blocknumber_index(self,
49-
block_number: Union[str, int],
50-
index: Union[str, int]):
53+
block_number: Union[str, int],
54+
index: Union[str, int]):
5155
self.url_dict[self.ACTION] = 'eth_getTransactionByBlockNumberAndIndex'
52-
self.url_dict[self.TAG] = block_number if type(block_number) is str else hex(block_number)
56+
self.url_dict[self.TAG] = block_number if type(
57+
block_number) is str else hex(block_number)
5358
self.url_dict[self.INDEX] = index if type(index) is str else hex(index)
5459
self.build_url()
5560
req = self.connect()
@@ -69,5 +74,3 @@ def get_transaction_receipt(self, tx_hash: str):
6974
self.build_url()
7075
req = self.connect()
7176
return req['result']
72-
73-

examples/accounts/Accounts Examples Notebook.ipynb

+2-2
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@
457457
"language_info": {
458458
"codemirror_mode": {
459459
"name": "ipython",
460-
"version": 3.0
460+
"version": 3
461461
},
462462
"file_extension": ".py",
463463
"mimetype": "text/x-python",
@@ -489,4 +489,4 @@
489489
},
490490
"nbformat": 4,
491491
"nbformat_minor": 0
492-
}
492+
}

examples/accounts/get_all_blocks_mined.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88

99
api = Account(address=address, api_key=key)
1010
blocks = api.get_all_blocks_mined(offset=10000, blocktype='uncles')
11-
print(blocks)
11+
print(blocks)

examples/accounts/get_all_transactions.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
with open('../../api_key.json', mode='r') as key_file:
55
key = json.loads(key_file.read())['key']
66

7-
# address = ['0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a', '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a']
87
address = '0x49edf201c1e139282643d5e7c6fb0c7219ad1db7'
98

109
api = Account(address=address, api_key=key)
11-
transactions = api.get_all_transactions(offset=10000, sort='asc', internal=True)
10+
transactions = api.get_all_transactions(offset=10000, sort='asc',
11+
internal=False)
1212

13-
print(transactions[0])
13+
print(transactions[0])

examples/accounts/get_balance.py

-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,3 @@
99
api = Account(address=address, api_key=key)
1010
balance = api.get_balance()
1111
print(balance)
12-

examples/accounts/get_balance_multi.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
with open('../../api_key.json', mode='r') as key_file:
55
key = json.loads(key_file.read())['key']
66

7-
address = ['0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a', '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a']
7+
address = ['0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a',
8+
'0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a']
89

910
api = Account(address=address, api_key=key)
1011
balances = api.get_balance_multiple()
11-
print(balances)
12+
print(balances)

examples/accounts/get_blocks_mined.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88

99
api = Account(address=address, api_key=key)
1010
blocks = api.get_blocks_mined_page(page=1, offset=10000, blocktype='blocks')
11-
print(blocks)
11+
print(blocks)

examples/accounts/get_transaction_page.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88

99
api = Account(address=address, api_key=key)
1010
transactions = api.get_transaction_page(page=1, offset=10000, sort='des')
11-
print(transactions)
11+
print(transactions)

examples/contracts/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
+2-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
from etherscan.proxies import Proxies
1+
from etherscan.proxies import Proxies
22
import json
33

44
with open('../../api_key.json', mode='r') as key_file:
55
key = json.loads(key_file.read())['key']
66

7-
87
api = Proxies(api_key=key)
98
block = api.get_block_by_number(5747732)
10-
print(block['number'])
9+
print(block['number'])
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
from etherscan.proxies import Proxies
1+
from etherscan.proxies import Proxies
22
import json
33

44
with open('../../api_key.json', mode='r') as key_file:
55
key = json.loads(key_file.read())['key']
66

7-
87
api = Proxies(api_key=key)
98
tx_count = api.get_block_transaction_count_by_number(block_number='0x10FB78')
10-
print(int(tx_count, 16))
9+
print(int(tx_count, 16))
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from etherscan.proxies import Proxies
1+
from etherscan.proxies import Proxies
22
import json
33

44
with open('../../api_key.json', mode='r') as key_file:
55
key = json.loads(key_file.read())['key']
6-
6+
77
api = Proxies(api_key=key)
88
block = api.get_most_recent_block()
99
print(int(block, 16))
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from etherscan.proxies import Proxies
1+
from etherscan.proxies import Proxies
22
import json
33

44
with open('../../api_key.json', mode='r') as key_file:
55
key = json.loads(key_file.read())['key']
66

7-
87
api = Proxies(api_key=key)
9-
transaction = api.get_transaction_by_blocknumber_index(block_number='0x57b2cc', index='0x2')
10-
print(transaction['transactionIndex'])
8+
transaction = api.get_transaction_by_blocknumber_index(block_number='0x57b2cc',
9+
index='0x2')
10+
print(transaction['transactionIndex'])
+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from etherscan.proxies import Proxies
1+
from etherscan.proxies import Proxies
22
import json
33

44
with open('../../api_key.json', mode='r') as key_file:
55
key = json.loads(key_file.read())['key']
6-
6+
TX_HASH = '0x1e2910a262b1008d0616a0beb24c1a491d78771baa54a33e66065e03b1f46bc1'
77
api = Proxies(api_key=key)
88
transaction = api.get_transaction_by_hash(
9-
tx_hash='0x1e2910a262b1008d0616a0beb24c1a491d78771baa54a33e66065e03b1f46bc1')
10-
print(transaction['hash'])
9+
tx_hash=TX_HASH)
10+
print(transaction['hash'])
+2-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
from etherscan.proxies import Proxies
1+
from etherscan.proxies import Proxies
22
import json
33

44
with open('../../api_key.json', mode='r') as key_file:
55
key = json.loads(key_file.read())['key']
66

7-
87
api = Proxies(api_key=key)
98
count = api.get_transaction_count('0x6E2446aCfcec11CC4a60f36aFA061a9ba81aF7e0')
10-
print(int(count, 16))
9+
print(int(count, 16))
+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
from etherscan.proxies import Proxies
1+
from etherscan.proxies import Proxies
22
import json
33

44
with open('../../api_key.json', mode='r') as key_file:
55
key = json.loads(key_file.read())['key']
66

77
api = Proxies(api_key=key)
8-
receipt = api.get_transaction_receipt('0xb03d4625fd433ad05f036abdc895a1837a7d838ed39f970db69e7d832e41205d')
9-
print(receipt)
8+
receipt = api.get_transaction_receipt(
9+
'0xb03d4625fd433ad05f036abdc895a1837a7d838ed39f970db69e7d832e41205d')
10+
print(receipt)

0 commit comments

Comments
 (0)