Best Python code snippet using selenium-respectful_python
respectful_requester.py
Source: respectful_requester.py
...57 self.redis.smembers("%s:REALMS" % self.redis_prefix),58 )59 )60 def register_realm(self, realm, max_requests, timespan):61 redis_key = self._realm_redis_key(realm)62 if not self.redis.hexists(redis_key, "max_requests"):63 self.redis.hmset(64 redis_key, {"max_requests": max_requests, "timespan": timespan}65 )66 self.redis.sadd("%s:REALMS" % self.redis_prefix, realm)67 return True68 def register_realms(self, realm_tuples):69 for realm_tuple in realm_tuples:70 self.register_realm(*realm_tuple)71 return True72 def update_realm(self, realm, **kwargs):73 redis_key = self._realm_redis_key(realm)74 updatable_keys = ["max_requests", "timespan"]75 for updatable_key in updatable_keys:76 if updatable_key in kwargs and type(kwargs[updatable_key]) == int:77 self.redis.hset(redis_key, updatable_key, kwargs[updatable_key])78 return True79 def unregister_realm(self, realm):80 self.redis.delete(self._realm_redis_key(realm))81 self.redis.srem("%s:REALMS" % self.redis_prefix, realm)82 request_keys = self.redis.keys("%s:REQUEST:%s:*" % (self.redis_prefix, realm))83 [self.redis.delete(k) for k in request_keys]84 return True85 def unregister_realms(self, realms):86 for realm in realms:87 self.unregister_realm(realm)88 return True89 def realm_max_requests(self, realm):90 realm_info = self._fetch_realm_info(realm)91 return int(realm_info["max_requests".encode("utf-8")].decode("utf-8"))92 def realm_timespan(self, realm):93 realm_info = self._fetch_realm_info(realm)94 return int(realm_info["timespan".encode("utf-8")].decode("utf-8"))95 @classmethod96 def configure(cls, **kwargs):97 if "redis" in kwargs:98 if type(kwargs["redis"]) != dict:99 raise RequestsRespectfulConfigError("'redis' key must be a dict")100 expected_redis_keys = ["host", "port", "password", "database"]101 missing_redis_keys = list()102 for expected_redis_key in expected_redis_keys:103 if expected_redis_key not in kwargs["redis"]:104 missing_redis_keys.append(expected_redis_key)105 if len(missing_redis_keys):106 raise RequestsRespectfulConfigError(107 "'%s' %s missing from the 'redis' configuration key"108 % (109 ", ".join(missing_redis_keys),110 "is" if len(missing_redis_keys) == 1 else "are",111 )112 )113 config["redis"] = kwargs["redis"]114 global redis115 redis = StrictRedis(116 host=config["redis"]["host"],117 port=config["redis"]["port"],118 password=config["redis"]["password"],119 db=config["redis"]["database"],120 )121 if "safety_threshold" in kwargs:122 if (123 type(kwargs["safety_threshold"]) != int124 or kwargs["safety_threshold"] < 0125 ):126 raise RequestsRespectfulConfigError(127 "'safety_threshold' key must be a positive integer"128 )129 config["safety_threshold"] = kwargs["safety_threshold"]130 if "requests_module_name" in kwargs:131 if type(kwargs["requests_module_name"]) != str:132 raise RequestsRespectfulConfigError(133 "'requests_module_name' key must be string"134 )135 config["requests_module_name"] = kwargs["requests_module_name"]136 return config137 @classmethod138 def configure_default(cls):139 for key in config:140 config[key] = default_config[key]141 return config142 def _perform_request(self, request_func, realms=None):143 self._validate_request_func(request_func)144 rate_limited_realms = list()145 for realm in realms:146 if not self._can_perform_request(realm):147 rate_limited_realms.append(realm)148 if not len(rate_limited_realms):149 for realm in realms:150 request_uuid = str(uuid.uuid4())151 self.redis.setex(152 name="%s:REQUEST:%s:%s" % (self.redis_prefix, realm, request_uuid),153 time=self.realm_timespan(realm),154 value=request_uuid,155 )156 return request_func()157 else:158 raise RequestsRespectfulRateLimitedError(159 "Currently rate-limited on Realm(s): %s"160 % ", ".join(rate_limited_realms)161 )162 def _realm_redis_key(self, realm):163 return "%s:REALMS:%s" % (self.redis_prefix, realm)164 def _fetch_realm_info(self, realm):165 redis_key = self._realm_redis_key(realm)166 return self.redis.hgetall(redis_key)167 def _requests_in_timespan(self, realm):168 return len(169 self.redis.scan(170 cursor=0,171 match="%s:REQUEST:%s:*" % (self.redis_prefix, realm),172 count=self._redis_keys_in_db() + 100,173 )[1]174 )175 def _redis_keys_in_db(self):176 return self.redis.info().get("db%d" % config["redis"]["database"]).get("keys")177 def _can_perform_request(self, realm):178 return self._requests_in_timespan(realm) < (179 self.realm_max_requests(realm) - config["safety_threshold"]...
Check out the latest blogs from LambdaTest on this topic:
When it comes to UI components, there are two versatile methods that we can use to build it for your website: either we can use prebuilt components from a well-known library or framework, or we can develop our UI components from scratch.
The best agile teams are built from people who work together as one unit, where each team member has both the technical and the personal skills to allow the team to become self-organized, cross-functional, and self-motivated. These are all big words that I hear in almost every agile project. Still, the criteria to make a fantastic agile team are practically impossible to achieve without one major factor: motivation towards a common goal.
Ever since the Internet was invented, web developers have searched for the most efficient ways to display content on web browsers.
Hey LambdaTesters! We’ve got something special for you this week. ????
How do we acquire knowledge? This is one of the seemingly basic but critical questions you and your team members must ask and consider. We are experts; therefore, we understand why we study and what we should learn. However, many of us do not give enough thought to how we learn.
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!!