Skip to content

Commit 058534c

Browse files
UtumnoInfernio
authored andcommitted
WIP Counter SSS
Took the opportunity to unnamespace the defaultdict - those classes are builtin status (plus wraps better)
1 parent d382d3f commit 058534c

6 files changed

Lines changed: 64 additions & 84 deletions

File tree

Mopy/bash/game/oblivion/patcher/special.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
# https://github.com/wrye-bash
2222
#
2323
# =============================================================================
24-
import collections
2524
import os
2625
import re
26+
from collections import Counter, defaultdict
2727
from ....bolt import GPath, sio, SubProgress, CsvReader
2828
from ....patcher import getPatchesPath
2929
from ....parsers import LoadFactory, ModFile
@@ -142,7 +142,7 @@ def getBook(objectId,eid,full,value,iconPath,modelPath,modb_p):
142142
buffWrite(u'\r\n')
143143
book.text = re.sub(u'\r\n',u'<br>\r\n',buff.getvalue())
144144
#--Get Ingredients by Effect
145-
effect_ingred = collections.defaultdict(list)
145+
effect_ingred = defaultdict(list)
146146
for fid,(eid,full,effects) in id_ingred.iteritems():
147147
for index,(mgef,actorValue) in enumerate(effects):
148148
effectName = mgef_name[mgef]
@@ -184,7 +184,7 @@ def initPatchFile(self, patchFile):
184184
if not self.isActive: return
185185
patchFile.indexMGEFs = True
186186
self.id_ingred = {}
187-
self.effect_ingred = collections.defaultdict(list)
187+
self.effect_ingred = defaultdict(list)
188188
self.SEFF = MGEFCode('SEFF')
189189
self.DebugPrintOnce = 0
190190

@@ -287,7 +287,7 @@ def getBook(patchFile, objectId):
287287
buff.write(u'\r\n')
288288
book.text = re.sub(u'\r\n',u'<br>\r\n',buff.getvalue())
289289
#--Get Ingredients by Effect
290-
effect_ingred = self.effect_ingred = collections.defaultdict(list)
290+
effect_ingred = self.effect_ingred = defaultdict(list)
291291
for fid,(eid,full,effects_list) in id_ingred.iteritems():
292292
for index,effect in enumerate(effects_list):
293293
mgef, actorValue = effect[0], effect[5]
@@ -353,7 +353,7 @@ def buildPatchLog(self, log):
353353
if not self.isActive: return
354354
#--Log
355355
self._pLog(log, self.mod_count)
356-
self.mod_count = collections.defaultdict(int)
356+
self.mod_count = Counter()
357357

