Best Python code snippet using robotframework-pageobjects_python
prefix-grep.py
Source:prefix-grep.py  
1from __future__ import with_statement, division2import subprocess, sys3from memoize import memoize4GOOD = True5BAD = False6INVALID = None7# http://python3porting.com/problems.html8if sys.version_info < (3,):9    def b(x):10        return x11    def s(x):12        return x13else:14    def b(x):15        return x.encode()16    def s(x):17        return x.decode()18def binary_search_on_string(f, arg):19    start = 020    mid = 021    end = len(arg)22    while mid < end and f(arg[start:end]) is not GOOD:23        new_end = (mid + end) // 224        if new_end == end:25            new_end -= 126        if new_end <= mid:27            end = mid28        else:29            ret = f(arg[start:new_end])30            if ret is GOOD:31                mid = new_end32            elif ret is BAD:33                end = new_end34            else:35                orig_new_end = new_end36                while ret is INVALID and new_end < end:37                    new_end += 138                    ret = f(arg[start:new_end])39                if mid < new_end and ret is GOOD:40                    mid = new_end41                elif new_end < end and ret is BAD:42                    end = new_end43                else:44                    new_end = orig_new_end45                    while ret is INVALID and mid < new_end:46                        new_end -= 147                        ret = f(arg[start:new_end])48                    if mid == new_end:49                        end = new_end50                    elif ret is GOOD:51                        mid = new_end52                    elif ret is BAD:53                        end = new_end54                    else:55                        sys.stderr.write("This should be impossible\n")56                        end = mid57                        break58    while end + 1 < len(arg) and f(arg[start:end + 1]) == GOOD:59        sys.stderr.write("This should be impossible (2)\n")60        end += 161    orig_end = end62    while end + 1 < len(arg):63        end += 164        if f(arg[start:end]) == GOOD:65            orig_end = end66    return arg[start:orig_end]67@memoize68def check_grep_for(in_str, search_for):69    # print("echo %s | grep -q %s" % (repr(in_str), repr(search_for)))70    p = subprocess.Popen(["timeout", "0.5s", "grep", search_for], universal_newlines=False, stderr=subprocess.PIPE, stdout=subprocess.PIPE, stdin=subprocess.PIPE)71    # print(search_for)72    (stdout, stderr) = p.communicate(input=b(in_str))73    p.stdin.close()74    p.wait()75    if stderr != b('') or p.returncode == 124: # timeout76        return INVALID77    return (GOOD if p.returncode == 0 else BAD)78if __name__ == '__main__':79    if len(sys.argv) != 3:80        sys.stderr.write("USAGE: %s SEARCH_IN SEARCH_FOR\n" % sys.argv[0])81        sys.exit(1)82    def check_grep(search_for):83        return check_grep_for(sys.argv[1], search_for)84    search_for = binary_search_on_string(check_grep, sys.argv[2])85    p = subprocess.Popen(["grep", "--color=auto", search_for], universal_newlines=False, stdin=subprocess.PIPE)86    p.communicate(input=b(sys.argv[1]))87    p.stdin.close()88    p.wait()89    if len(search_for) < len(sys.argv[2]):90        print("Mismatch: good prefix:")91        p = subprocess.Popen(["grep", "-o", "--color=auto", search_for], universal_newlines=False, stdin=subprocess.PIPE)92        p.communicate(input=b(sys.argv[1]))93        p.stdin.close()94        p.wait()95        p = subprocess.Popen(["grep", "-o", '^.*' + search_for], universal_newlines=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)96        (stdout, stderr) = p.communicate(input=b(sys.argv[1]))97        p.stdin.close()98        p.wait()99        print("Mismatch: good prefix search:\n%s" % search_for)100        print("Mismatch: expected next characters at %d: %s" % (len(search_for), repr(sys.argv[2][len(search_for):][:10])))101        print("Mismatch: actual next characters at %d: %s" % (len(stdout)-1, repr(sys.argv[1][len(stdout)-1:][:10])))...text_count.py
Source:text_count.py  
1"""2file: text_count.py3language: python 34author: Duc Phan - ddp3945@rit.edu5description: Homework 3 - CSCI-1416"""7import pdb8def count_text_string(search_for, search_in):9    """Counts and return the number of occurences of the string search_for in the string search_in"""10    if len(search_for) > len(search_in):11        return 012    if search_for == '':13        return 014    s1 = ''15    s2 = search_in16    sum = 017    pdb.set_trace()18    for i in range(len(search_for)):19        s1 = s1 + s2[0]20        s2 = s2[1:]21    if search_for == s1:22        sum = 123    return sum + count_text_string(search_for, search_in[1:])24def count_text_file(search_for, text_file_name):25    """26    - Count and return the number of occurrences of string search_for in file text_file_name27    - Print the lines in the file text_file_name that contain the string search_for28    """29    asum = 030    for lines in open(text_file_name):31        sum = count_text_string(search_for, lines)32        asum = asum + sum33        if sum > 0:34            print(lines.rstrip())35    return asum36def main():37    """38    - Prompt users for a string to search for39    - Prompt users for a filename to search in40    - Print the lines in the file which contain the input string41    - Print the number of occurrences of the input string in the file42    """43    search_for = input('Enter text:')44    text_file_name = input('Enter filename:')45    sum = count_text_file(search_for, text_file_name)46    print('Total count of', search_for, 'is:', sum, sep=' ')...FancyFind.py
Source:FancyFind.py  
1#Write a function called fancy_find. fancy_find should have2#two parameters: search_within and search_for.3#4#fancy_find should check if search_for is found within the5#string search_within. If it is, it should print the message6#"[search_for] found at index [index]!", with [search_for]7#and [index] replaced by the value of search_for and the8#index at which it is found. If search_for is not found9#within search_within, it should print, "[search_for] was10#not found within [search_within]!", again with the values11#of search_for and search_within.12#13#For example:14#15#  fancy_find("ABCDEF", "DEF") -> "DEF found at index 3!"16#  fancy_find("ABCDEF", "GHI") -> "GHI was not found within ABCDEF!"17#Add your function here!18def fancy_find(search_within, search_for):19    if search_for in search_within:20        return search_for + " found at index " +str(search_within.find(search_for)) + "!"21    elif search_for not in search_within:22        return search_for + " was not found within " + search_within + "!"23#Below are some lines of code that will test your function.24#You can change the value of the variable(s) to test your25#function with different inputs.26#27#If your function works correctly, this will originally28#print:29#DEF found at index 3!30#GHI was not found within ABCDEF!31print(fancy_find("ABCDEF", "DEF"))...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!!
