Best Python code snippet using autotest_python
lgtestlib.py
Source:lgtestlib.py
1import os2import subprocess3import tempfile4def run_grep(grep, pats, text, emptymsg):5 pf = None6 try:7 if len(pats) == 1:8 # specify single patterns on command line9 cmd = (grep, '-p', pats[0])10 else:11 # write multiple patterns to temporary pattern file12 fd, pfname = tempfile.mkstemp('w')13 pf = os.fdopen(fd, 'w')14 for p in pats:15 print >>pf, p16 pf.close()17 cmd = (grep, pfname)18 # get matches from grep19 proc = subprocess.Popen(20 cmd,21 stdin=subprocess.PIPE,22 stdout=subprocess.PIPE,23 stderr=subprocess.PIPE24 )25 gout, gerr = proc.communicate(text)26 retval = proc.wait()27 if retval:28 raise Exception('{} returned {}, {}'.format(grep, retval, gerr))29 finally:30 if pf:31 # clean up pattern file, if we used one32 pf.close()33 os.unlink(pfname)34 if len(pats) == gerr.count(emptymsg):35 # every pattern in this pattern set has zero-length matches36 return None37 # parse the matches38 matches = []39 for m in gout.splitlines():40 matches.append(map(int, m.split('\t', 3)[0:3]))41 # sort the matches by start, end, label42 lex = lambda x,y: cmp(x[0], y[0]) or cmp(x[1], y[1]) or cmp(x[2], y[2])43 matches.sort(lex)44 return matches45def run_shitgrep(sg, pats, text):46 return run_grep(sg, pats, text, 'is not allowed as a final state of the NFA')47def run_lightgrep(lg, pats, text):...
grep2.py
Source:grep2.py
1import re2def run_grep(expression):3 res = []4 count = 05 file_ = open("mbox.txt", "r")6 for line in file_:7 line = line.rstrip()8 if re.findall(expression, line):9 number = re.findall(expression, line) # number is a string in a list.10 for item in number: 11 new_num = int(item) # We need to convert these into ints in order 12 # to perform any calculations. 13 res.append(new_num)14 count += 115 final = sum(res)16 final = final/count 17 print("Count of", expression, "is", count)18 print("Average is", final)19 file_.close()20if __name__ == "__main__":21 expression = 'New Revision: ([0-9.]+)'...
grep.py
Source:grep.py
1import re2def run_grep(expression):3 count = 04 file_ = open("mbox.txt", "r")5 for line in file_:6 line = line.rstrip()7 if re.search(expression, line):8 count += 19 print("Count of", expression, "is", count)10 file_.close()11if __name__ == "__main__":12 expression = input("Enter a regular expression: ")...
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!!