- 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)
total = len(currentMissing)+len(currentImplemented)
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 : hasFetchedSets = False
output.write("Implemented (" + str(len(currentImplemented)) + "):\n") hasFetchedCardName = False
for everyImplemented in currentImplemented : tmpName = ""
output.write(everyImplemented + '\n') line = ""
output.write("\n") prevline = ""
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 #Parse mtg-data
totalDataList = sorted(totalData.items(), key=lambda (key,entry): entry[3], reverse=True) 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
totalPercentage = 0 if hasFetchedSets :
totalMissing = 0 if not hasFetchedCardName :
totalImplemented = 0 tmpName = line.rstrip().replace("AE", "Ae")
fullTotal = 0 hasFetchedCardName = True
with open(sys.path[0] + os.sep + "PerSetTrackingResults" + os.sep + "CompleteStats.txt", "w") as statsfile: if line == "\n" :
statsfile.write("Set: Implemented (Missing) / Total = Percentage Implemented\n") sets = prevline.split(", ")
for k,dataKey in totalDataList : for i in range(len(sets)):
totalImplemented += dataKey[0] sets[i] = sets[i].split(' ')[0]
totalMissing += dataKey[1] #print sets
fullTotal += dataKey[2] mtgDataCards[tmpName] = sets
statsfile.write(setCodeToName[k].lstrip() + ": " + str(dataKey[0]) + " (" + str(dataKey[1]) + ") / " + str(dataKey[2]) + " = " + str(round(dataKey[3], 2)) + "%\n") hasFetchedCardName = False
totalPercentage = totalImplemented / fullTotal
statsfile.write("\n")
statsfile.write("Total over all sets: " + str(totalImplemented) + " (" + str(totalMissing) + ") / " + str(fullTotal))
# Add another file that will print out distinct cards implemented/missing prevline = line
# Convert back to lists so they can be sorted
impCount = len(allImplemented)
misCount = len(allMissing)
#implemented = list(allImplemented)
#implemented.sort()
missing = list(allMissing)
missing.sort()
totalCount = impCount+misCount
with open(sys.path[0] + os.sep + "PerSetTrackingResults" + os.sep + "DistinctStats.txt", "w") as distinctfile:
distinctfile.write("Distinct: Implemented (Missing) / Total = Percentage Implemented\n")
distinctfile.write("%d (%d) / %d = %.2f %%\n" % (impCount, misCount, totalCount, float(impCount)/totalCount*100))
# Currently only print missing cards, implemented cards are less important #Parse Forge
#distinctfile.write("\nImplemented (%d):" % impCount) print("Parsing Forge")
#for s in implemented: for root, dirnames, filenames in os.walk("cardsfolder"):
# distinctfile.write("\n%s" % s) 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)
distinctfile.write("\nMissing (%d):" % misCount) #Compare datasets and output results
for s in missing: print("Comparing datasets and outputting results.")
distinctfile.write("\n%s" % s) totalData = {}
currentMissing = []
currentImplemented = []
allMissing = set()
allImplemented = set()
standardMissing = set()
standardImplemented = set()
total = 0
percentage = 0
print "Done!" standardSets = getSetByFormat('Standard')
ignoredSet = [ 'ASTRAL', 'ATH', 'BD', 'BR', 'DD2', 'DDC', 'DDD', 'DDE', 'DDF',
'DDG', 'DDH', 'DDI', 'DDJ', 'DKM', 'DRB', 'EVG', 'H09', 'ME2',
'ME3', 'ME4', 'MED', 'PD2', 'PD3', 'SDC', 'UGL', 'UNH',
'V09', 'V10', 'V11', 'V12',]
for currentSet in setCodes :
# Ignore any sets that we don't tabulate
if currentSet in ignoredSet: continue
for key in mtgDataCards.keys() :
setList = mtgDataCards[key]
if currentSet in setList:
if key in forgeCards :
currentImplemented.append(key)
else :
currentMissing.append(key)
total = len(currentMissing)+len(currentImplemented)
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 :
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)
if currentSet in standardSets:
standardMissing |= set(currentMissing)
standardImplemented |= set(currentImplemented)
del currentMissing[:]
del currentImplemented[:]
#sort sets by percentage completed
totalDataList = sorted(totalData.items(), key=lambda (key,entry): entry[3], reverse=True)
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!"