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

View File

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

View File

@ -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}')