358358
class _ACoblExhaustion(SpecialPatcher):
359359
"""Modifies most Greater power to work with Cobl's power exhaustion
@@ -432,7 +432,7 @@ def scanModFile(self,modFile,progress):
432432
def buildPatch(self,log,progress):
433433
"""Edits patch file as desired. Will write to log."""
434434
if not self.isActive: return
435-
count = {}
435+
count = Counter()
436436
exhaustId = (self.cobl,0x05139B)
437437
keep = self.patchFile.getKeeper()
438438
for record in self.patchFile.SPEL.records:
@@ -461,8 +461,7 @@ def buildPatch(self,log,progress):
461461
effect.scriptEffect = scriptEffect
462462
record.effects.append(effect)
463463
keep(record.fid)
464-
srcMod = record.fid[0]
465-
count[srcMod] = count.get(srcMod,0) + 1
464+
count[record.fid[0]] += 1
466465
#--Log
467466
self._pLog(log, count)
468467

@@ -622,7 +621,7 @@ def buildPatch(self,log,progress):
622621
id_info = self.id_info
623622
modFile = self.patchFile
624623
keep = self.patchFile.getKeeper()
625-
changed = collections.defaultdict(int)
624+
changed = Counter()
626625
mFactable = []
627626
for record in modFile.FACT.getActiveRecords():
628627
if record.fid not in id_info: continue
@@ -648,8 +647,7 @@ def buildPatch(self,log,progress):
648647
rank.insigniaPath = \
649648
u'Menus\\Stats\\Cobl\\generic%02d.dds' % rank.rank
650649
keep(record.fid)
651-
mod = record.fid[0]
652-
changed[mod] += 1
650+
changed[record.fid[0]] += 1
653651
#--MFact record
654652
record = modFile.FACT.getRecord(mFactLong)
655653
if record:
@@ -847,7 +845,7 @@ def initPatchFile(self, patchFile):
847845
self.cyrodiilQuests = set()
848846
self.srcs = [GPath(u'Oblivion.esm')]
849847
self.isActive = self.srcs[0] in patchFile.loadSet
850-
self.mod_eids = collections.defaultdict(list)
848+
self.mod_eids = defaultdict(list)
851849

852850
def getTypes(self):
853851
return ['QUST']
@@ -890,7 +888,7 @@ def buildPatchLog(self,log):
890888
log(u'* %s: %d' % (mod.s, len(eids)))
891889
for eid in sorted(eids):
892890
log(u' * %s' % eid)
893-
self.mod_eids = collections.defaultdict(list)
891+
self.mod_eids = defaultdict(list)
894892

895893
# Alchemical Catalogs ---------------------------------------------------------
896894
_ingred_alchem = (

Mopy/bash/patcher/patchers/base.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# =============================================================================
2424

2525
"""This module contains base patcher classes."""
26-
import collections
26+
from collections import Counter
2727
from operator import itemgetter
2828
# Internal
2929
from .. import getPatchesPath
@@ -45,7 +45,7 @@ def initPatchFile(self, patchFile):
4545
super(CBash_ListPatcher, self).initPatchFile(patchFile)
4646
# used in all subclasses except CBash_RacePatcher,
4747
# CBash_PatchMerger, CBash_UpdateReferences
48-
self.mod_count = collections.defaultdict(int)
48+
self.mod_count = Counter()
4949

5050
#--Patch Phase ------------------------------------------------------------
5151
def getConfigChecked(self):
@@ -94,7 +94,7 @@ def __init__(self, label, tweak_tip, key, *choices, **kwargs):
9494
super(CBash_MultiTweakItem, self).__init__(label, tweak_tip, key,
9595
*choices, **kwargs)
9696
# extra CBash_MultiTweakItem attribute, mod -> num of tweaked records
97-
self.mod_count = collections.defaultdict(int)
97+
self.mod_count = Counter()
9898

9999
#--Patch Phase ------------------------------------------------------------
100100
def getTypes(self):
@@ -107,7 +107,7 @@ def getTypes(self):
107107
def buildPatchLog(self,log):
108108
"""Will write to log."""
109109
self._patchLog(log, self.mod_count)
110-
self.mod_count = collections.defaultdict(int)
110+
self.mod_count = Counter()
111111

112112
class MultiTweaker(AMultiTweaker,Patcher):
113113

@@ -286,7 +286,7 @@ def buildPatch(self,log,progress):
286286
if not self.isActive: return
287287
old_new,old_eid,new_eid = self.old_new,self.old_eid,self.new_eid
288288
keep = self.patchFile.getKeeper()
289-
count = collections.defaultdict(int)
289+
count = Counter()
290290
def swapper(oldId):
291291
newId = old_new.get(oldId,None)
292292
return newId if newId else oldId
@@ -531,7 +531,7 @@ def _clog(self,log):
531531
log(self.__class__.logMsg % sum(mod_count.values()))
532532
for srcMod in load_order.get_ordered(mod_count.keys()):
533533
log(u' * %s: %d' % (srcMod.s,mod_count[srcMod]))
534-
self.mod_count = collections.defaultdict(int)
534+
self.mod_count = Counter()
535535

536536
# helpers WIP
537537
def _parse_texts(self, parser_class, progress):

Mopy/bash/patcher/patchers/importers.py

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
# =============================================================================
2424

2525
"""This module contains the oblivion importer patcher classes."""
26-
import collections
2726
import re
27+
from collections import defaultdict, Counter
2828
from itertools import chain
2929
from operator import attrgetter
3030
# Internal
@@ -50,7 +50,7 @@ class _SimpleImporter(ImportPatcher):
5050
def initPatchFile(self, patchFile):
5151
super(_SimpleImporter, self).initPatchFile(patchFile)
5252
#--(attribute-> value) dicts keyed by long fid.
53-
self.id_data = collections.defaultdict(dict)
53+
self.id_data = defaultdict(dict)
5454
self.srcClasses = set() #--Record classes actually provided by src
5555
# mods/files.
5656
self.classestemp = set()
@@ -195,7 +195,7 @@ def buildPatch(self, log, progress, types=None):
195195
if not self.isActive: return
196196
modFileTops = self.patchFile.tops
197197
keep = self.patchFile.getKeeper()
198-
type_count = collections.defaultdict(int)
198+
type_count = Counter()
199199
types = filter(modFileTops.__contains__, types if types else map(
200200
attrgetter('classType'), self.srcClasses))
201201
for top_mod_rec in types:
@@ -213,9 +213,8 @@ class _RecTypeModLogging(CBash_ImportPatcher):
213213

214214
def initPatchFile(self, patchFile):
215215
super(_RecTypeModLogging, self).initPatchFile(patchFile)
216-
self.mod_count = collections.defaultdict(
217-
lambda: collections.defaultdict(int))
218-
self.fid_attr_value = collections.defaultdict(dict) # used in some
216+
self.mod_count = defaultdict(Counter)
217+
self.fid_attr_value = defaultdict(dict) # used in some
219218

220219
def _clog(self, log):
221220
"""Used in: CBash_SoundPatcher, CBash_ImportScripts,
@@ -235,8 +234,7 @@ def _clog(self, log):
235234
'count': sum(mod_count[group_type].values())})
236235
for srcMod in load_order.get_ordered(mod_count[group_type].keys()):
237236
log(u' * %s: %d' % (srcMod.s, mod_count[group_type][srcMod]))
238-
self.mod_count = collections.defaultdict(
239-
lambda: collections.defaultdict(int))
237+
self.mod_count = defaultdict(Counter)
240238

