Skip to content

Commit

Permalink
Write the letters conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
xianghuzhao committed Sep 29, 2019
1 parent 114f665 commit ec1c78c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
32 changes: 29 additions & 3 deletions pdf_bookmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ class InvalidLettersNumeralError(InvalidNumeralError):
'''Invalid letters numeral expression'''


class LettersOutOfRangeError(Exception):
'''The letters number is out of range'''


def roman_to_arabic(roman):
'''
Convert roman to arabic
Expand Down Expand Up @@ -111,9 +115,30 @@ def letters_to_arabic(letters):
Convert letters to arabic
'''
if not letters:
raise InvalidLettersNumeralError('No input found')
return 0

return 0
letter = letters[0]
if ord(letter) < ord('A') or ord(letter) > ord('Z'):
raise InvalidLettersNumeralError('Must be capital letter')

for digit in letters[1:]:
if digit != letter:
raise InvalidLettersNumeralError('Letters are not identical')

return len(letters)*26 - 25 + ord(letter) - ord('A')


def arabic_to_letters(arabic):
'''
Convert arabic to letters
'''
if arabic < 0:
raise LettersOutOfRangeError('Letters numeral must >= 0')

if arabic == 0:
return ''

return chr(((arabic-1) % 26) + ord('A')) * ((arabic+25) // 26)


def call(cmd, encoding=None):
Expand Down Expand Up @@ -160,7 +185,8 @@ def main():
'''
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('pdf', metavar='PDF', help='an input PDF')
parser.add_argument('--expand-level', help='the max level to be expanded')
parser.add_argument(
'--expand-level', help='the max level to be expanded')

args = parser.parse_args()

Expand Down
46 changes: 46 additions & 0 deletions test_pdf_bookmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
from pdf_bookmark import roman_to_arabic
from pdf_bookmark import arabic_to_roman

from pdf_bookmark import InvalidLettersNumeralError
from pdf_bookmark import letters_to_arabic
from pdf_bookmark import arabic_to_letters


INVALID_ROMAN = (
'',
Expand Down Expand Up @@ -81,3 +85,45 @@ def test_out_of_range_roman():
def test_arabic_to_roman():
for arabic, roman in ROMAN_PAIRS:
assert arabic_to_roman(arabic) == roman


INVALID_LETTERS = (
'0',
'0342',
'a',
'ABC',
'AAAAAA8',
'9BBBB',
'&*-+#',
'12345',
'jflaiffj',
'+=_-&^%#!$%#*&)~`,.><',
)

LETTERS_PAIRS = (
(0, ''),
(1, 'A'),
(2, 'B'),
(3, 'C'),
(8, 'H'),
(26, 'Z'),
(27, 'AA'),
(52, 'ZZ'),
(106, 'BBBBB'),
)


def test_invalid_letter():
for letters in INVALID_LETTERS:
with pytest.raises(InvalidLettersNumeralError):
letters_to_arabic(letters)


def test_letters_to_arabic():
for arabic, letters in LETTERS_PAIRS:
assert letters_to_arabic(letters) == arabic


def test_arabic_to_letters():
for arabic, letters in LETTERS_PAIRS:
assert arabic_to_letters(arabic) == letters

0 comments on commit ec1c78c

Please sign in to comment.