1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-04-18 18:07:37 +03:00

made ships() return a List of NamedTuples

This way, we can access items without remembing indexes, if needed
This commit is contained in:
A_D 2020-10-09 17:21:05 +02:00 committed by Athanasius
parent 3b4d56b842
commit ad8fdd763a

View File

@ -1,7 +1,7 @@
"""CMDR Status information.""" """CMDR Status information."""
import csv import csv
from sys import platform from sys import platform
from typing import TYPE_CHECKING, Any, AnyStr, Dict, List, Optional, Sequence, Tuple, cast from typing import NamedTuple, TYPE_CHECKING, Any, AnyStr, Dict, List, Optional, Sequence, Tuple, Union, cast
if TYPE_CHECKING: if TYPE_CHECKING:
from EDMarketConnector import AppWindow from EDMarketConnector import AppWindow
@ -184,15 +184,25 @@ def export_status(data: Dict[str, Any], filename: AnyStr) -> None:
:param data: The data to generate the file from :param data: The data to generate the file from
:param filename: The target file :param filename: The target file
""" """
# TODO: Context manager with open(filename, 'w') as f:
h = csv.writer(open(filename, 'w')) h = csv.writer(f)
h.writerow(['Category', 'Value']) h.writerow(('Category', 'Value'))
for thing in status(data): for thing in status(data):
h.writerow(list(thing)) h.writerow(list(thing))
def ships(companion_data: Dict[str, Any]) -> List[Tuple[str, str, str, str, str, str]]: class ShipRet(NamedTuple):
# TODO: Replace this with a NamedTuple """ShipRet is a NamedTuple containing the return data from stats.ships."""
id: str
type: str
name: str
system: str
station: str
value: str
def ships(companion_data: Dict[str, Any]) -> List[ShipRet]:
""" """
Return a list of 5 tuples of ship information. Return a list of 5 tuples of ship information.
@ -206,40 +216,37 @@ def ships(companion_data: Dict[str, Any]) -> List[Tuple[str, str, str, str, str,
ships.insert(0, ships.pop(current)) # Put current ship first ships.insert(0, ships.pop(current)) # Put current ship first
if not companion_data['commander'].get('docked'): if not companion_data['commander'].get('docked'):
out: List[Tuple[str, str, str, str, str, str]] = [] out: List[ShipRet] = []
# Set current system, not last docked # Set current system, not last docked
out.append( out.append(ShipRet(
( id=str(ships[0]['id']),
str(ships[0]['id']), type=ship_map.get(ships[0]['name'].lower(), ships[0]['name']),
ship_map.get(ships[0]['name'].lower(), ships[0]['name']), name=str(ships[0].get('shipName', '')),
str(ships[0].get('shipName', '')), system=companion_data['lastSystem']['name'],
companion_data['lastSystem']['name'], station='',
'', value=str(ships[0]['value']['total'])
str(ships[0]['value']['total']) ))
)
)
out.extend( out.extend(
( ShipRet(
str(ship['id']), id=str(ship['id']),
ship_map.get(ship['name'].lower(), ship['name']), type=ship_map.get(ship['name'].lower(), ship['name']),
ship.get('shipName', ''), name=ship.get('shipName', ''),
ship['starsystem']['name'], system=ship['starsystem']['name'],
ship['station']['name'], station=ship['station']['name'],
str(ship['value']['total']) value=str(ship['value']['total'])
) for ship in ships[1:] if ship ) for ship in ships[1:] if ship
) )
return out return out
return [ return [
( ShipRet(
str(ship['id']), id=str(ship['id']),
ship_map.get(ship['name'].lower(), ship['name']), type=ship_map.get(ship['name'].lower(), ship['name']),
ship.get('shipName', ''), name=ship.get('shipName', ''),
ship['starsystem']['name'], system=ship['starsystem']['name'],
ship['station']['name'], station=ship['station']['name'],
str(ship['value']['total']) value=str(ship['value']['total'])
) for ship in ships if ship is not None ) for ship in ships if ship is not None
] ]
@ -251,11 +258,11 @@ def export_ships(companion_data: Dict[str, Any], filename: AnyStr) -> None:
:param companion_data: Data from which to generate the ship list :param companion_data: Data from which to generate the ship list
:param filename: The target file :param filename: The target file
""" """
# TODO: context manager with open(filename, 'w') as f:
h = csv.writer(open(filename, 'w')) h = csv.writer(f)
h.writerow(['Id', 'Ship', 'Name', 'System', 'Station', 'Value']) h.writerow(['Id', 'Ship', 'Name', 'System', 'Station', 'Value'])
for thing in ships(companion_data): for thing in ships(companion_data):
h.writerow(list(thing)) h.writerow(list(thing))
class StatsDialog(): class StatsDialog():