Best Python code snippet using autotest_python
person.py
Source:person.py  
...21        print "Hello! I am {} years old".format(self.age)22    23#**********************24# Pnum() class25# built to explore issues iwth using __gte() and __lte__() as overloads.26# ######################27class Pnum(object):28    def __init__(self,29                 age=2730                 ):31        self.age = age32 33   #Used to compare self with another object        34    def __eq__(self, otherObj):35        if self.age == otherObj.age:36            return True37        else:38            return False39    def __ne__(self, otherObj):40        a = self.__eq__(otherObj=otherObj)41        if a:42            return False43        else:44            return True45    def showMe(self):46        print "My age: " ,self.age47    #------------------------------    48    def __gte__(self, otherObj):49#        print ("CHECKER:  ", self.age, ">=", otherObj.age)50        if self.age >= otherObj.age:51            return True52        else:53            return False54        55#**********************56# Person() class        57#  Rewrote the Person class to replace __gte__() and __lte__() overloades58#  with __ge__() and __le__().59# ######################60class Person(object):61    def __init__(self,62                 name= "Noone",63                 age=2764                 ):65        self.name = name 66        self.age = age67        self.text = "Hello there {:)){-<',"68 69   #Used to compare self with another object        70    def __eq__(self, otherObj):71        if (self.name == otherObj.name and 72            self.age == otherObj.age73            ):74            return True75        else:76            return False77    def __ne__(self, otherObj):78        #Note:  calling the following with <otherObj> works fine because79        #           self is automatically passed.80        a = self.__eq__(otherObj=otherObj)81        if a:82            return False83        else:84            return True85    def showMe(self):86        print "My name: " ,self.name87        print "My age: " ,self.age88    #------------------------------89    # Note:  "__ge__()" is the overload for ">=" operator and NOT  "__gte__()"90    #           as listed in the SAMS Python book example and in Table 12.1.91    def __ge__(self, otherObj):92        print ("CHECKER:  ", self.age, ">=", otherObj.age)93        if (self.age >= otherObj.age):94            return True95        else:96            return False97    # Note:  "__le__()" is the overload for ">=" operator and NOT  "__lte__()"98    #           as listed in the SAMS Python book example and in Table 12.1.99    def __le__(self, otherObj):100        if (self.age <= otherObj.age):101            return True102        else:103            return False104    def __gt__(self, otherObj):105        if (self.age > otherObj.age):106            return True107        else:108            return False109    def __lt__(self, otherObj):110        if (self.age < otherObj.age):111            return True112        else:113            return False114    def __str__(self):115        #This __str__() is used to overload the "print()"116        whoAmI = self.text + "\n------------------------------\n"117        whoAmI += (" <me>.name = " + str(self.name) + "\n <me>.age    = " + str(self.age) +118        '\n <me>.text   =  "' + str(self.text) + '"\n---------------------------\n' + " My methods are:\n---------------------------\n" +119        " .showMe() = shows my name and age.\n" +120        " Arithmetic Operatos: [ =, >, < >=, <= ],  where these compare <me>.age to the external value.\n" +121        " print <me>:  shows this print out detailing my properties and methods :o)~")122                           123        return whoAmI124        125        126        #I would like to go further in coding here than what is in the book.127        #I agree that as in the book, a programmer may want to compare a subset128        #of attributes.  However, I would like to compare ALL attributes with129        #a loop; will have to research this if not covered in this chapter.130    131#**********************132#  2]# The original Person() class133#     ORIGINAL Built-in Extras example using __gte__() and __lte__() that did134#     not work as a ">=" and "<=" overload.135#     Built a simpler class to reduce typing instead of re-using Student and136#     the old person() class.  the following is a new simpler person137#     with __eq__(), __gte__(), __lte__ and other common methods overloaded.138# ######################139class Person_original(object):140    def __init__(self,141                 name= "Noone",142                 age=27143                 ):144        self.name = name 145        self.age = age146        self.text = "Hello there {:)){-<',"147 148   #Used to compare self with another object        149    def __eq__(self, otherObj):150        if (self.name == otherObj.name and 151            self.age == otherObj.age152            ):153            return True154        else:155            return False156    def __ne__(self, otherObj):157        #Note:  calling the following with <otherObj> works fine because158        #           self is automatically passed.159        a = self.__eq__(otherObj=otherObj)160        if a:161            return False162        else:163            return True164    def showMe(self):165        print "My name: " ,self.name166        print "My age: " ,self.age167    #------------------------------168    # Note:  "__ge__()" is the overload for ">=" operator and NOT  "__gte__()"169    #           as listed in the SAMS Python book example and in Table 12.1.170    def __gte__(self, otherObj):171        print ("CHECKER:  ", self.age, ">=", otherObj.age)172        if (self.age >= otherObj.age):173            return True174        else:175            return False176    # Note:  "__le__()" is the overload for ">=" operator and NOT  "__lte__()"177    #           as listed in the SAMS Python book example and in Table 12.1.178    def __lte__(self, otherObj):179        if (self.age <= otherObj.age):180            return True181        else:182            return False183    def __gt__(self, otherObj):184        if (self.age > otherObj.age):185            return True186        else:187            return False188    def __lt__(self, otherObj):189        if (self.age < otherObj.age):190            return True191        else:192            return False...query_builder.py
Source:query_builder.py  
1from service.esutil import dsl_search2from elasticsearch_dsl import Q3from service.esutil.querybuilder.query.query import Query4class Operator(object):5    # equals, contains, wildcard, gt, gte, lt, lte6    def __init__(self, **kwargs):7        self.kwargs = kwargs8        self.operator = kwargs.get('operator')9    __equals__ = "equals"10    __contains__ = "contains"11    __wildcard__ = "wildcard"12    __gt__ = "gt"13    __gte__ = "gte"14    __lt__ = "lt"15    __lte__ = "lte"16    def get_query_type_instance(self):17        if self.operator == self.__equals__:18            return Query.instantiate(Query.TERM, **self.kwargs)19        elif self.operator == self.__contains__:20                return Query.instantiate(Query.MULTIMATCH, **self.kwargs)21        elif self.operator in [self.__wildcard__]:22                return Query.instantiate(Query.WILDCARD, **self.kwargs)23        elif self.operator in [self.__gt__, self.__gte__, self.__lt__, self.__lte__]:24                return Query.instantiate(Query.RANGE, **self.kwargs)25class QueryBuilder(object):26    def __init__(self, criteria):27        self.criteria = criteria28    def _get_fragments(self):29        bool_query_fragments = {30            "AND": [],  # must31            "OR": [],   # should32            "NOT": []   # must_not33        }34        for operator_type, operator_type_criteria in self.criteria.items():35            for operator_type_criterion in operator_type_criteria:36                query_type_obj = Operator(**operator_type_criterion).get_query_type_instance()37                q = query_type_obj.build()38                bool_query_fragments[operator_type].append(q)39        return bool_query_fragments40    def _combine_query(self):41        processed_fragments = dict()42        fragments = self._get_fragments()43        print fragments.items()44        for op, queries in fragments.items():45            if len(queries) > 0:46                if op == "AND":47                    processed_fragments["must"] = queries48                elif op == "OR":49                    processed_fragments["should"] = queries50                elif op == "NOT":51                    processed_fragments["must_not"] = queries52        combined_q = Q('bool', **processed_fragments)53        return combined_q54    def search(self, index=None, doc_type=None):55        q = self._combine_query()56        s = dsl_search.index(index).doc_type(doc_type).query(q)57        return s58    @classmethod59    def get_raw_query(cls, s):60        return s.to_dict()61    @classmethod62    def execute(cls, s):...__init__.py
Source:__init__.py  
...16	def __gte__(self, value):17		return PyMongoQuery({self.key: {'$gte': value}})18	def __lt__(self, value):19		return PyMongoQuery({self.key: {'$lt': value}})20	def __lte__(self, value):21		return PyMongoQuery({self.key: {'$lte': value}})22	def starts_with(self, string):23		return PyMongoQuery({self.key: {'$regex': '^' + re.escape(string)}})24	def ends_with(self, string):...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!!
