Best Python code snippet using autotest_python
database_connection.py
Source:database_connection.py  
...144        return backend_class()145    def _reached_max_attempts(self, num_attempts):146        return (self.max_reconnect_attempts is not RECONNECT_FOREVER and147                num_attempts > self.max_reconnect_attempts)148    def _is_reconnect_enabled(self, supplied_param):149        if supplied_param is not None:150            return supplied_param151        return self.reconnect_enabled152    def _connect_backend(self, try_reconnecting=None):153        num_attempts = 0154        while True:155            try:156                self._backend.connect(host=self.host, username=self.username,157                                      password=self.password,158                                      db_name=self.db_name)159                return160            except self._backend.OperationalError:161                num_attempts += 1162                if not self._is_reconnect_enabled(try_reconnecting):163                    raise164                if self._reached_max_attempts(num_attempts):165                    raise166                traceback.print_exc()167                print ("Can't connect to database; reconnecting in %s sec" %168                       self.reconnect_delay_sec)169                time.sleep(self.reconnect_delay_sec)170                self.disconnect()171    def connect(self, db_type=None, host=None, username=None, password=None,172                db_name=None, try_reconnecting=None):173        """174        Parameters passed to this function will override defaults from global175        config.  try_reconnecting, if passed, will override176        self.reconnect_enabled.177        """178        self.disconnect()179        self._read_options(db_type, host, username, password, db_name)180        self._backend = self._get_backend(self.db_type)181        _copy_exceptions(self._backend, self)182        self._connect_backend(try_reconnecting)183    def disconnect(self):184        if self._backend:185            self._backend.disconnect()186    def execute(self, query, parameters=None, try_reconnecting=None):187        """188        Execute a query and return cursor.fetchall(). try_reconnecting, if189        passed, will override self.reconnect_enabled.190        """191        if self.debug:192            print 'Executing %s, %s' % (query, parameters)193        # _connect_backend() contains a retry loop, so don't loop here194        try:195            results = self._backend.execute(query, parameters)196        except self._backend.OperationalError:197            if not self._is_reconnect_enabled(try_reconnecting):198                raise199            traceback.print_exc()200            print ("MYSQL connection died; reconnecting")201            self.disconnect()202            self._connect_backend(try_reconnecting)203            results = self._backend.execute(query, parameters)204        self.rowcount = self._backend.rowcount205        return results206    def get_database_info(self):207        return dict((attribute, getattr(self, attribute))208                    for attribute in self._DATABASE_ATTRIBUTES)209    @classmethod210    def get_test_database(cls, file_path=':memory:', **constructor_kwargs):211        """...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!!
