From 7385362d4fc7358a123adc60293117eb92564275 Mon Sep 17 00:00:00 2001 From: Richard Bronkhorst Date: Tue, 13 Jun 2023 09:27:13 +0200 Subject: [PATCH] Update analyzer.py, command_line.py and two other files --- nullptr/analyzer.py | 11 +++++++++++ nullptr/command_line.py | 6 ++++++ nullptr/commander.py | 43 +++++++++++++++++------------------------ nullptr/store.py | 2 +- 4 files changed, 36 insertions(+), 26 deletions(-) create mode 100644 nullptr/analyzer.py diff --git a/nullptr/analyzer.py b/nullptr/analyzer.py new file mode 100644 index 0000000..3e951e8 --- /dev/null +++ b/nullptr/analyzer.py @@ -0,0 +1,11 @@ +from nullptr.models.marketplace import Marketplace + +class Analyzer: + def __init__(self, store): + self.store = store + + def find_markets(self, resource, sellbuy, location): + for m in self.store.all(Marketplace): + resources = m.imports if sellbuy == 'sell' else m.exports + if resource in resources: + yield m diff --git a/nullptr/command_line.py b/nullptr/command_line.py index d659584..c3ab875 100644 --- a/nullptr/command_line.py +++ b/nullptr/command_line.py @@ -25,6 +25,12 @@ class CommandLine: self.reloading = False self.stopping = False + def ask_multichoice(self, choices, prompt): + choice = None + while choice not in choices: + choice = input(prompt).lower() + return choice + def stop(self): self.stopping = True diff --git a/nullptr/commander.py b/nullptr/commander.py index 5e1a21e..bbe74d5 100644 --- a/nullptr/commander.py +++ b/nullptr/commander.py @@ -1,5 +1,6 @@ from nullptr.command_line import CommandLine from nullptr.store import Store +from nullptr.analyzer import Analyzer import argparse from nullptr.models.agent import Agent from nullptr.models.system import System @@ -20,10 +21,20 @@ class Commander(CommandLine): self.agent = self.select_agent(agent) self.api = Api(self.store, self.agent) self.atlas_builder = AtlasBuilder(self.store, self.api) + self.analyzer = Analyzer(self.store) self.stop_auto= False super().__init__() + def ask_obj(self, typ, prompt): + obj = None + while obj is None: + symbol = input(prompt) + obj = self.store.get(typ, symbol.upper()) + if obj is None: + print('not found') + return obj + def select_agent(self, agent_str): if agent_str is not None: return self.store.get(Agent, agent_str) @@ -65,28 +76,10 @@ class Commander(CommandLine): r = self.api.jumps(waypoint) pprint(r) - def do_test(self): - start_time = time() - total = 0 - for m in self.store.all(Marketplace): - total += 1 - dur = time() - start_time - print(f'{total} markets in {dur:.2f} seconds') - start_time = time() - total = 0 - for m in self.store.all(Jumpgate): - total += 1 - dur = time() - start_time - print(f'{total} jumpgates in {dur:.2f} seconds') - start_time = time() - total = 0 - for m in self.store.all(Waypoint): - total += 1 - dur = time() - start_time - print(f'{total} waypoints in {dur:.2f} seconds') - start_time = time() - total = 0 - for m in self.store.all(System): - total += 1 - dur = time() - start_time - print(f'{total} systems in {dur:.2f} seconds') + def do_query(self): + location = None # self.ask_obj(Waypoint, 'Where are you? ') + resource = input('what resource?').upper() + sellbuy = self.ask_multichoice(['sell','buy'], 'do you want to sell or buy?') + print('Found markets:') + for m in self.analyzer.find_markets(resource, sellbuy, location): + print(m) diff --git a/nullptr/store.py b/nullptr/store.py index b71be1e..e380e47 100644 --- a/nullptr/store.py +++ b/nullptr/store.py @@ -100,4 +100,4 @@ class Store: self.store(obj) self.dirty_objects = set() dur = time() - start_time - print(f'flush done {it} items {dur:.2f}') + # print(f'flush done {it} items {dur:.2f}')