Code Review for Python code
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

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.

The code:

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()

The class 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.

What we want is a review of the function add_cookie_header. We would like you to tell us how to refactor our code to be more elegant, efficient and maintainable.
We expect a list of comments, in the form of: "Rename CookieJar to AGoodClass", for example.

Crowdsource coding tasks.

1 Solution

Winning solution

Here is a proposed solution based on my experience in writing code in the python 3 language:

class CookieJar(object):
    def add_cookie_header(self, request):
        # CR: change w_r to a more indicative name
        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
        # CR: more elegant way is checking if the variable is None
        if not req_host:
            return

        if not IPV4_RE.search(req_host):
            hosts = potential_domain_matches(req_host)
            if '.' not in req_host:
                # CR: change l_req_host to a more indicative name
                # CR: use f-strings. They are a new and improved way to format string in python3.
                l_req_host = "%s%s" % (req_host, '.local')
                hosts += [l_req_host]
        else:
            hosts = [req_host]

        # CR: variable name should be lower case
        Cookies = []
        # CR: run on the list objects themselves, do not use the indeces if not needed
        for hosti in range(len(hosts)):
            if hosts[hosti] in self.jar._cookies:
                Cookies.extend(self.jar._cookies_for_domain(hosts[hosti], w_r))

        # CR: change ats to a more indicative name
        ats = self.jar._cookie_attrs(Cookies)
        if ats:
            if w_r.has_header("Cookie") == False:
                w_r.add_unredirected_header("Cookie", "; ".join(ats))

        # CR: use += syntax
        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()