241239
def scan(self,modFile,record,bashTags):
242240
"""Records information needed to apply the patch."""
@@ -262,7 +260,7 @@ class CellImporter(_ACellImporter, ImportPatcher):
262260
#--Patch Phase ------------------------------------------------------------
263261
def initPatchFile(self, patchFile):
264262
super(CellImporter, self).initPatchFile(patchFile)
265-
self.cellData = collections.defaultdict(dict)
263+
self.cellData = defaultdict(dict)
266264
# TODO: docs: recAttrs vs tag_attrs - extra in PBash:
267265
# 'unused1','unused2','unused3'
268266
self.recAttrs = bush.game.cellRecAttrs # dict[unicode, tuple[str]]
@@ -326,7 +324,7 @@ def checkMasterCellBlockData(cellBlock):
326324
# (attributes (among attrs) -> their values for this mod). It is
327325
# used to update cellData with cells that change those attributes'
328326
# values from the value in any of srcMod's masters.
329-
tempCellData = collections.defaultdict(dict)
327+
tempCellData = defaultdict(dict)
330328
tempCellData['Maps'] = {} # unused !
331329
srcInfo = bosh.modInfos[srcMod]
332330
srcFile = ModFile(srcInfo,loadFactory)
@@ -437,7 +435,7 @@ def handlePatchCellBlock(patchCellBlock):
437435
return modified
438436
if not self.isActive: return
439437
keep = self.patchFile.getKeeper()
440-
cellData, count = self.cellData, collections.defaultdict(int)
438+
cellData, count = self.cellData, Counter()
441439
for cellBlock in self.patchFile.CELL.cellBlocks:
442440
if cellBlock.cell.fid in cellData and handlePatchCellBlock(cellBlock):
443441
count[cellBlock.cell.fid[0]] += 1
@@ -479,7 +477,7 @@ class CBash_CellImporter(_ACellImporter,CBash_ImportPatcher):
479477
def initPatchFile(self, patchFile):
480478
super(CBash_CellImporter, self).initPatchFile(patchFile)
481479
if not self.isActive: return
482-
self.fid_attr_value = collections.defaultdict(dict)
480+
self.fid_attr_value = defaultdict(dict)
483481
self.tag_attrs = {
484482
u'C.Climate': ('climate','IsBehaveLikeExterior'),
485483
u'C.Music': ('musicType',),
@@ -973,7 +971,7 @@ class CBash_KFFZPatcher(CBash_ImportPatcher, _AKFFZPatcher):
973971
def initPatchFile(self, patchFile):
974972
super(CBash_KFFZPatcher, self).initPatchFile(patchFile)
975973
if not self.isActive: return
976-
self.id_animations = collections.defaultdict(list)
974+
self.id_animations = defaultdict(list)
977975

978976
def getTypes(self):
979977
"""Returns the group types that this patcher checks"""
@@ -1160,7 +1158,7 @@ def buildPatch(self,log,progress): # buildPatch1:no modFileTops, for type..
11601158
if not self.isActive: return
11611159
keep = self.patchFile.getKeeper()
11621160
merged_deleted = self.id_merged_deleted
1163-
mod_count = collections.defaultdict(int)
1161+
mod_count = Counter()
11641162
for rec_type in ('NPC_','CREA'):
11651163
for record in getattr(self.patchFile,rec_type).records:
11661164
fid = record.fid
@@ -1171,8 +1169,7 @@ def buildPatch(self,log,progress): # buildPatch1:no modFileTops, for type..
11711169
changed = True
11721170
if changed:
11731171
keep(record.fid)
1174-
mod = record.fid[0]
1175-
mod_count[mod] += 1
1172+
mod_count[record.fid[0]] += 1
11761173
self.id_merged_deleted.clear()
11771174
self._patchLog(log,mod_count)
11781175

@@ -1799,7 +1796,7 @@ def buildPatch(self,log,progress): # buildPatch1:no modFileTops, for type..
17991796
if not self.isActive: return
18001797
keep = self.patchFile.getKeeper()
18011798
id_deltas = self.id_deltas
1802-
mod_count = collections.defaultdict(int)
1799+
mod_count = Counter()
18031800
for inv_type in bush.game.inventoryTypes:
18041801
for record in getattr(self.patchFile,inv_type).records:
18051802
changed = False
@@ -1823,8 +1820,7 @@ def buildPatch(self,log,progress): # buildPatch1:no modFileTops, for type..
18231820
changed = True
18241821
if changed:
18251822
keep(record.fid)
1826-
mod = record.fid[0]
1827-
mod_count[mod] += 1
1823+
mod_count[record.fid[0]] += 1
18281824
self.id_deltas.clear()
18291825
self._patchLog(log,mod_count)
18301826

@@ -2074,7 +2070,7 @@ def buildPatch(self,log,progress): # buildPatch1:no modFileTops, for type..
20742070
if not self.isActive: return
20752071
keep = self.patchFile.getKeeper()
20762072
merged_deleted = self.id_merged_deleted
2077-
mod_count = collections.defaultdict(int)
2073+
mod_count = Counter()
20782074
for rec_type in ('NPC_','CREA'):
20792075
for record in getattr(self.patchFile,rec_type).records:
20802076
fid = record.fid
@@ -2086,8 +2082,7 @@ def buildPatch(self,log,progress): # buildPatch1:no modFileTops, for type..
20862082
changed = True
20872083
if changed:
20882084
keep(record.fid)
2089-
mod = record.fid[0]
2090-
mod_count[mod] += 1
2085+
mod_count[record.fid[0]] += 1
20912086
self.id_merged_deleted.clear()
20922087
self._patchLog(log,mod_count)
20932088

@@ -2235,7 +2230,7 @@ def buildPatch(self,log,progress):# buildPatch0
22352230
modFile = self.patchFile
22362231
keep = self.patchFile.getKeeper()
22372232
id_full = self.id_full
2238-
type_count = collections.defaultdict(int)
2233+
type_count = Counter()
22392234
for act_type in self.activeTypes:
22402235
if act_type not in modFile.tops: continue
22412236
if act_type == 'CELL':
@@ -2785,7 +2780,7 @@ def buildPatch(self,log,progress):# buildPatch2 !!!!
27852780
for group in self.activeTypes:
27862781
if group not in patchFile.tops: continue
27872782
attrs = self.class_attrs[group]
2788-
count,counts = 0,{}
2783+
counts = Counter()
27892784
for record in patchFile.tops[group].records:
27902785
fid = record.fid
27912786
itemStats = fid_attr_value.get(fid,None)
@@ -2795,9 +2790,8 @@ def buildPatch(self,log,progress):# buildPatch2 !!!!
27952790
for attr, value in itemStats.iteritems():
27962791
setattr(record,attr,value)
27972792
keep(fid)
2798-
count += 1
2799-
counts[fid[0]] = 1 + counts.get(fid[0],0)
2800-
allCounts.append((group,count,counts))
2793+
counts[fid[0]] += 1
2794+
allCounts.append((group, sum(counts.values()), counts))
28012795
self.fid_attr_value.clear()
28022796
self._patchLog(log, allCounts)
28032797

@@ -2911,7 +2905,7 @@ def buildPatch(self,log,progress):# buildPatch3: one type
29112905
id_stat = self.id_stat
29122906
allCounts = []
29132907
spell_attrs = self.spell_attrs
2914-
count,counts = 0,{}
2908+
counts = Counter()
29152909
for record in patchFile.SPEL.records:
29162910
fid = record.fid
29172911
spellStats = id_stat.get(fid)
@@ -2921,10 +2915,9 @@ def buildPatch(self,log,progress):# buildPatch3: one type
29212915
for attr,value in zip(spell_attrs,spellStats):
29222916
setattr_deep(record,attr,value)
29232917
keep(fid)
2924-
count += 1
2925-
counts[fid[0]] = 1 + counts.get(fid[0],0)
2918+
counts[fid[0]] += 1
29262919
self.id_stat.clear()
2927-
allCounts.append(('SPEL',count,counts))
2920+
allCounts.append(('SPEL', sum(counts.values()), counts))
29282921
self._patchLog(log, allCounts)
29292922

29302923
def _plog(self, log, allCounts): self._plog2(log, allCounts)

0 commit comments

Comments
 (0)