def _update_cache(self, start: int, end: int): fetch_set = set(set(range(start, end)) - self._cached_items) while len(fetch_set) != 0: fetch_range = next(range_in_set(fetch_set)) page = int(fetch_range.start / self._page_size) + 1 self._cached_items.update( range(*self._fetch_range(page, self._page_size))) fetch_set = set(set(range(start, end)) - self._cached_items)
def v_split(img): """ A helper function to crop images vertically. :param img: input image. :return cropped images. """ row_filled = { row if LegacyRecognizer.row_not_empty(img, row) else None for row in range(img.height) } row_filled.remove(None) segments = list(range_in_set(row_filled)) top = min(segment.start for segment in segments) bottom = max(segment.stop for segment in segments) return img.crop((0, top, img.width, bottom))
def h_split(img): """ A helper function to split captcha into segments horizontally using projection. :param img: input image. :return segmented images. """ col_filled = { col if LegacyRecognizer.col_not_empty(img, col) else None for col in range(img.width) } col_filled.remove(None) segments = range_in_set(col_filled) rtn = [] for segment in segments: rtn.append( img.crop((segment.start, 0, segment.stop - 1, img.height))) return rtn
def test_range_in_set(): assert len(list(range_in_set({}))) == 0 ranges = range_in_set({1, 3, 4, 5, 7, 8, 9, 10}) assert isinstance(ranges, Generator) assert list(ranges) == [range(1, 2), range(3, 6), range(7, 11)]