Best Python code snippet using autotest_python
ir.py
Source:ir.py  
1## Maintains the interface for the intermediate representation2## This can be sent in and out of both code generators and3## the GHD optimizer.4import jpype5def strip_unicode(values):6  return [str(x) for x in values]7#Convinance class for expressing relations8class RELATION:9  def __init__(self,name,attributes,annotations=[],scaling=1):10    self.name = name11    self.attributes = attributes12    self.annotations = annotations13  def __repr__(self):14    return """[%s %s,%s]""" % (self.name,\15      self.attributes,\16      self.annotations)17class IR:18  def __init__(self,rules):19    if not isinstance(rules[0],RULE):20      raise Exception("IR type incorrect (list of RULES).")21    self.rules = rules22    self.duncecap = jpype.JPackage('duncecap')23  def python2java(self):24    irbuilder = self.duncecap.IRBuilder()25    for r in self.rules:26      irbuilder.addRule(r.python2java())27    return irbuilder.build()28  @staticmethod29  def java2python(jobject):30    rules = []31    nRules = jobject.getNumRules()32    for i in range(0,nRules):33      rules.append(RULE.java2python(jobject.getRule(i)))34    return IR(rules)35#The top level class36class RULE:37  def __init__(self,38    result,39    recursion,40    operation,41    order,42    project,43    join,44    aggregates,45    filters,46    ):47    if not isinstance(result,RESULT) or \48      not isinstance(recursion,RECURSION) or \49      not isinstance(operation,OPERATION) or \50      not isinstance(order,ORDER) or \51      not isinstance(project,PROJECT) or \52      not isinstance(join,JOIN) or \53      not isinstance(aggregates,AGGREGATES) or \54      not isinstance(filters,FILTERS):55      raise Exception("Types not correct for IR.")56    self.duncecap = jpype.JPackage('duncecap')57    self.result = result58    self.recursion = recursion59    self.operation = operation60    self.order = order61    self.project = project62    self.join = join63    self.aggregates = aggregates64    self.filters = filters65  def python2java(self):66    javaResult = self.result.python2java(self.duncecap)67    javaRecursion = self.recursion.python2java(self.duncecap)68    javaOperation = self.operation.python2java(self.duncecap)69    javaOrder = self.order.python2java(self.duncecap)70    javaProject = self.project.python2java(self.duncecap)71    javaJoin = self.join.python2java(self.duncecap)72    javaAgg = self.aggregates.python2java(self.duncecap)73    javaFilter = self.filters.python2java(self.duncecap)74    return self.duncecap.Rule(75      javaResult,76      javaRecursion,77      javaOperation,78      javaOrder,79      javaProject,80      javaJoin,81      javaAgg,82      javaFilter)83  @staticmethod84  def java2python(jobject):85    result = RESULT.java2python(jobject.getResult())86    recursion = RECURSION.java2python(jobject.getRecursion())87    operation = OPERATION.java2python(jobject.getOperation())88    order = ORDER.java2python(jobject.getOrder())89    project = PROJECT.java2python(jobject.getProject())90    join = JOIN.java2python(jobject.getJoin())91    filters = FILTERS.java2python(jobject.getFilters())92    aggregates = AGGREGATES.java2python(jobject.getAggregations())93    return RULE(result,recursion,operation,order,project,join,aggregates,filters)94  def __repr__(self):95    return """RULE :-\t %s \n\t %s \n\t %s \n\t %s \n\t %s \n\t %s \n\t %s \n\t %s>""" \96      % (self.result,\97      self.recursion,\98      self.operation,\99      self.order,\100      self.project,\101      self.join,\102      self.aggregates,\103      self.filters)104#Relation for the result105class RESULT:106  def __init__(self,rel,isIntermediate):107    self.rel = rel108    self.isIntermediate = isIntermediate109  def python2java(self,duncecap):110    return duncecap.Result(duncecap.IR.buildRel(self.rel.name,111      strip_unicode(self.rel.attributes),112      strip_unicode(self.rel.annotations)), self.isIntermediate)113  @staticmethod114  def java2python(jobject):115    return RESULT(RELATION(116      jobject.getRel().getName(),117      strip_unicode(jobject.getRel().getAttributes()),118      strip_unicode(jobject.getRel().getAnnotations())), jobject.getIsIntermediate())119  def __repr__(self):120    return """RESULT: %s %s""" % (self.rel, self.isIntermediate)121#Holder for recursive statements122class RECURSION:123  def __init__(self,criteria="",operation="",value=""):124    self.criteria = criteria125    self.operation = operation126    self.value = value127  def python2java(self,duncecap):128    return duncecap.RecursionBuilder().build(129      self.criteria,130      self.operation,131      self.value)132  @staticmethod133  def java2python(jobject):134    if jobject.isEmpty():135      return RECURSION()136    else:137      newobj = jobject.get()138      return RECURSION(139        newobj.getCriteria(),140        newobj.getOperation(),141        newobj.getValue())142  def __repr__(self):143    return """RECURSION: %s %s %s """ % (self.criteria,self.operation,self.value)144#Order attributes are processed in NPRR145class ORDER:146  def __init__(self,attributes):147    self.attributes = attributes148  def python2java(self,duncecap):149    return duncecap.Order(150      duncecap.IR.buildAttributes(self.attributes))151  @staticmethod152  def java2python(jobject):153    return ORDER(strip_unicode(jobject.getAttributes()))154  def __repr__(self):155    return """ORDER: %s""" % (self.attributes)156#Attributes that are projected away.157class PROJECT:158  def __init__(self,attributes=[]):159    self.attributes = attributes160  def python2java(self,duncecap):161    return duncecap.Project(162      duncecap.IR.buildAttributes(self.attributes))163  @staticmethod164  def java2python(jobject):165    return PROJECT(strip_unicode(jobject.getAttributes()))166  def __repr__(self):167    return """PROJECT: %s""" % (self.attributes)168#Join operation for aggregations169class OPERATION:170  def __init__(self,operation):171    self.operation = operation172  def python2java(self,duncecap):173    return duncecap.Operation(self.operation)174  @staticmethod175  def java2python(jobject):176    return OPERATION(str(jobject.getOperation()))177  def __repr__(self):178    return """OPERATION: %s""" % (self.operation)179#Relations and attributes that are joined.180class JOIN:181  def __init__(self,relations):182    self.relations = relations183  def python2java(self,duncecap):184    joinBuilder = duncecap.JoinBuilder()185    for rel in self.relations:186      joinBuilder.addRel(rel.name,rel.attributes,rel.annotations)187    return joinBuilder.build()188  @staticmethod189  def java2python(jobject):190    nRels = jobject.getNumRels()191    relations = []192    for i in range(0,nRels):193      relations.append(RELATION(194        jobject.getRel(i).getName(),195        strip_unicode(jobject.getRel(i).getAttributes()),196        strip_unicode(jobject.getRel(i).getAnnotations())))197    return JOIN(relations)198  def __repr__(self):199    return """JOIN: %s""" % (self.relations)200class SELECT:201  def __init__(self,attribute,operation,value):202    self.attribute = attribute203    self.operation = operation204    self.value = value205  def python2java(self,filterBuilder):206    return filterBuilder.buildSelection(self.attribute,self.operation,self.value)207  @staticmethod208  def java2python(jobject):209    return SELECT(jobject.getAttr(),jobject.getOperation(),jobject.getValue())210  def __repr__(self):211    return """SELECT(%s,%s,%s)""" % (self.attribute,self.operation,self.value)212#Relations and attributes that are joined.213class FILTERS:214  def __init__(self,filters=[]):215    if filters:216      if not isinstance(filters[0],SELECT):217        raise Exception("Filters types incorrect.")218    self.filters = filters219  def python2java(self,duncecap):220    filterBuilder = duncecap.FilterBuilder()221    for f in self.filters:222      filterBuilder.addSelection(f.python2java(filterBuilder))223    return filterBuilder.build()224  @staticmethod225  def java2python(jobject):226    nFilters = jobject.getNumFilters()227    filters = []228    for i in range(0,nFilters):229      filters.append(SELECT.java2python(jobject.getSelect(i)))230    return FILTERS(filters)231  def __repr__(self):232    return """FILTERS: %s""" % (self.filters)233#Aggregations over attributes.234class AGGREGATE:235  def __init__(self,annotation="",datatype="",operation="",attributes=[],init="",expression="",usedScalars=[]):236    self.annotation = annotation237    self.datatype = datatype238    self.operation = operation239    self.attributes = attributes240    self.init = init241    self.expression = expression242    self.usedScalars = usedScalars243  def __repr__(self):244    return """(%s,%s,%s,%s,%s,%s,%s)""" % (245      self.annotation,246      self.datatype,247      self.operation,248      self.attributes,249      self.init,250      self.expression,251      [RELATION(252          jobject.getName(),253          strip_unicode(jobject.getAttributes()),254          strip_unicode(jobject.getAnnotations())) for jobject in self.usedScalars])255class AGGREGATES:256  def __init__(self,aggregates):257    if aggregates:258      if not isinstance(aggregates[0],AGGREGATE):259        raise Exception("Aggregate types incorrect.")260    self.aggregates = aggregates261  def python2java(self,duncecap):262    aggBuilder = duncecap.AggregationsBuilder()263    for agg in self.aggregates:264      aggBuilder.addAggregation(265        agg.annotation,266        agg.type,267        agg.operation,268        agg.attributes,269        agg.init,270        agg.expression,271        agg.usedScalars)272    return aggBuilder.build()273  @staticmethod274  def java2python(jobject):275    nAggs = jobject.getNumAggregations()276    aggs = []277    for i in range(0,nAggs):278      aggs.append(AGGREGATE(279        jobject.getAnnotation(i),280        jobject.getDatatype(i),281        jobject.getOperation(i),282        strip_unicode(jobject.getAttributes(i)),283        jobject.getInit(i),284        jobject.getExpression(i),285        jobject.getDependedOnRels(i)))286    return AGGREGATES(aggs)287  def __repr__(self):...farm2table.py
Source:farm2table.py  
1# -*- coding: cp1252 -*-2from bs4 import BeautifulSoup3from collections import namedtuple4from Table import *5import urllib6import re7class Page:8    def __init__(self, url):9        """10        Retrieves and stores the urllib.urlopen object for a given url11        """12        self.link = urllib.urlopen(url)13    def get_tables(self):14        """15        Extracts each table on the page and places it in a dictionary.16        Converts each dictionary to a Table object. Returns a list of17        pointers to the respective Table object(s).18        """19        raw_html = self.link.read()20        soup = BeautifulSoup(raw_html, "html.parser")21        tables = soup.findAll("table")22        # have to extract each entry using nested loops23        table_list = []24        for table in tables:25            # empty dictionary each time represents our table26            table_dict = {}27            rows = table.findAll("tr")28            # count will be the key for each list of values29            count = 030            for row in rows:31                value_list = []32                entries = row.findAll("td")33                name_entries = row.findAll("td")[1::1]34                for entry in name_entries:35                    images = entry.findAll("img")36                    # fix the encoding issues with utf-837                    entry = entry.text.encode("utf-8", "ignore")38                    strip_unicode = re.compile("([^-_a-zA-Z0-9!@#%&=,/'\";:~`\$\^\*\(\)\+\[\]\.\{\}\|\?\<\>\\]+|[^\s]+)")39                    entry = strip_unicode.sub(" ", entry)40                    entry = entry.replace('Â', '')41                    entrystripped = entry.replace('Champion', '')42                    value_list.append(entrystripped)43                    if "star" in str(images):44                        value_list.append("premium")45                        if "Champion" in entry:46                            value_list.append("champion")47                        else:48                            value_list.append("standard")49                    else:    50                        value_list.append("regular")51                        if "Champion" in entry:52                            value_list.append("champion")53                        else:54                            value_list.append("standard")55                        56                57                for entry in entries:58                    images = entry.findAll("img")59                    # fix the encoding issues with utf-860                    entry = entry.text.encode("utf-8", "ignore")61                    strip_unicode = re.compile("([^-_a-zA-Z0-9!@#%&=,/'\";:~`\$\^\*\(\)\+\[\]\.\{\}\|\?\<\>\\]+|[^\s]+)")62                    entry = strip_unicode.sub(" ", entry)63                    entry = entry.replace('Â', '')64                    entrystripped = entry.replace('Champion', '')65                    value_list.append(entrystripped)66                # we don't want empty data packages67                if len(value_list) > 0:68                    table_dict[count] = value_list69                    count += 170            table_obj = Table(table_dict)71            table_list.append(table_obj)72        return table_list73    def save_tables(self, tables, ignore_small=False):74        """75        Takes an input a list of table objects and saves each76        table to csv format. If ignore_small is True,77        we ignore any tables with 5 entries or fewer. 78        """79        counter = 180        for table in tables:81            if ignore_small:82                if table.get_metadata().num_entries > 5:83                    name = "table" + str(counter)84                    table.save_table(name)85                    counter += 186            else:87                name = "table" + str(counter)88                table.save_table(name)...redditcheck.py
Source:redditcheck.py  
1import json2import praw3import config4from datetime import datetime, timezone, timedelta5def strip_unicode(s):6    return s.encode('ascii', 'ignore').decode()7class Reddit(object):8    def __init__(self):9        self.reddit = praw.Reddit(10            client_id=config.REDDIT_CREDS['client_id'],11            client_secret=config.REDDIT_CREDS['client_secret'],12            password=config.REDDIT_CREDS['password'],13            user_agent=config.REDDIT_CREDS['user_agent'],14            username=config.REDDIT_CREDS['username'],15        )16        self.filter_func = lambda x: x17        print("Successfully logged into reddit as user %s" % self.reddit.user.me())18    def check_subreddit(self, subreddit_name, max_time = None):19        try:20            subreddit = self.reddit.subreddit(subreddit_name)21            posts = subreddit.new(limit=200)22        except:23            print("Failed to fetch posts.")24            return []25        filtered_posts = self._parse_posts(posts)26        return filtered_posts27    28    def set_filter_func(self, func):29        self.filter_func = func30    def _parse_posts(self, posts):31        formatted_posts = list(map(self._format_post, posts))32        return list(filter(self.filter_func, formatted_posts))33    def _format_post(self, post):34        return {35            'id': strip_unicode(str(post)),36            'title': strip_unicode(post.title),37            'reddit_url': 'reddit.com%s' % strip_unicode(post.permalink),38            'link': strip_unicode(post.url),39            'body': strip_unicode(post.selftext),40            'created': post.created_utc41        }...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!!
