mirror of
https://github.com/EDCD/EDMarketConnector.git
synced 2025-04-14 08:17:13 +03:00
Cleanup commodity.py (PEP8, code simplification)
I removed most of the overly complex oneliners for equivalent multi-line constructs. The code behaviour should remain the same. Otherwise mostly reformatting happened here, spaces after commas, that sort of thing.
This commit is contained in:
parent
65793bce24
commit
76e561d231
72
commodity.py
72
commodity.py
@ -3,45 +3,72 @@
|
||||
|
||||
from os.path import join
|
||||
import hashlib
|
||||
import codecs
|
||||
import numbers
|
||||
import time
|
||||
|
||||
from config import config
|
||||
|
||||
bracketmap = { 0: '',
|
||||
1: 'Low',
|
||||
2: 'Med',
|
||||
3: 'High', }
|
||||
bracketmap = {
|
||||
0: '',
|
||||
1: 'Low',
|
||||
2: 'Med',
|
||||
3: 'High',
|
||||
}
|
||||
|
||||
(COMMODITY_DEFAULT, COMMODITY_BPC, COMMODITY_CSV) = range(3)
|
||||
|
||||
def export(data, kind=COMMODITY_DEFAULT, filename=None):
|
||||
|
||||
def export(data, kind=COMMODITY_DEFAULT, filename=None):
|
||||
querytime = config.getint('querytime') or int(time.time())
|
||||
|
||||
if not filename:
|
||||
filename = join(config.get('outdir'), '%s.%s.%s.%s' % (data['lastSystem']['name'].strip(), data['lastStarport']['name'].strip(), time.strftime('%Y-%m-%dT%H.%M.%S', time.localtime(querytime)), kind==COMMODITY_BPC and 'bpc' or 'csv'))
|
||||
kind_str = 'bpc'
|
||||
if kind != COMMODITY_BPC:
|
||||
kind_str = 'csv'
|
||||
|
||||
filename = '{system}.{starport}.{time}.{kind}'.format(
|
||||
system=data['lastSystem']['name'].strip(),
|
||||
starport=data['lastStarport']['name'].strip(),
|
||||
time=time.strftime('%Y-%m-%dT%H.%M.%S', time.localtime(querytime)),
|
||||
kind=kind_str
|
||||
)
|
||||
|
||||
filename = join(config.get('outdir'), filename)
|
||||
|
||||
if kind == COMMODITY_CSV:
|
||||
sep = ';'
|
||||
header = sep.join(['System','Station','Commodity','Sell','Buy','Demand','','Supply','','Date','\n'])
|
||||
rowheader = sep.join([data['lastSystem']['name'], data['lastStarport']['name']])
|
||||
sep = ';' # BUG: for fixing later after cleanup
|
||||
header = sep.join(('System', 'Station', 'Commodity', 'Sell', 'Buy', 'Demand', '', 'Supply', '', 'Date', '\n'))
|
||||
rowheader = sep.join((data['lastSystem']['name'], data['lastStarport']['name']))
|
||||
|
||||
elif kind == COMMODITY_BPC:
|
||||
sep = ';'
|
||||
header = sep.join(['userID', 'System','Station','Commodity','Sell','Buy','Demand','','Supply','','Date\n'])
|
||||
header = sep.join(
|
||||
('userID', 'System', 'Station', 'Commodity', 'Sell', 'Buy', 'Demand', '', 'Supply', '', 'Date\n')
|
||||
)
|
||||
|
||||
cmdr = data['commander']['name'].strip()
|
||||
rowheader = sep.join([(config.getint('anonymous') and hashlib.md5(cmdr.encode('utf-8')).hexdigest()) or (sep in cmdr and '"%s"' % cmdr) or cmdr, data['lastSystem']['name'], data['lastStarport']['name']])
|
||||
|
||||
header_cmdr = cmdr
|
||||
if config.getint('anonymous'):
|
||||
header_cmdr = hashlib.md5(cmdr.encode('utf-8')).hexdigest()
|
||||
|
||||
elif sep in cmdr:
|
||||
header_cmdr = f'"{cmdr}"'
|
||||
|
||||
rowheader = sep.join((header_cmdr, data['lastSystem']['name'], data['lastStarport']['name']))
|
||||
|
||||
else:
|
||||
sep = ','
|
||||
header = sep.join(['System','Station','Commodity','Sell','Buy','Demand','','Supply','','Average','FDevID','Date\n'])
|
||||
rowheader = sep.join([data['lastSystem']['name'], data['lastStarport']['name']])
|
||||
header = sep.join(
|
||||
('System', 'Station', 'Commodity', 'Sell', 'Buy', 'Demand', '', 'Supply', '', 'Average', 'FDevID', 'Date\n')
|
||||
)
|
||||
|
||||
h = open(filename, 'wt') # codecs can't automatically handle line endings, so encode manually where required
|
||||
rowheader = sep.join((data['lastSystem']['name'], data['lastStarport']['name']))
|
||||
|
||||
h = open(filename, 'wt') # codecs can't automatically handle line endings, so encode manually where required
|
||||
h.write(header)
|
||||
|
||||
for commodity in data['lastStarport']['commodities']:
|
||||
line = sep.join([
|
||||
line = sep.join((
|
||||
rowheader,
|
||||
commodity['name'],
|
||||
commodity['sellPrice'] and str(int(commodity['sellPrice'])) or '',
|
||||
@ -50,11 +77,14 @@ def export(data, kind=COMMODITY_DEFAULT, filename=None):
|
||||
bracketmap[commodity['demandBracket']],
|
||||
str(int(commodity['stock'])) if commodity['stockBracket'] else '',
|
||||
bracketmap[commodity['stockBracket']]
|
||||
])
|
||||
if kind==COMMODITY_DEFAULT:
|
||||
line = sep.join([line, str(int(commodity['meanPrice'])), str(commodity['id']), data['timestamp'] + '\n'])
|
||||
))
|
||||
|
||||
if kind == COMMODITY_DEFAULT:
|
||||
line = sep.join((line, str(int(commodity['meanPrice'])), str(commodity['id']), data['timestamp'] + '\n'))
|
||||
|
||||
else:
|
||||
line = sep.join([line, data['timestamp'] + '\n'])
|
||||
line = sep.join((line, data['timestamp'] + '\n'))
|
||||
|
||||
h.write(line)
|
||||
|
||||
h.close()
|
||||
|
Loading…
x
Reference in New Issue
Block a user