Best Python code snippet using autotest_python
scheduler_models.py
Source:scheduler_models.py  
...126            self._instances_by_type_and_id[(type(self), id)] = self127        self.__table = self._table_name128        self.__new_record = new_record129        if row is None:130            row = self._fetch_row_from_db(id)131        if self._initialized:132            differences = self._compare_fields_in_row(row)133            if differences:134                logging.warning(135                    'initialized %s %s instance requery is updating: %s',136                    type(self), self.id, differences)137        self._update_fields_from_row(row)138        self._initialized = True139    @classmethod140    def _clear_instance_cache(cls):141        """Used for testing, clear the internal instance cache."""142        cls._instances_by_type_and_id.clear()143    def _fetch_row_from_db(self, row_id):144        fields = ', '.join(self._fields)145        sql = 'SELECT %s FROM %s WHERE ID=%%s' % (fields, self.__table)146        rows = _db.execute(sql, (row_id,))147        if not rows:148            raise DBError("row not found (table=%s, row id=%s)"149                          % (self.__table, row_id))150        return rows[0]151    def _assert_row_length(self, row):152        assert len(row) == len(self._fields), (153            "table = %s, row = %s/%d, fields = %s/%d" % (154            self.__table, row, len(row), self._fields, len(self._fields)))155    def _compare_fields_in_row(self, row):156        """157        Given a row as returned by a SELECT query, compare it to our existing in158        memory fields.  Fractional seconds are stripped from datetime values159        before comparison.160        @param row - A sequence of values corresponding to fields named in161                The class attribute _fields.162        @returns A dictionary listing the differences keyed by field name163                containing tuples of (current_value, row_value).164        """165        self._assert_row_length(row)166        differences = {}167        for field, row_value in itertools.izip(self._fields, row):168            current_value = getattr(self, field)169            if (isinstance(current_value, datetime.datetime)170                and isinstance(row_value, datetime.datetime)):171                current_value = current_value.strftime(time_utils.TIME_FMT)172                row_value = row_value.strftime(time_utils.TIME_FMT)173            if current_value != row_value:174                differences[field] = (current_value, row_value)175        return differences176    def _update_fields_from_row(self, row):177        """178        Update our field attributes using a single row returned by SELECT.179        @param row - A sequence of values corresponding to fields named in180                the class fields list.181        """182        self._assert_row_length(row)183        self._valid_fields = set()184        for field, value in itertools.izip(self._fields, row):185            setattr(self, field, value)186            self._valid_fields.add(field)187        self._valid_fields.remove('id')188    def update_from_database(self):189        assert self.id is not None190        row = self._fetch_row_from_db(self.id)191        self._update_fields_from_row(row)192    def count(self, where, table = None):193        if not table:194            table = self.__table195        rows = _db.execute("""196                SELECT count(*) FROM %s197                WHERE %s198        """ % (table, where))199        assert len(rows) == 1200        return int(rows[0][0])201    def update_field(self, field, value):202        assert field in self._valid_fields203        if getattr(self, field) == value:204            return...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!!
