1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-05-29 14:49:29 +03:00

commodity.py: Use 'with' for output file.

This commit is contained in:
Athanasius 2021-04-07 12:31:34 +01:00
parent 5cc80174d2
commit f3383858c3

View File

@ -44,27 +44,30 @@ def export(data, kind=COMMODITY_DEFAULT, filename=None) -> None:
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)
with open(filename, 'wt') as h: # codecs can't automatically handle line endings, so encode manually where required
h.write(header)
for commodity in data['lastStarport']['commodities']:
line = sep.join((
rowheader,
commodity['name'],
commodity['sellPrice'] and str(int(commodity['sellPrice'])) or '',
commodity['buyPrice'] and str(int(commodity['buyPrice'])) or '',
str(int(commodity['demand'])) if commodity['demandBracket'] else '',
bracketmap[commodity['demandBracket']],
str(int(commodity['stock'])) if commodity['stockBracket'] else '',
bracketmap[commodity['stockBracket']]
))
for commodity in data['lastStarport']['commodities']:
line = sep.join((
rowheader,
commodity['name'],
commodity['sellPrice'] and str(int(commodity['sellPrice'])) or '',
commodity['buyPrice'] and str(int(commodity['buyPrice'])) or '',
str(int(commodity['demand'])) if commodity['demandBracket'] else '',
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'))
else:
line = sep.join((line, data['timestamp'] + '\n'))
h.write(line)
h.close()
h.write(line)