def make_array(self): chapter_ranges = [] for rng in self.ranges: min_chapter = Pyricope._get_chapter(min(rng)) max_chapter = Pyricope._get_chapter(max(rng)) if min_chapter == max_chapter: chapter_ranges.append(rng) else: chapter_ranges += irange(min(rng), Pyricope._get_last_verse(self.book, min_chapter)) for chapter in range(min_chapter + 1, max_chapter): chapter_ranges.append( irange( Pyricope._get_first_verse(self.book, chapter), Pyricope._get_last_verse(self.book, chapter) ) ) chapter_ranges.append( irange( Pyricope._get_first_verse(self.book, max_chapter), max(rng) ) ) return flatten(chapter_ranges)
def _parse_ranges(cls, book, ranges): """ """ # e.g. when 12:1-8, remember that 12 is the chapter when we get to # the 8 recent_chapter = None if not cls.does_book_has_chapters(book): recent_chapter = 1 parsed_ranges = [] for rng in ranges: # parse the low and high ends of the range separately rng = rng.split('-') # this is to treat "12:4" as "12:4 - 12:4" if len(rng) < 2: rng.append(rng[0]) lower_chapter_verse = [int(r) for r in rng[0].split(':')] upper_chapter_verse = [int(r) for r in rng[1].split(':')] # treat Mark 3-1 as Mark 3-3 and, eventually, Mark 3:1-35 if (len(lower_chapter_verse) == 1 and len(upper_chapter_verse) == 1 and upper_chapter_verse[0] < lower_chapter_verse[0]): upper_chapter_verse = lower_chapter_verse[:] chapter_range = False if len(lower_chapter_verse) < 2: if recent_chapter: # e.g. parsing 11 in 12:1-8,11 => remember that 12 is the chapter lower_chapter_verse.insert(0, recent_chapter) else: # No previous chapter lower_chapter_verse[0] = Pyricope._to_valid_chapter(book, lower_chapter_verse[0]) lower_chapter_verse.append(1) chapter_range = True else: lower_chapter_verse[0] = Pyricope._to_valid_chapter(book, lower_chapter_verse[0]) lower_chapter_verse[1] = Pyricope._to_valid_verse(book, *lower_chapter_verse) if len(upper_chapter_verse) < 2: if chapter_range: upper_chapter_verse[0] = Pyricope._to_valid_chapter(book, upper_chapter_verse[0]) upper_chapter_verse.append(Pyricope.get_max_verse(book, upper_chapter_verse[0])) else: upper_chapter_verse.insert(0, lower_chapter_verse[0]) else: upper_chapter_verse[0] = Pyricope._to_valid_chapter(book, upper_chapter_verse[0]) upper_chapter_verse[1] = Pyricope._to_valid_verse(book, *upper_chapter_verse) recent_chapter = upper_chapter_verse[0] parsed_ranges.append(irange( Pyricope._get_id(book, *lower_chapter_verse), Pyricope._get_id(book, *upper_chapter_verse) )) return parsed_ranges
def _group_array_into_ranges(cls, ary): if not ary: return [] ary = sorted(compact(flatten(ary))) ranges = [] range_start = ary.pop(0) range_end = range_start while True: if not ary: break next_value = ary.pop(0) if (next_value == cls._get_next_verse(range_end) or next_value == cls._get_start_of_next_chapter(range_end)): range_end = next_value else: ranges.append(irange(range_start, range_end)) range_start = range_end = next_value ranges.append(irange(range_start, range_end)) return ranges
def _to_valid_verse(cls, book, chapter, verse): return cls._coerce_to_range(verse, irange(1, cls.get_max_verse(book, chapter)))
def _to_valid_chapter(cls, book, chapter): return cls._coerce_to_range(chapter, irange(1, cls.get_max_chapter(book)))
def _to_valid_book(cls, book): return cls._coerce_to_range(book, irange(1, 66))