def sweep(self):
        '''
        This methods runs in a separate thread. So long as
        `self.sweep_flag` is set, it expires keys according to
        the process explained in the docstring for the `TimedDict`
        class. The thread is halted by calling `self.stop_sweep()`,
        which sets the `self.sweep_flag` to `False`.
        '''

        while self.sweep_flag:
            current_time = time.time()
            expire_keys = set()
            keys_checked = 0.
            items = list(self.time_dict.items())
            for key, expire_time in items:
                if random.random() > self.sample_probability:
                    continue
                keys_checked += 1
                if current_time >= expire_time:
                    expire_keys.add(key)
                    logging.debug(
                        'marking key for deletion: {key}'.
                        format(key=str(key)))
            for key in expire_keys:
                self.expire_key(key)
            expired_keys_ratio = (
                len(expire_keys) / keys_checked
                if keys_checked > 0 else 0.)
            if expired_keys_ratio < self.expired_keys_ratio:
                time.sleep(1. / self.checks_per_second)
This methods runs in a separate thread. So long as
        `self.sweep_flag` is set, it expires keys according to
        the process explained in the docstring for the `TimedDict`
        class. The thread is halted by calling `self.stop_sweep()`,
        which sets the `self.sweep_flag` to `False`.