Преглед изворни кода

refactor into iter_planet_cogc

raylu пре 1 недеља
родитељ
комит
fc5cd7dc8e
2 измењених фајлова са 22 додато и 24 уклоњено
  1. 19 15
      company.py
  2. 3 9
      mat_competitors.py

+ 19 - 15
company.py

@@ -1,7 +1,6 @@
 from __future__ import annotations
 
 import collections
-import concurrent.futures
 import sys
 import typing
 
@@ -11,22 +10,12 @@ import integration
 def main() -> None:
 	code = sys.argv[1]
 	company: Company = cache.get('https://rest.fnar.net/company/code/' + code)
+	planets = {cp['PlanetName'] for cp in company['Planets']}
 
 	cogc_planets: dict[str, list[str]] = collections.defaultdict(list)
-	with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
-		futures: list[concurrent.futures.Future[Planet]] = [
-				executor.submit(cache.get, 'https://rest.fnar.net/planet/' + planet['PlanetNaturalId'])
-				for planet in company['Planets']]
-		for future in futures:
-			planet = future.result()
-			cogc = None
-			cogcs = planet['COGCPrograms']
-			if len(cogcs) > 1:
-				cogcs.sort(key=lambda c: c['StartEpochMs'], reverse=True)
-				cogc = cogcs[1]['ProgramType']
-				if cogc is not None:
-					cogc = cogc.removeprefix('ADVERTISING_')
-			print(planet['PlanetName'], cogc)
+	for planet, cogc in iter_planet_cogc():
+		if planet['PlanetName'] in planets:
+			print(planet['PlanetName'], cogc, sep='\t\t')
 			if cogc is not None:
 				cogc_planets[cogc].append(planet['PlanetName'])
 
@@ -43,12 +32,27 @@ def main() -> None:
 		expertise = experts.get(mat)
 		print(mat, expertise, ', '.join(cogc_planets.get(expertise, []))) # pyright: ignore[reportArgumentType, reportCallIssue]
 
+def iter_planet_cogc() -> typing.Iterator[tuple[Planet, Expertise | None]]:
+	all_planets: typing.Collection[Planet] = cache.get('https://universemap.taiyibureau.de/planet_data.json')
+	for planet in all_planets:
+		cogc = None
+		if len(cogcs := planet['COGCPrograms']) > 1:
+			cogcs.sort(key=lambda c: c['StartEpochMs'], reverse=True)
+			cogc = cogcs[1]['ProgramType']
+			if cogc is not None:
+				cogc = cogc.removeprefix('ADVERTISING_')
+		yield planet, typing.cast(Expertise | None, cogc)
+
+Expertise = typing.Literal['AGRICULTURE', 'CHEMISTRY', 'CONSTRUCTION', 'ELECTRONICS', 'FOOD_INDUSTRIES',
+	'FUEL_REFINING', 'MANUFACTURING', 'METALLURGY', 'RESOURCE_EXTRACTION']
+
 class Company(typing.TypedDict):
 	CompanyId: str
 	Planets: typing.Sequence[CompanyPlanet]
 
 class CompanyPlanet(typing.TypedDict):
 	PlanetNaturalId: str
+	PlanetName: str
 
 class Planet(typing.TypedDict):
 	PlanetId: str

+ 3 - 9
mat_competitors.py

@@ -18,17 +18,11 @@ def main() -> None:
 	with open('www/closest.json') as f:
 		planet_ids = {planet_id for planet_id, closest_cx in json.load(f).items() if closest_cx == cx}
 
-	all_planets: typing.Collection[company.Planet] = cache.get('https://universemap.taiyibureau.de/planet_data.json')
 	planets = []
-	for planet in all_planets:
-		if planet['PlanetId'] not in planet_ids:
-			continue
-		if len(cogcs := planet['COGCPrograms']) > 1:
-			cogcs.sort(key=lambda c: c['StartEpochMs'], reverse=True)
-			cogc = cogcs[1]['ProgramType']
-			if cogc is not None and cogc.removeprefix('ADVERTISING_') == expertise:
-				planets.append(planet['PlanetName'])
 	print(len(planets), expertise, 'planets near', cx)
+	for planet, cogc in company.iter_planet_cogc():
+		if planet['PlanetId'] in planet_ids and cogc == expertise:
+			planets.append(planet['PlanetName'])
 
 	coid_code: dict[str, str] = {}
 	coid_bases = collections.defaultdict(list)