How to use _realm_redis_key method in selenium-respectful

Best Python code snippet using selenium-respectful_python

respectful_requester.py

Source: respectful_requester.py Github

copy

Full Screen

...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"]...

Full Screen

Full Screen

Blogs

Check out the latest blogs from LambdaTest on this topic:

Do you possess the necessary characteristics to adopt an Agile testing mindset?

To understand the agile testing mindset, we first need to determine what makes a team “agile.” To me, an agile team continually focuses on becoming self-organized and cross-functional to be able to complete any challenge they may face during a project.

What exactly do Scrum Masters perform throughout the course of a typical day

Many theoretical descriptions explain the role of the Scrum Master as a vital member of the Scrum team. However, these descriptions do not provide an honest answer to the fundamental question: “What are the day-to-day activities of a Scrum Master?”

Project Goal Prioritization in Context of Your Organization&#8217;s Strategic Objectives

One of the most important skills for leaders to have is the ability to prioritize. To understand how we can organize all of the tasks that must be completed in order to complete a project, we must first understand the business we are in, particularly the project goals. There might be several project drivers that stimulate project execution and motivate a company to allocate the appropriate funding.

Putting Together a Testing Team

As part of one of my consulting efforts, I worked with a mid-sized company that was looking to move toward a more agile manner of developing software. As with any shift in work style, there is some bewilderment and, for some, considerable anxiety. People are being challenged to leave their comfort zones and embrace a continuously changing, dynamic working environment. And, dare I say it, testing may be the most ‘disturbed’ of the software roles in agile development.

Why Agile Is Great for Your Business

Agile project management is a great alternative to traditional methods, to address the customer’s needs and the delivery of business value from the beginning of the project. This blog describes the main benefits of Agile for both the customer and the business.

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run selenium-respectful automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful