def __init__(self, *args): """Create a new Verse object - accepts several different inputs: Examples: book = 46 chapter = 2 verse = 1 Verse(book, chapter, verse) hash = 46002001 Verse(hash) normalized_string = '46-2-1' Verse(normalized_string) unformatted_string = '1 Cor 12:1' unformatted_string = '1cor12:1' unformatted_string = '1c 12:1' Verse(unformatted_string)""" # Initialize all properties first self.book = 0 self.chapter = 0 self.verse = 0 self.translation = None # If we received one integer, convert to string and parse if len(args) == 1 and (isinstance(args[0], long) or isinstance(args[0], int)): try: strval = str(args[0]) # If strval length is 7, we know the leading 0 got chopped off during conversion if len(strval) == 7: strval = '0{}'.format(strval) # Parse (e.g. 01001033) self.book = int(strval[:2]) self.chapter = int(strval[2:5]) self.verse = int(strval[5:]) self.translation = None except: raise RangeError("We can't parse the number passed in: {}".format(args[0])) # if we got 3 or 4 values, let's assume they are book, chapter, verse, translation) elif len(args) >= 3: self.book = args[0] self.chapter = args[1] self.verse = args[2] if len(args) == 4: self.translation = args[3] # if we only got one value, lets try to figure it out elif len(args) == 1 and len(args[0]) > 0: # maybe we got a normalized b-c-v(-t) string try: # check to make sure we have a valid verse string if not verse_re.search(args[0]): raise Exception('String should be in normalized b-c-v(-t) format.') # extract the parts from the string parts = args[0].split('-') self.book, self.chapter, self.verse = map(int, parts[:3]) if len(parts) > 3: self.translation = parts[3] # if not, let's try to extract the values except: # find the book reference try: b = book_re.search(args[0]).group(0) except: raise RangeError("We can't find that book of the Bible: %s" % (args[0])) # find the chapter:verse reference try: ref = ref_re.search(args[0]).group(0) except: raise Exception("We can't make sense of your chapter:verse reference") # find the translation, if provided try: self.translation = translation_re.search(args[0]).group(0).upper() except: pass # try to find the book listed as a book name or abbreviation self.bible = data.bible_data(self.translation) b = b.rstrip('.').lower().strip() for i, book in enumerate(self.bible): if book['name'].lower() == b: found = i + 1 break else: for abbr in book['abbrs']: if abbr == b: found = i + 1 break try: self.book = found except: raise RangeError("We can't find that book of the Bible!: " + b) # extract chapter and verse from ref self.chapter, self.verse = map(int, ref.split(':')) # if we didn't add the bible attribute above, add it now if 'bible' not in self.__dict__: self.bible = data.bible_data(self.translation) # check to see if the chapter is in range for the given book try: verse_count = self.bible[self.book - 1]['verse_counts'][self.chapter - 1] except: raise RangeError("There are not that many chapters in" + self.bible[self.book - 1]['name']) # check to see if the verse is in range for the given chapter if verse_count < self.verse: raise RangeError("There is no verse %s in %s %s" % (self.verse, self.bible[self.book - 1]['name'], self.chapter)) # check to see if the specified verse is omitted try: omitted = self.verse in self.bible[self.book - 1]['omissions'][self.chapter-1] try: err = 'This verse is omitted from the %s translation.' % self.translation except: err = 'This verse is omitted from all modern translations.' except: omitted = False if omitted: raise RangeError(err)
import data import json translation = 'esv' versecountsdict = {} abbrsdict = {} thedata = data.bible_data(translation=translation) for book in thedata: bookname = book['name'] abbrs = book['abbrs'] #printtoscreen; for debug only #print(bookname) #print(book['abbrs']) #print(book['verse_counts']) #verse_counts to dict. versecountsdict[bookname] = book['verse_counts'] #abbr to dict. for abbr in abbrs: abbrsdict[abbr] = bookname #addtojsonfile with open(translation + 'verse_counts.json', 'w') as outfile: json.dump(versecountsdict, outfile) with open(translation + 'abrs.json', 'w') as outfile: json.dump(abbrsdict, outfile)
def __init__(self, *args): """Create a new Verse object - accepts several different inputs: Examples: book = 46 chapter = 2 verse = 1 Verse(book, chapter, verse) normalized_string = '46-2-1' Verse(normalized_string) unformatted_string = '1 Cor 12:1' unformatted_string = '1cor12:1' unformatted_string = '1c 12:1' Verse(unformatted_string)""" # if we got 3 or 4 values, let's assume they are book, chapter, verse, translation) if len(args) >= 3: self.book = args[0] self.chapter = args[1] self.verse = args[2] if len(args) == 4: self.translation = args[3] else: self.translation = None # if we only got one value, lets try to figure it out elif len(args) == 1: # maybe we got a normalized b-c-v(-t) string try: # check to make sure we have a valid verse string if not verse_re.search(args[0]): raise Exception("String should be in normalized b-c-v(-t) format.") # extract the parts from the string parts = args[0].split("-") self.book, self.chapter, self.verse = map(int, parts[:3]) if len(parts) > 3: self.translation = parts[3] else: self.translation = None # if not, let's try to extract the values except: # find the book reference try: b = book_re.search(args[0]).group(0) except: raise RangeError("We can't find that book of the Bible: %s" % (args[0])) # find the translation, if provided try: self.translation = translation_re.search(args[0]).group(0).upper() except: self.translation = None # try to find the book listed as a book name or abbreviation self.bible = data.bible_data(self.translation) b = b.rstrip(".").lower().strip() for i, book in enumerate(self.bible): if book["name"].lower() == b: found = i + 1 break else: for abbr in book["abbrs"]: if abbr == b: found = i + 1 break try: self.book = found except: raise RangeError("We can't find that book of the Bible!: " + b) if len(self.bible[self.book - 1]["verse_counts"]) == 1: # find the verse reference try: ref = simple_ref_re.search(args[0]).group(0) except: raise Exception("We can't make sense of your verse reference") self.chapter = 1 self.verse = int(ref) else: # find the chapter:verse reference try: ref = ref_re.search(args[0]).group(0) except: raise Exception("We can't make sense of your chapter:verse reference") # extract chapter and verse from ref self.chapter, self.verse = map(int, ref.split(":")) # if we didn't add the bible attribute above, add it now if "bible" not in self.__dict__: self.bible = data.bible_data(self.translation) # check to see if the chapter is in range for the given book try: verse_count = self.bible[self.book - 1]["verse_counts"][self.chapter - 1] except: raise RangeError("There are not that many chapters in" + self.bible[self.book - 1]["name"]) # check to see if the verse is in range for the given chapter if verse_count < self.verse: raise RangeError( "There is no verse %s in %s %s" % (self.verse, self.bible[self.book - 1]["name"], self.chapter) ) # check to see if the specified verse is omitted try: omitted = self.verse in self.bible[self.book - 1]["omissions"][self.chapter - 1] try: err = "This verse is omitted from the %s translation." % self.translation except: err = "This verse is omitted from all modern translations." except: omitted = False if omitted: raise RangeError(err)