Best Python code snippet using fMBT_python
EditionTracking.py
Source:EditionTracking.py
1#!/usr/bin/env python23############IMPLEMENTATION FOLLOWS############4import json5import os,sys,fnmatch,re6import requests78toolsDir = os.path.abspath(os.path.dirname( __file__ ))9resDir = os.path.abspath(os.path.join(toolsDir, '..', 'res'))10editionsDir = os.path.join(resDir, 'editions')11allJson = os.path.join(toolsDir, 'AllCards.json')12allJsonUrl = 'http://mtgjson.com/json/AllCards.json'1314def initializeEditions():15 ignoredTypes = [ "From_the_Vault", "Duel_Decks", "Online", "Premium_Deck_Series" ]16 print("Parsing Editions folder")17 for root, dirnames, filenames in os.walk(editionsDir):18 for fileName in fnmatch.filter(filenames, '*.txt'):19 #print "Parsing...", fileName20 hasSetNumbers = None21 with open(os.path.join(root, fileName)) as currentEdition:22 # Check all names for this card23 metadata = True24 setcode = setname = settype = None25 for line in currentEdition.readlines():26 if metadata:27 if line.startswith("[cards]"):28 metadata = False29 if setcode:30 setCodes.append(setcode)31 setCodeToName[setcode] = setname32 if settype in ignoredTypes:33 ignoredSet.append(setcode)3435 elif line.startswith("Code="):36 setcode = line.split("=")[1].rstrip()3738 elif line.startswith("Name="):39 setname = line.split("=")[1].rstrip()4041 elif line.startswith("Type="):42 settype = line.split("=")[1].rstrip()4344 else:45 if line:46 if hasSetNumbers is None:47 hasSetNumbers = line.split(" ", 1)[0].isdigit()4849 card = line.split(" ", 2 if hasSetNumbers else 1)[-1].replace("AE","Ae").rstrip()50 if card not in mtgDataCards:51 #print card52 mtgDataCards[card] = [setcode]5354 else:55 mtgDataCards[card].append(setcode)5657 print "Total Cards Found in all editions", len(mtgDataCards)58 print "These sets will be ignored in some output files", ignoredSet5960def initializeOracleText():61 print "Initializing Oracle text"62 oracleDict = None63 if not os.path.exists(allJson):64 print "Need to download Json file..."65 r = requests.get(allJsonUrl)66 with open(allJson, 'w') as f:67 f.write(r.text)6869 oracleDict = r.json()7071 else:72 with open(allJson) as f:73 oracleDict = json.loads(f.read())7475 print "Found Oracle text for ", len(oracleDict)76 return oracleDict7778def normalizeOracle(oracle):79 return oracle.replace(u'\u2014', '-').replace(u'\u2018', "'").replace(u'\u201c', '"').replace(u'\u201d', '"').replace(u'\u2022', '-')808182def initializeForgeCards():83 #Parse Forge84 print("Parsing Forge")85 cardsfolderLocation = os.path.join(resDir, 'cardsfolder')86 for root, dirnames, filenames in os.walk(cardsfolderLocation):87 for fileName in fnmatch.filter(filenames, '*.txt'):88 with open(os.path.join(root, fileName)) as currentForgeCard :89 # Check all names for this card90 name = ''91 split = False92 for line in currentForgeCard.readlines():93 if line.startswith("Name:"):94 if split:95 name += ' // '9697 if not name or split:98 name += line[5:].replace("AE","Ae").rstrip()99 100 elif line.startswith("AlternateMode") and 'Split' in line:101 split = True102103 forgeCards.append(name)104105def initializeFormats():106 formats = {}107 formatLocation = os.path.join(resDir, 'blockdata', 'formats.txt')108 print "Looking for formats in ", formatLocation109 with open(formatLocation) as formatFile:110 while formatFile:111 try:112 line = formatFile.readline().strip()113 if not line:114 # this should only happen when the file is done processing if we did things correctly?115 break116117 format = line[1:-1]118 formats[format] = {}119 except:120 break121122 # Pull valid sets123 while line != '':124 line = formatFile.readline().strip()125 if line.startswith('Sets:'):126 sets = line.split(':')[1]127 formats[format]['sets'] = sets.split(', ')128 elif line.startswith('Banned'):129 banned = line.split(':')[1]130 formats[format]['banned'] = banned.split('; ')131132 return formats133134def writeToFiles(text, files):135 for f in files:136 if f:137 f.write(text)138139def printOverallEditions(totalDataList, setCodeToName, releaseFile=None):140 totalPercentage = 0141 totalMissing = 0142 totalImplemented = 0143 fullTotal = 0144 if releaseFile:145 releaseFile.write("[spoiler=Overall Editions]\n")146 with open(os.path.join(toolsDir, "EditionTrackingResults", "CompleteStats.txt"), "w") as statsfile:147 files = [statsfile, releaseFile]148 writeToFiles("Set: Implemented (Missing) / Total = Percentage Implemented\n", files)149 for k,dataKey in totalDataList :150 totalImplemented += dataKey[0]151 totalMissing += dataKey[1]152 fullTotal += dataKey[2]153 if dataKey[2] == 0:154 print "SetCode unknown", k155 continue156 writeToFiles(setCodeToName[k].lstrip() + ": " + str(dataKey[0]) + " (" + str(dataKey[1]) + ") / " + str(dataKey[2]) + " = " + str(round(dataKey[3], 2)) + "%\n", files)157 totalPercentage = totalImplemented / fullTotal158 writeToFiles("\nTotal over all sets: " + str(totalImplemented) + " (" + str(totalMissing) + ") / " + str(fullTotal) + "\n", files)159160 if releaseFile:161 releaseFile.write("[/spoiler]\n\n")162163def printCardSet(implementedSet, missingSet, fileName, setCoverage=None, printImplemented=False, printMissing=True, releaseFile=None):164 # Add another file that will print out whichever set is requested165 # Convert back to lists so they can be sorted166 impCount = len(implementedSet)167 misCount = len(missingSet)168 totalCount = impCount + misCount169 print fileName, "Counts: ", impCount, misCount, totalCount170171 if totalCount == 0:172 print "Something definitely wrong, why is total count 0 for ", fileName173 return174175 if releaseFile:176 releaseFile.write("[spoiler=%s]\n" % fileName)177178 filePath = os.path.join(toolsDir, "EditionTrackingResults", fileName)179 with open(filePath, "w") as outfile:180 files = [outfile, releaseFile]181 if setCoverage:182 writeToFiles(' '.join(setCoverage), files)183 writeToFiles('\n', files)184 writeToFiles("Implemented (Missing) / Total = Percentage Implemented\n", files)185 writeToFiles("%d (%d) / %d = %.2f %%\n" % (impCount, misCount, totalCount, float(impCount)/totalCount*100), files)186187 # If you really need to, we can print implemented cards188 if printImplemented:189 implemented = list(implementedSet)190 implemented.sort()191 outfile.write("\nImplemented (%d):" % impCount)192 for s in implemented:193 outfile.write("\n%s" % s)194195 # By default Missing will print, but you can disable it196 if printMissing:197 missing = list(missingSet)198 missing.sort()199 writeToFiles("\nMissing (%d):" % misCount, files)200 for s in missing:201 writeToFiles("\n%s" % s, files)202203 writeToFiles("\n", files)204205 if releaseFile:206 releaseFile.write("[/spoiler]\n\n")207208def printDistinctOracle(missingSet, fileName):209 filePath = os.path.join(toolsDir, "EditionTrackingResults", fileName)210 missing = list(missingSet)211 missing.sort()212 with open(filePath, "w") as outfile:213 for s in missing:214 if s:215 try:216 oracle = normalizeOracle(mtgOracleCards.get(s).get('text'))217 outfile.write("%s\n%s\n\n" % (s, oracle))218 except:219 outfile.write("%s\n\n" % (s))220 print "Failed to grab oracle for ", s221 outfile.write("\n")222223224if __name__ == '__main__':225 if not os.path.isdir(toolsDir + os.sep + 'EditionTrackingResults') :226 os.mkdir(toolsDir + os.sep + 'EditionTrackingResults')227228 ignoredSet = []229 forgeFolderFiles = []230 forgeCards = []231 mtgDataCards = {}232 setCodes = []233 setCodeToName = {}234 forgeCardCount = 0235 mtgDataCardCount = 0236 setCodeCount = 0237238 hasFetchedSets = False239 hasFetchedCardName = False240 tmpName = ""241 line = ""242 prevline = ""243244 # Initialize Editions245 initializeEditions()246247 initializeForgeCards()248 mtgOracleCards = initializeOracleText()249250 #Compare datasets and output results251 print("Comparing datasets and outputting results.")252 totalData = {}253 currentMissing = []254 currentImplemented = []255 allMissing = set()256 allImplemented = set()257 formats = initializeFormats()258 unknownFormat = {'sets': []}259260 standardSets = formats.get('Standard', unknownFormat)['sets']261 #print "Standard sets", standardSets, len(standardSets)262 standardMissing = set()263 standardImplemented = set()264265 modernSets = formats.get('Modern', unknownFormat)['sets']266 modernMissing = set()267 modernImplemented = set()268269 total = 0270 percentage = 0271272 for currentSet in setCodes :273 # Ignore any sets that we don't tabulate274 if currentSet in ignoredSet: continue275 #print "Tabulating set", currentSet276277 for key in mtgDataCards.keys():278 setList = mtgDataCards[key]279 if currentSet in setList:280 if key in forgeCards:281 currentImplemented.append(key)282 elif key != "":283 currentMissing.append(key)284 total = len(currentMissing)+len(currentImplemented)285 percentage = 0 286 if total > 0 :287 percentage = (float(len(currentImplemented))/float(total))*100288 currentMissing.sort()289 currentImplemented.sort()290291 # Output each edition file on it's own292 with open(toolsDir + os.sep + "EditionTrackingResults" + os.sep + "set_" + currentSet.strip() + ".txt", "w") as output :293 output.write("Implemented (" + str(len(currentImplemented)) + "):\n")294 for everyImplemented in currentImplemented :295 output.write(everyImplemented + '\n')296 output.write("\n")297 output.write("Missing (%s):\n" % len(currentMissing))298 for everyMissing in currentMissing :299 output.write(everyMissing + '\n')300 orc = mtgOracleCards.get(everyMissing)301 try:302 if 'manaCost' in orc:303 output.write(orc.get('manaCost') + '\n')304305 #output.write(' '.join(orc.get('supertypes', [])))306 #output.write(' '.join(orc.get('types', [])))307 #output.write(' '.join(orc.get('subtypes', [])))308 output.write(normalizeOracle(orc.get('type')))309 output.write('\n')310311 if 'power' in orc:312 output.write( "PT:" + orc.get('power') + '/' + orc.get('toughness') + '\n')313 if 'loyalty' in orc:314 output.write('Loyalty:' + orc.get('loyalty'))315 except Exception as e:316 print "some issue?", str(e)317318 # Blah319320321 try:322 text = normalizeOracle(orc.get('text'))323 except:324 text = ''325 326 output.write(text + '\n\n')327 output.write("\n")328 output.write("Total: " + str(total) + "\n")329 output.write("Percentage implemented: " + str(round(percentage,2)) + "%\n")330 totalData[currentSet] = (len(currentImplemented),len(currentMissing),total,percentage)331 allMissing |= set(currentMissing)332 allImplemented |= set(currentImplemented)333 if currentSet in standardSets:334 #print "Found a standard set", currentSet335 standardMissing |= set(currentMissing)336 standardImplemented |= set(currentImplemented)337 if currentSet in modernSets:338 modernMissing |= set(currentMissing)339 modernImplemented |= set(currentImplemented)340341 del currentMissing[:]342 del currentImplemented[:]343344 #sort sets by percentage completed345 totalDataList = sorted(totalData.items(), key=lambda k: k[1][3], reverse=True)346347 releaseOutput = open(os.path.join(toolsDir, "EditionTrackingResults", "ReleaseStats.txt"), "w")348349 printCardSet(allImplemented, allMissing, "DistinctStats.txt", releaseFile=releaseOutput)350 printOverallEditions(totalDataList, setCodeToName, releaseFile=releaseOutput)351 printCardSet(standardImplemented, standardMissing, "FormatStandard.txt", setCoverage=standardSets, releaseFile=releaseOutput)352 printCardSet(modernImplemented, modernMissing, "FormatModern.txt", setCoverage=modernSets, releaseFile=releaseOutput)353 printDistinctOracle(allMissing, "DistinctOracle.txt")354355 releaseOutput.close()356
...
PerSetTracking.py
Source:PerSetTracking.py
1#!/usr/bin/env python2############IMPLEMENTATION FOLLOWS############3import os,sys,fnmatch,re4# TODO Move these somewhere else?5ignoredSet = [ 'ASTRAL', 'ATH', 'BD', 'BR', 'CM1', 'DD2', 'DDC', 'DDD', 'DDE', 'DDF',6 'DDG', 'DDH', 'DDI', 'DDJ', 'DDK', 'DDL', 'DDM', 'DDN', 'DKM', 'DRB', 'DREAM', 'EVG', 'H09', 'MD1', 'ME2',7 'ME3', 'ME4', 'MED', 'PD2', 'PD3', 'SDC', 'UG', 'UGL', 'UNH', 'V09', 'V10', 'V11', 'V12',8 'V13', 'V14', '', 'DD3_DVD']9toolsDir = os.path.abspath(os.path.dirname( __file__ ))10resDir = os.path.abspath(os.path.join(toolsDir, '..', 'res'))11pathToMtgData = os.path.join(toolsDir, "mtg-data.txt")12def initializeFormats():13 formats = {}14 formatLocation = os.path.join(resDir, 'blockdata', 'formats.txt')15 print "Looking for formats in ", formatLocation16 with open(formatLocation) as formatFile:17 while formatFile:18 try:19 line = formatFile.readline().strip()20 if not line:21 # this should only happen when the file is done processing if we did things correctly?22 break23 format = line[1:-1]24 formats[format] = {}25 except:26 break27 # Pull valid sets28 while line != '':29 line = formatFile.readline().strip()30 if line.startswith('Sets:'):31 sets = line.split(':')[1]32 formats[format]['sets'] = sets.split(', ')33 elif line.startswith('Banned'):34 banned = line.split(':')[1]35 formats[format]['banned'] = sets.split('; ')36 #print formats37 return formats38def writeToFiles(text, files):39 for f in files:40 if f:41 f.write(text)42def printOverallEditions(totalDataList, setCodeToName, releaseFile=None):43 totalPercentage = 044 totalMissing = 045 totalImplemented = 046 fullTotal = 047 if releaseFile:48 releaseFile.write("[spoiler=Overall Editions]\n")49 with open(os.path.join(toolsDir, "PerSetTrackingResults", "CompleteStats.txt"), "w") as statsfile:50 files = [statsfile, releaseFile]51 writeToFiles("Set: Implemented (Missing) / Total = Percentage Implemented\n", files)52 for k,dataKey in totalDataList :53 totalImplemented += dataKey[0]54 totalMissing += dataKey[1]55 fullTotal += dataKey[2]56 if dataKey[2] == 0:57 print "SetCode unknown", k58 continue59 writeToFiles(setCodeToName[k].lstrip() + ": " + str(dataKey[0]) + " (" + str(dataKey[1]) + ") / " + str(dataKey[2]) + " = " + str(round(dataKey[3], 2)) + "%\n", files)60 totalPercentage = totalImplemented / fullTotal61 writeToFiles("\nTotal over all sets: " + str(totalImplemented) + " (" + str(totalMissing) + ") / " + str(fullTotal) + "\n", files)62 if releaseFile:63 releaseFile.write("[/spoiler]\n\n")64def printCardSet(implementedSet, missingSet, fileName, setCoverage=None, printImplemented=False, printMissing=True, releaseFile=None):65 # Add another file that will print out whichever set is requested66 # Convert back to lists so they can be sorted67 impCount = len(implementedSet)68 misCount = len(missingSet)69 totalCount = impCount + misCount70 if releaseFile:71 releaseFile.write("[spoiler=%s]\n" % fileName)72 filePath = os.path.join(toolsDir, "PerSetTrackingResults", fileName)73 with open(filePath, "w") as outfile:74 files = [outfile, releaseFile]75 if setCoverage:76 writeToFiles(' '.join(setCoverage), files)77 writeToFiles('\n', files)78 writeToFiles("Implemented (Missing) / Total = Percentage Implemented\n", files)79 writeToFiles("%d (%d) / %d = %.2f %%\n" % (impCount, misCount, totalCount, float(impCount)/totalCount*100), files)80 # If you really need to, we can print implemented cards81 if printImplemented:82 implemented = list(implementedSet)83 implemented.sort()84 outfile.write("\nImplemented (%d):" % impCount)85 for s in implemented:86 outfile.write("\n%s" % s)87 # By default Missing will print, but you can disable it88 if printMissing:89 missing = list(missingSet)90 missing.sort()91 writeToFiles("\nMissing (%d):" % misCount, files)92 for s in missing:93 writeToFiles("\n%s" % s, files)94 writeToFiles("\n", files)95 if releaseFile:96 releaseFile.write("[/spoiler]\n\n")97def printDistinctOracle(missingSet, fileName):98 filePath = os.path.join(toolsDir, "PerSetTrackingResults", fileName)99 missing = list(missingSet)100 missing.sort()101 with open(filePath, "w") as outfile:102 for s in missing:103 if s:104 oracle = mtgOracleCards.get(s, "")105 outfile.write("%s\n%s" % (s, oracle))106 outfile.write("\n")107if __name__ == '__main__':108 if not os.path.exists(pathToMtgData) :109 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.")110 print("Press Enter to exit")111 raw_input("")112 sys.exit()113 if not os.path.isdir(toolsDir + os.sep + 'PerSetTrackingResults') :114 os.mkdir(toolsDir + os.sep + 'PerSetTrackingResults')115 forgeFolderFiles = []116 forgeCards = []117 mtgDataCards = {}118 mtgOracleCards = {}119 setCodes = []120 setCodeToName = {}121 forgeCardCount = 0122 mtgDataCardCount = 0123 setCodeCount = 0124 hasFetchedSets = False125 hasFetchedCardName = False126 tmpName = ""127 line = ""128 prevline = ""129 #Parse mtg-data130 print("Parsing mtg-data")131 with open(pathToMtgData) as mtgdata :132 for line in mtgdata :133 # Parse the sets at the top of the mtgdata file134 if not hasFetchedSets :135 if line != "\n" :136 splitLine = line.split(' ') 137 code = splitLine[0]138 setCodeToName[code] = splitLine[-1].replace('\n', '')139 #print splitLine, code, setCodeToName[code]140 setCodes.append(code)141 else :142 hasFetchedSets = True143 # Once all sets are parsed, time to parse the cards144 if hasFetchedSets :145 if not hasFetchedCardName :146 tmpName = line.rstrip().replace("AE", "Ae")147 hasFetchedCardName = True148 oracle = ""149 else:150 oracle += line151 if line == "\n" :152 mtgOracleCards[tmpName] = oracle.replace(prevline, '')153 sets = prevline.split(", ")154 for i in range(len(sets)):155 sets[i] = sets[i].split(' ')[0]156 #print sets157 mtgDataCards[tmpName] = sets158 hasFetchedCardName = False159 prevline = line160 #Parse Forge161 print("Parsing Forge")162 cardsfolderLocation = os.path.join(resDir, 'cardsfolder')163 for root, dirnames, filenames in os.walk(cardsfolderLocation):164 for fileName in fnmatch.filter(filenames, '*.txt'):165 with open(os.path.join(root, fileName)) as currentForgeCard :166 # Check all names for this card167 for line in currentForgeCard.readlines():168 if line.startswith("Name:"):169 forgeCards.append(line[5:].replace("AE","Ae").rstrip())170 #Compare datasets and output results171 print("Comparing datasets and outputting results.")172 totalData = {}173 currentMissing = []174 currentImplemented = []175 allMissing = set()176 allImplemented = set()177 formats = initializeFormats()178 unknownFormat = {'sets': []}179 standardSets = formats.get('Standard', unknownFormat)['sets']180 standardMissing = set()181 standardImplemented = set()182 modernSets = formats.get('Modern', unknownFormat)['sets']183 modernMissing = set()184 modernImplemented = set()185 #extendedSets = formats.get('Extended', unknownFormat)['sets']186 #extendedMissing = set()187 #extendedImplemented = set()188 total = 0189 percentage = 0190 for currentSet in setCodes :191 # Ignore any sets that we don't tabulate192 if currentSet in ignoredSet: continue193 for key in mtgDataCards.keys() :194 setList = mtgDataCards[key]195 if currentSet in setList:196 if key in forgeCards :197 currentImplemented.append(key)198 elif key != "":199 currentMissing.append(key)200 total = len(currentMissing)+len(currentImplemented)201 percentage = 0 202 if total > 0 :203 percentage = (float(len(currentImplemented))/float(total))*100204 currentMissing.sort()205 currentImplemented.sort()206 # Output each edition file on it's own207 with open(toolsDir + os.sep + "PerSetTrackingResults" + os.sep + "set_" + currentSet.strip() + ".txt", "w") as output :208 output.write("Implemented (" + str(len(currentImplemented)) + "):\n")209 for everyImplemented in currentImplemented :210 output.write(everyImplemented + '\n')211 output.write("\n")212 output.write("Missing (" + str(len(currentMissing)) + "):\n")213 for everyMissing in currentMissing :214 output.write(everyMissing + '\n')215 output.write(mtgOracleCards[everyMissing])216 output.write("\n")217 output.write("Total: " + str(total) + "\n")218 output.write("Percentage implemented: " + str(round(percentage,2)) + "%\n")219 totalData[currentSet] = (len(currentImplemented),len(currentMissing),total,percentage)220 allMissing |= set(currentMissing)221 allImplemented |= set(currentImplemented)222 if currentSet in standardSets:223 standardMissing |= set(currentMissing)224 standardImplemented |= set(currentImplemented)225 if currentSet in modernSets:226 modernMissing |= set(currentMissing)227 modernImplemented |= set(currentImplemented)228 #if currentSet in extendedSets:229 # extendedMissing |= set(currentMissing)230 # extendedImplemented |= set(currentImplemented)231 del currentMissing[:]232 del currentImplemented[:]233 #sort sets by percentage completed234 totalDataList = sorted(totalData.items(), key=lambda k: k[1][3], reverse=True)235 releaseOutput = open(os.path.join(toolsDir, "PerSetTrackingResults", "ReleaseStats.txt"), "w")236 printCardSet(allImplemented, allMissing, "DistinctStats.txt", releaseFile=releaseOutput)237 printOverallEditions(totalDataList, setCodeToName, releaseFile=releaseOutput)238 printCardSet(standardImplemented, standardMissing, "FormatStandard.txt", setCoverage=standardSets, releaseFile=releaseOutput)239 #printCardSet(extendedImplemented, extendedMissing, "FormatExtended.txt", setCoverage=extendedSets)240 printCardSet(modernImplemented, modernMissing, "FormatModern.txt", setCoverage=modernSets, releaseFile=releaseOutput)241 printDistinctOracle(allMissing, "DistinctOracle.txt")242 releaseOutput.close()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!