Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Smith committed Dec 18, 2014
1 parent b994198 commit 2ebd752
Showing 1 changed file with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions tradedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,10 @@ def _loadCategories(self):
SELECT category_id, name
FROM Category
"""
self.categoryByID = { ID: Category(ID, name, []) for (ID, name) in self.cur.execute(stmt) }
self.categoryByID = {
ID: Category(ID, name, [])
for (ID, name) in self.cur.execute(stmt)
}

self.tdenv.DEBUG1("Loaded {} Categories", len(self.categoryByID))

Expand All @@ -938,7 +941,11 @@ def lookupCategory(self, name):
"""
Look up a category by name
"""
return TradeDB.listSearch("Category", name, self.categoryByID.values(), key=lambda cat: cat.dbname)
return TradeDB.listSearch(
"Category", name,
self.categoryByID.values(),
key=lambda cat: cat.dbname
)


def items(self):
Expand All @@ -959,7 +966,11 @@ def _loadItems(self):
itemByID, itemByName = {}, {}
for (ID, name, categoryID) in self.cur.execute(stmt):
category = self.categoryByID[categoryID]
item = Item(ID, name, category, '{}/{}'.format(category.dbname, name), None)
item = Item(
ID, name, category,
'{}/{}'.format(category.dbname, name),
None
)
itemByID[ID] = item
itemByName[name] = item
category.items.append(item)
Expand All @@ -977,20 +988,29 @@ def _loadItems(self):
item = itemByID[itemID]
item.altname = altName
itemByName[altName] = item
self.tdenv.DEBUG1("'{}' alias for #{} '{}'", altName, itemID, item.fullname)
self.tdenv.DEBUG1(
"'{}' alias for #{} '{}'",
altName, itemID, item.fullname
)

self.itemByID = itemByID
self.itemByName = itemByName

self.tdenv.DEBUG1("Loaded {:n} Items, {:n} AltItemNames",
len(self.itemByID), aliases)
self.tdenv.DEBUG1(
"Loaded {:n} Items, {:n} AltItemNames",
len(self.itemByID), aliases
)


def lookupItem(self, name):
"""
Look up an Item by name using "CATEGORY/Item"
"""
return TradeDB.listSearch("Item", name, self.itemByName.items(), key=lambda kvTup: kvTup[0], val=lambda kvTup: kvTup[1])
return TradeDB.listSearch(
"Item", name, self.itemByName.items(),
key=lambda kvTup: kvTup[0],
val=lambda kvTup: kvTup[1]
)


############################################################
Expand Down Expand Up @@ -1026,16 +1046,30 @@ def loadStationTrades(self, fromStationIDs):
if self.tradingCount is None:
self.tradingCount = 0

for (itemID, srcStnID, dstStnID, srcPriceCr, profit, stock, stockLevel, demand, demandLevel, srcAge, dstAge) in self.cur:
for (
itemID,
srcStnID, dstStnID,
srcPriceCr, profit,
stock, stockLevel,
demand, demandLevel,
srcAge, dstAge
) in self.cur:
if srcStnID != prevSrcStnID:
srcStn, prevSrcStnID, prevDstStnID = stations[srcStnID], srcStnID, None
srcStn = stations[srcStnID]
prevSrcStnID = srcStnID
prevDstStnID = None
assert srcStn.tradingWith is None
srcStn.tradingWith = {}
if dstStnID != prevDstStnID:
dstStn, prevDstStnID = stations[dstStnID], dstStnID
tradingWith = srcStn.tradingWith[dstStn] = []
self.tradingCount += 1
tradingWith.append(Trade(items[itemID], itemID, srcPriceCr, profit, stock, stockLevel, demand, demandLevel, srcAge, dstAge))
tradingWith.append(Trade(
items[itemID], itemID,
srcPriceCr, profit,
stock, stockLevel,
demand, demandLevel,
srcAge, dstAge))


def getTrades(self, src, dst):
Expand Down

0 comments on commit 2ebd752

Please sign in to comment.