Update analyzer.py, command_line.py and two other files

This commit is contained in:
Richard Bronkhorst 2023-06-13 09:27:13 +02:00
parent 5917f94a82
commit 7385362d4f
4 changed files with 36 additions and 26 deletions

11
nullptr/analyzer.py Normal file
View File

@ -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

View File

@ -25,6 +25,12 @@ class CommandLine:
self.reloading = False self.reloading = False
self.stopping = 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): def stop(self):
self.stopping = True self.stopping = True

View File

@ -1,5 +1,6 @@
from nullptr.command_line import CommandLine from nullptr.command_line import CommandLine
from nullptr.store import Store from nullptr.store import Store
from nullptr.analyzer import Analyzer
import argparse import argparse
from nullptr.models.agent import Agent from nullptr.models.agent import Agent
from nullptr.models.system import System from nullptr.models.system import System
@ -20,10 +21,20 @@ class Commander(CommandLine):
self.agent = self.select_agent(agent) self.agent = self.select_agent(agent)
self.api = Api(self.store, self.agent) self.api = Api(self.store, self.agent)
self.atlas_builder = AtlasBuilder(self.store, self.api) self.atlas_builder = AtlasBuilder(self.store, self.api)
self.analyzer = Analyzer(self.store)
self.stop_auto= False self.stop_auto= False
super().__init__() 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): def select_agent(self, agent_str):
if agent_str is not None: if agent_str is not None:
return self.store.get(Agent, agent_str) return self.store.get(Agent, agent_str)
@ -65,28 +76,10 @@ class Commander(CommandLine):
r = self.api.jumps(waypoint) r = self.api.jumps(waypoint)
pprint(r) pprint(r)
def do_test(self): def do_query(self):
start_time = time() location = None # self.ask_obj(Waypoint, 'Where are you? ')
total = 0 resource = input('what resource?').upper()
for m in self.store.all(Marketplace): sellbuy = self.ask_multichoice(['sell','buy'], 'do you want to sell or buy?')
total += 1 print('Found markets:')
dur = time() - start_time for m in self.analyzer.find_markets(resource, sellbuy, location):
print(f'{total} markets in {dur:.2f} seconds') print(m)
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')

View File

@ -100,4 +100,4 @@ class Store:
self.store(obj) self.store(obj)
self.dirty_objects = set() self.dirty_objects = set()
dur = time() - start_time dur = time() - start_time
print(f'flush done {it} items {dur:.2f}') # print(f'flush done {it} items {dur:.2f}')