- Some improvements to the structure of PerSetTracking.py

- Stats about Standard Format printed, similar to Distinct
This commit is contained in:
Sol
2013-01-20 17:05:21 +00:00
parent 61bdf760cc
commit 3e946b48e8

View File

@@ -5,155 +5,196 @@ pathToMtgData = "mtg-data.txt"
############IMPLEMENTATION FOLLOWS############ ############IMPLEMENTATION FOLLOWS############
import os,sys,fnmatch,re import os,sys,fnmatch,re
if not os.path.exists(pathToMtgData) : def getSetByFormat(requestedFormat):
print("This script requires the text version of Arch's mtg-data to be present.You can download it from slightlymagic.net's forum and either place the text version next to this script or edit this script and provide the path to the file at the top.") # Parse out Standard sets from the Format file
print("Press Enter to exit") formatLocation = os.path.join(sys.path[0], 'blockdata', 'formats.txt')
raw_input("") with open(formatLocation) as formatFile:
sys.exit() formats = formatFile.readlines()
if not os.path.isdir(sys.path[0] + os.sep + 'PerSetTrackingResults') : for format in formats:
os.mkdir(sys.path[0] + os.sep + 'PerSetTrackingResults') if requestedFormat not in format:
continue
parsed = format.split('|')
for p in parsed:
if not p.startswith('Sets:'):
continue
forgeFolderFiles = [] sets = p.strip().split(':')[1]
forgeCards = [] return sets.split(', ')
mtgDataCards = {}
setCodes = []
setCodeToName = {}
forgeCardCount = 0
mtgDataCardCount = 0
setCodeCount = 0
hasFetchedSets = False return []
hasFetchedCardName = False
tmpName = ""
line = ""
prevline = ""
#Parse mtg-data def printCardSet(implementedSet, missingSet, fileName, setCoverage=None, printImplemented=False, printMissing=True):
print("Parsing mtg-data") # Add another file that will print out whichever set is requested
with open(pathToMtgData) as mtgdata : # Convert back to lists so they can be sorted
for line in mtgdata : impCount = len(implementedSet)
if not hasFetchedSets : misCount = len(missingSet)
if line != "\n" : totalCount = impCount + misCount
splitLine = line.split(' ')
code = splitLine[0]
setCodeToName[code] = splitLine[-1].replace('\n', '')
#print splitLine, code, setCodeToName[code]
setCodes.append(code)
else :
hasFetchedSets = True
if hasFetchedSets : filePath = os.path.join(sys.path[0], "PerSetTrackingResults", fileName)
if not hasFetchedCardName : with open(filePath, "w") as outfile:
tmpName = line.rstrip().replace("AE", "Ae") if setCoverage:
hasFetchedCardName = True outfile.write(' '.join(setCoverage))
if line == "\n" : outfile.write('\n')
sets = prevline.split(", ") outfile.write("Implemented (Missing) / Total = Percentage Implemented\n")
for i in range(len(sets)): outfile.write("%d (%d) / %d = %.2f %%\n" % (impCount, misCount, totalCount, float(impCount)/totalCount*100))
sets[i] = sets[i].split(' ')[0]
#print sets
mtgDataCards[tmpName] = sets
hasFetchedCardName = False
prevline = line # If you really need to, we can print implemented cards
if printImplemented:
implemented = list(implementedSet)
implemented.sort()
outfile.write("\nImplemented (%d):" % impCount)
for s in implemented:
outfile.write("\n%s" % s)
outfile.write("\n")
#Parse Forge # By default Missing will print, but you can disable it
print("Parsing Forge") if printMissing:
for root, dirnames, filenames in os.walk("cardsfolder"): missing = list(missingSet)
for fileName in fnmatch.filter(filenames, '*.txt'): missing.sort()
with open(os.path.join(root, fileName)) as currentForgeCard : outfile.write("\nMissing (%d):" % misCount)
tmpname = currentForgeCard.readline() for s in missing:
tmpname = tmpname[5:].replace("AE","Ae") outfile.write("\n%s" % s)
tmpname = tmpname.rstrip()
forgeCards.append(tmpname)
#Compare datasets and output results if __name__ == '__main__':
print("Comparing datasets and outputting results.") if not os.path.exists(pathToMtgData) :
totalData = {} print("This script requires the text version of Arch's mtg-data to be present.You can download it from slightlymagic.net's forum and either place the text version next to this script or edit this script and provide the path to the file at the top.")
currentMissing = [] print("Press Enter to exit")
currentImplemented = [] raw_input("")
allMissing = set() sys.exit()
allImplemented = set()
total = 0
percentage = 0
ignoredSet = [ 'ASTRAL', 'ATH', 'BD', 'BR', 'DD2', 'DDC', 'DDD', 'DDE', 'DDF', if not os.path.isdir(sys.path[0] + os.sep + 'PerSetTrackingResults') :
'DDG', 'DDH', 'DDI', 'DDJ', 'DKM', 'DRB', 'EVG', 'H09', 'ME2', os.mkdir(sys.path[0] + os.sep + 'PerSetTrackingResults')
'ME3', 'ME4', 'MED', 'PD2', 'PD3', 'SDC', 'UGL', 'UNH',
'V09', 'V10', 'V11', 'V12',]
for currentSet in setCodes : forgeFolderFiles = []
# Ignore the following sets for tab calculation forgeCards = []
if currentSet in ignoredSet: continue mtgDataCards = {}
for key in mtgDataCards.keys() : setCodes = []
setList = mtgDataCards[key] setCodeToName = {}
if currentSet in setList: forgeCardCount = 0
if key in forgeCards : mtgDataCardCount = 0
currentImplemented.append(key) setCodeCount = 0
else :
currentMissing.append(key) hasFetchedSets = False
total = len(currentMissing)+len(currentImplemented) hasFetchedCardName = False
tmpName = ""
line = ""
prevline = ""
#Parse mtg-data
print("Parsing mtg-data")
with open(pathToMtgData) as mtgdata :
for line in mtgdata :
if not hasFetchedSets :
if line != "\n" :
splitLine = line.split(' ')
code = splitLine[0]
setCodeToName[code] = splitLine[-1].replace('\n', '')
#print splitLine, code, setCodeToName[code]
setCodes.append(code)
else :
hasFetchedSets = True
if hasFetchedSets :
if not hasFetchedCardName :
tmpName = line.rstrip().replace("AE", "Ae")
hasFetchedCardName = True
if line == "\n" :
sets = prevline.split(", ")
for i in range(len(sets)):
sets[i] = sets[i].split(' ')[0]
#print sets
mtgDataCards[tmpName] = sets
hasFetchedCardName = False
prevline = line
#Parse Forge
print("Parsing Forge")
for root, dirnames, filenames in os.walk("cardsfolder"):
for fileName in fnmatch.filter(filenames, '*.txt'):
with open(os.path.join(root, fileName)) as currentForgeCard :
tmpname = currentForgeCard.readline()
tmpname = tmpname[5:].replace("AE","Ae")
tmpname = tmpname.rstrip()
forgeCards.append(tmpname)
#Compare datasets and output results
print("Comparing datasets and outputting results.")
totalData = {}
currentMissing = []
currentImplemented = []
allMissing = set()
allImplemented = set()
standardMissing = set()
standardImplemented = set()
total = 0
percentage = 0 percentage = 0
if total > 0 :
percentage = (float(len(currentImplemented))/float(total))*100
currentMissing.sort()
currentImplemented.sort()
with open(sys.path[0] + os.sep + "PerSetTrackingResults" + os.sep + "set_" + currentSet.strip() + ".txt", "w") as output : standardSets = getSetByFormat('Standard')
output.write("Implemented (" + str(len(currentImplemented)) + "):\n")
for everyImplemented in currentImplemented :
output.write(everyImplemented + '\n')
output.write("\n")
output.write("Missing (" + str(len(currentMissing)) + "):\n")
for everyMissing in currentMissing :
output.write(everyMissing + '\n')
output.write("\n")
output.write("Total: " + str(total) + "\n")
output.write("Percentage implemented: " + str(round(percentage,2)) + "%\n")
totalData[currentSet] = (len(currentImplemented),len(currentMissing),total,percentage)
allMissing |= set(currentMissing)
allImplemented |= set(currentImplemented)
del currentMissing[:]
del currentImplemented[:]
#sort sets by percentage completed ignoredSet = [ 'ASTRAL', 'ATH', 'BD', 'BR', 'DD2', 'DDC', 'DDD', 'DDE', 'DDF',
totalDataList = sorted(totalData.items(), key=lambda (key,entry): entry[3], reverse=True) 'DDG', 'DDH', 'DDI', 'DDJ', 'DKM', 'DRB', 'EVG', 'H09', 'ME2',
'ME3', 'ME4', 'MED', 'PD2', 'PD3', 'SDC', 'UGL', 'UNH',
'V09', 'V10', 'V11', 'V12',]
totalPercentage = 0 for currentSet in setCodes :
totalMissing = 0 # Ignore any sets that we don't tabulate
totalImplemented = 0 if currentSet in ignoredSet: continue
fullTotal = 0 for key in mtgDataCards.keys() :
with open(sys.path[0] + os.sep + "PerSetTrackingResults" + os.sep + "CompleteStats.txt", "w") as statsfile: setList = mtgDataCards[key]
statsfile.write("Set: Implemented (Missing) / Total = Percentage Implemented\n") if currentSet in setList:
for k,dataKey in totalDataList : if key in forgeCards :
totalImplemented += dataKey[0] currentImplemented.append(key)
totalMissing += dataKey[1] else :
fullTotal += dataKey[2] currentMissing.append(key)
statsfile.write(setCodeToName[k].lstrip() + ": " + str(dataKey[0]) + " (" + str(dataKey[1]) + ") / " + str(dataKey[2]) + " = " + str(round(dataKey[3], 2)) + "%\n") total = len(currentMissing)+len(currentImplemented)
totalPercentage = totalImplemented / fullTotal percentage = 0
statsfile.write("\n") if total > 0 :
statsfile.write("Total over all sets: " + str(totalImplemented) + " (" + str(totalMissing) + ") / " + str(fullTotal)) percentage = (float(len(currentImplemented))/float(total))*100
currentMissing.sort()
currentImplemented.sort()
# Add another file that will print out distinct cards implemented/missing with open(sys.path[0] + os.sep + "PerSetTrackingResults" + os.sep + "set_" + currentSet.strip() + ".txt", "w") as output :
# Convert back to lists so they can be sorted output.write("Implemented (" + str(len(currentImplemented)) + "):\n")
impCount = len(allImplemented) for everyImplemented in currentImplemented :
misCount = len(allMissing) output.write(everyImplemented + '\n')
#implemented = list(allImplemented) output.write("\n")
#implemented.sort() output.write("Missing (" + str(len(currentMissing)) + "):\n")
missing = list(allMissing) for everyMissing in currentMissing :
missing.sort() output.write(everyMissing + '\n')
totalCount = impCount+misCount output.write("\n")
with open(sys.path[0] + os.sep + "PerSetTrackingResults" + os.sep + "DistinctStats.txt", "w") as distinctfile: output.write("Total: " + str(total) + "\n")
distinctfile.write("Distinct: Implemented (Missing) / Total = Percentage Implemented\n") output.write("Percentage implemented: " + str(round(percentage,2)) + "%\n")
distinctfile.write("%d (%d) / %d = %.2f %%\n" % (impCount, misCount, totalCount, float(impCount)/totalCount*100)) totalData[currentSet] = (len(currentImplemented),len(currentMissing),total,percentage)
allMissing |= set(currentMissing)
allImplemented |= set(currentImplemented)
if currentSet in standardSets:
standardMissing |= set(currentMissing)
standardImplemented |= set(currentImplemented)
# Currently only print missing cards, implemented cards are less important del currentMissing[:]
#distinctfile.write("\nImplemented (%d):" % impCount) del currentImplemented[:]
#for s in implemented:
# distinctfile.write("\n%s" % s)
distinctfile.write("\nMissing (%d):" % misCount) #sort sets by percentage completed
for s in missing: totalDataList = sorted(totalData.items(), key=lambda (key,entry): entry[3], reverse=True)
distinctfile.write("\n%s" % s)
print "Done!" totalPercentage = 0
totalMissing = 0
totalImplemented = 0
fullTotal = 0
with open(sys.path[0] + os.sep + "PerSetTrackingResults" + os.sep + "CompleteStats.txt", "w") as statsfile:
statsfile.write("Set: Implemented (Missing) / Total = Percentage Implemented\n")
for k,dataKey in totalDataList :
totalImplemented += dataKey[0]
totalMissing += dataKey[1]
fullTotal += dataKey[2]
statsfile.write(setCodeToName[k].lstrip() + ": " + str(dataKey[0]) + " (" + str(dataKey[1]) + ") / " + str(dataKey[2]) + " = " + str(round(dataKey[3], 2)) + "%\n")
totalPercentage = totalImplemented / fullTotal
statsfile.write("\n")
statsfile.write("Total over all sets: " + str(totalImplemented) + " (" + str(totalMissing) + ") / " + str(fullTotal))
printCardSet(allImplemented, allMissing, "DistinctStats.txt")
printCardSet(standardImplemented, standardMissing, "FormatStandard.txt", setCoverage=standardSets)
print "Done!"