Revision: 4265 Author: wikipedian Date: 2007-09-13 09:34:39 +0000 (Thu, 13 Sep 2007)
Log Message: ----------- handle ISBN10s with small x as checksum
Modified Paths: -------------- trunk/pywikipedia/isbn.py
Modified: trunk/pywikipedia/isbn.py =================================================================== --- trunk/pywikipedia/isbn.py 2007-09-13 09:34:00 UTC (rev 4264) +++ trunk/pywikipedia/isbn.py 2007-09-13 09:34:39 UTC (rev 4265) @@ -1275,7 +1275,7 @@ """ result = [] for c in self.code: - if c.isdigit() or c == 'X': + if c.isdigit() or c in 'Xx': result.append(c) elif c != '-': raise InvalidIsbnException('The ISBN %s contains invalid characters.' % self.code) @@ -1295,13 +1295,13 @@ #print checksum lastDigit = self.digits()[-1] #print lastDigit - if not ((checksum == 10 and lastDigit == 'X') or (lastDigit.isdigit() and checksum == int(lastDigit))): + if not ((checksum == 10 and lastDigit in 'Xx') or (lastDigit.isdigit() and checksum == int(lastDigit))): raise InvalidIsbnException('The ISBN checksum of %s is incorrect.' % self.code)
def checkValidity(self): if len(self.digits()) != 10: raise InvalidIsbnException('The ISBN %s is not 10 digits long.' % self.code) - if 'X' in self.digits()[:-1]: + if 'X' in self.digits()[:-1] or 'x' in self.digits()[:-1]: raise InvalidIsbnException('ISBN %s: X is only allowed at the end of the ISBN.' % self.code) self.checkChecksum()
@@ -1313,11 +1313,18 @@ ISBN number. """ code = '978-' + self.code[:-1] - + #cs = self.calculateChecksum() #code += str(cs) return ISBN13(code, checksumMissing = True)
+ def format(self): + # load overridden superclass method + ISBN.format(self) + # capitalize checksum + if self.code[-1] == 'x': + self.code = self.code[:-1] + 'X' + def getIsbn(code): try: i = ISBN13(code) @@ -1342,7 +1349,7 @@ return i.code
def hyphenateIsbnNumbers(text): - isbnR = re.compile(r'(?<=ISBN )(?P<code>[\d-]+X?)') + isbnR = re.compile(r'(?<=ISBN )(?P<code>[\d-]+[Xx]?)') text = isbnR.sub(_hyphenateIsbnNumber, text) return text
@@ -1360,7 +1367,7 @@ return i13.code
def convertIsbn10toIsbn13(text): - isbnR = re.compile(r'(?<=ISBN )(?P<code>[\d-]+X?)') + isbnR = re.compile(r'(?<=ISBN )(?P<code>[\d-]+[Xx]?)') text = isbnR.sub(_isbn10toIsbn13, text) return text
@@ -1371,7 +1378,7 @@ self.to13 = to13 self.format = format self.always = always - self.isbnR = re.compile(r'(?<=ISBN )(?P<code>[\d-]+X?)') + self.isbnR = re.compile(r'(?<=ISBN )(?P<code>[\d-]+[Xx]?)')
def treat(self, page): try: