We would like for a code review for our Python code. The code is written in Python 3.7 and is part of a class handling HTTP cookies.
class CookieJar(object): def add_cookie_header(self, request): w_r = WrappedRequest(request) self.policy._now = self.jar._now = int(time.time()) # the cookiejar implementation iterates through all domains # instead we restrict to potential matches on the domain req_host = request.hostname if not req_host: return if not IPV4_RE.search(req_host): hosts = potential_domain_matches(req_host) if '.' not in req_host: l_req_host = "%s%s" % (req_host, '.local') hosts += [l_req_host] else: hosts = [req_host] Cookies =  for hosti in range(len(hosts)): if hosts[hosti] in self.jar._cookies: Cookies.extend(self.jar._cookies_for_domain(hosts[hosti], w_r)) ats = self.jar._cookie_attrs(Cookies) if ats: if w_r.has_header("Cookie") == False: w_r.add_unredirected_header("Cookie", "; ".join(ats)) self.processed = self.processed + 1 if self.processed % self.check_expired_frequency == 0: # This is still quite inefficient for large number of Cookies self.jar.clear_expired_cookies()
CookieJar has other fields and methods, obviously, but they shouldn't be reviewed, so they're not provided here.
You can assume all required imports are present at the top of the file and all referenced methods and fields do exist.
How can we make our code more elegant, efficient and maintainable?