Best Python code snippet using Airtest
stock.py
Source:stock.py
1import json2class stock:3 stock={}4 def __init__(self,SID,master):5 self.master=master6 self.SID=SID7 def help(self):8 return {"inkoop": "Report buying products", "voorraad": "Set stock of a product"}9 def readstock(self):10 groupname=""11 with open('data/revbank.stock','r') as f:12 lines=f.readlines()13 for line in lines:14 parts=' '.join(line.split()).split(" ",2)15 if len(parts)==2:16 name=parts[0]17 self.stock[name]=int(parts[1])18 f.close()19 self.stockalias={}20 with open('data/revbank.stockalias','r') as f:21 lines=f.readlines()22 for line in lines:23 parts=' '.join(line.split()).split(" ",3)24 if len(parts)==3:25 name=parts[0]26 self.stockalias[name]={'prod': parts[1],'multi': int(parts[2])}27 def setstock(self,prod,count):28 self.readstock()29 if prod not in self.stock:30 self.stock[prod]=031 self.stock[prod]=count32 self.master.send_message(True,'stock/'+prod,json.dumps(self.stock[prod]))33 self.writestock()34 def addstock(self,prod,count):35 self.readstock()36 if prod not in self.stock:37 self.stock[prod]=038 self.stock[prod]+=count39 self.master.send_message(True,'stock/'+prod,json.dumps(self.stock[prod]))40 self.writestock()41 def hook_checkout(self,text):42 self.readstock()43 for rid in self.master.receipt.receipt:44 if rid['product'] in self.stockalias:45 rp=rid['product']46 prod=self.stockalias[rp]['prod']47 multiplier=self.stockalias[rp]['multi']48 else:49 prod=rid['product']50 multiplier=151 if prod in self.master.products.products:52 if prod not in self.stock:53 self.stock[prod]=054 self.stock[prod]-=rid['count']*multiplier55 self.master.send_message(True,'stock/'+prod,json.dumps(self.stock[prod]))56 self.writestock()57 def writestock(self):58 with open('data/revbank.stock','w') as f:59 for prod in self.stock:60 f.write("%-16s %+9d\n" % (prod,self.stock[prod]))61 f.close()62 def voorraad_amount(self,text):63 try:64 aantal=int(text)65 if aantal<0 or aantal>5000:66 self.master.donext(self,'voorraad_amount')67 self.master.send_message(True,'message','Please enter a number between 1 and 4999, how much '+self.prod+' is in stock?')68 self.master.send_message(True,'buttons',json.dumps({'special':'numbers'}))69 return True70 else:71 self.setstock(self.prod,aantal)72 self.master.donext(self,'voorraad')73 self.master.send_message(True,'message','What product to set the stock?')74 self.master.send_message(True,'buttons',json.dumps({'special':'products'}))75 return True76 except:77 if text=="abort":78 self.master.callhook('abort',None)79 return True80 else:81 self.master.donext(self,'voorraad_amount')82 self.master.send_message(True,'message','Not a number, how much '+self.prod+' is in stock')83 self.master.send_message(True,'buttons',json.dumps({'special':'numbers'}))84 return True85 def voorraad(self,text):86 prod=self.master.products.lookupprod(text)87 if prod:88 self.prod=prod89 self.master.donext(self,'voorraad_amount')90 self.master.send_message(True,'message','How much '+prod+' is in stock?')91 self.master.send_message(True,'buttons',json.dumps({'special':'numbers'}))92 return True93 elif text=="abort":94 self.master.callhook('abort',None)95 return True96 else:97 self.master.donext(self,'voorraad')98 self.master.send_message(True,'message','Unknown Product, what product to set the stock?')99 self.master.send_message(True,'buttons',json.dumps({'special':'products'}))100 return True101 102 def inkoop_amount(self,text):103 try:104 aantal=int(text)105 if aantal<0 or aantal>5000:106 self.master.donext(self,'inkoop_amount')107 self.master.send_message(True,'message','Please enter a number between 1 and 4999, how much '+self.prod+' did you buy?')108 self.master.send_message(True,'buttons',json.dumps({'special':'numbers'}))109 return True110 else:111 self.addstock(self.prod,aantal)112 self.master.donext(self,'inkoop')113 self.master.send_message(True,'message','What product did you buy?')114 self.master.send_message(True,'buttons',json.dumps({'special':'products'}))115 return True116 except:117 if text=="abort":118 self.master.callhook('abort',None)119 return True120 else:121 self.master.donext(self,'inkoop_amount')122 self.master.send_message(True,'message','Not a number, how much '+self.prod+' did you buy?')123 self.master.send_message(True,'buttons',json.dumps({'special':'numbers'}))124 return True125 def inkoop(self,text):126 prod=self.master.products.lookupprod(text)127 if prod:128 self.prod=prod129 self.master.donext(self,'inkoop_amount')130 self.master.send_message(True,'message','How much '+self.prod+' did you buy?')131 self.master.send_message(True,'buttons',json.dumps({'special':'numbers'}))132 return True133 elif text=="abort":134 self.master.callhook('abort',None)135 return True136 else:137 self.master.donext(self,'inkoop')138 self.master.send_message(True,'message','Unknown Product, what product did you buy?')139 self.master.send_message(True,'buttons',json.dumps({'special':'products'}))140 return True141 142 def input(self,text):143 if text=="voorraad":144 self.master.donext(self,'voorraad')145 self.master.send_message(True,'message','What product to set the stock?')146 self.master.send_message(True,'buttons',json.dumps({'special':'products'}))147 return True148 elif text=="inkoop":149 self.master.donext(self,'inkoop')150 self.master.send_message(True,'message','What product did you buy?')151 self.master.send_message(True,'buttons',json.dumps({'special':'products'}))152 return True153 def hook_abort(self,void):154 self.startup()155 def startup(self):156 self.readstock()157 for prod in self.stock:...
4_2.py
Source:4_2.py
1import string2file = open('4_input.txt', 'r')3lines = file.readlines()4"""5byr (Birth Year)6iyr (Issue Year)7eyr (Expiration Year)8hgt (Height)9hcl (Hair Color)10ecl (Eye Color)11pid12"""13doc = {}14count = 015for line in lines:16 #print (line)17 if line == "\n":18 # starting new passport19 # check if previous is valid:20 doNext = False21 if "byr" in doc:22 # four digits; at least 1920 and at most 200223 doNext = 1920 <= int(doc["byr"]) <= 200224 if not doNext:25 print ("BYR")26 else:27 doNext = False28 if doNext and "iyr" in doc:29 # four digits; at least 2010 and at most 2020.30 doNext = 2010 <= int(doc["iyr"]) <= 202031 if not doNext:32 print ("IYR")33 else:34 doNext = False35 if doNext and "eyr" in doc:36 # four digits; at least 2020 and at most 2030.37 doNext = 2020 <= int(doc["eyr"]) <= 203038 if not doNext:39 print ("EYR")40 else:41 doNext = False42 if doNext and "hgt" in doc:43 # a number followed by either cm or in:44 # If cm, the number must be at least 150 and at most 193.45 # If in, the number must be at least 59 and at most 76.46 if len(doc["hgt"]) >= 4:47 height = int(doc["hgt"][:-2])48 if "cm" == doc["hgt"][-2:]:49 doNext = 150 <= height <= 19350 elif "in" == doc["hgt"][-2:]:51 doNext = 59 <= height <= 7652 else:53 doNext = False54 else:55 doNext = False56 if not doNext:57 print ("HGT")58 else:59 doNext = False60 if doNext and "hcl" in doc:61 # a # followed by exactly six characters 0-9 or a-f.62 doNext = doc["hcl"][0] == "#"63 if doNext:64 doNext = all(c in string.hexdigits for c in doc["hcl"][1:])65 if not doNext:66 print ("HCL")67 else:68 doNext = False69 if doNext and "ecl" in doc:70 # exactly one of: amb blu brn gry grn hzl oth.71 doNext = doc["ecl"] in ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]72 if not doNext:73 print ("ECL")74 else:75 doNext = False76 if doNext and "pid" in doc:77 # a nine-digit number, including leading zeroes.78 val = doc["pid"]79 doNext = len(val) == 9 and val.isnumeric()80 if not doNext:81 print ("PID")82 else:83 doNext = False84 if doNext:85 count = count + 186 # print("VALID")87 else:88 print (doc)89 print ("INVALID")90 #reset91 doc = {}92 else:93 line = line.strip()94 tokens = line.split()95 for t in tokens:96 key, value = t.split(":")97 doc[key] = value...
addpostflight
Source:addpostflight
1#!/usr/bin/env python2import sys3donext = 04tab = sys.argv[1:]5for a in tab:6 if donext:7 path = a8 donext = 09 if a == "-srcfolder":10 donext = 111import os12for p in os.walk(path):13 if p[0].endswith("python.pkg"):14 pythondir = p[0]15dest = os.path.join(pythondir, "Contents", "Resources")16import shutil 17shutil.copy(os.path.join(os.path.dirname(__file__),18 "macpostinstall") ,os.path.join(dest, "postflight"))...
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!!