1 # Functions to convert between integers and Roman numerals.
3 # http://code.activestate.com/recipes/81611-roman-numerals/
4 # PSFL (GPL compatible)
8 def int_to_roman(input):
10 Convert an integer to Roman numerals.
14 Traceback (most recent call last):
15 ValueError: Argument must be between 1 and 3999
18 Traceback (most recent call last):
19 ValueError: Argument must be between 1 and 3999
21 >>> int_to_roman(1.5) # doctest: +IGNORE_EXCEPTION_DETAIL
22 Traceback (most recent call last):
23 TypeError: expected integer, got <type 'float'>
25 >>> for i in range(1, 21): print(int_to_roman(i))
47 >>> print(int_to_roman(2000))
49 >>> print(int_to_roman(1999))
52 if not isinstance(input, int):
53 raise TypeError("expected integer, got %s" % type(input))
54 if not 0 < input < 4000:
55 raise ValueError("Argument must be between 1 and 3999")
56 ints = (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1)
57 nums = ('M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV',
60 for i in range(len(ints)):
61 count = int(input / ints[i])
62 result += nums[i] * count
63 input -= ints[i] * count
67 def roman_to_int(input):
69 Convert a roman numeral to an integer.
71 >>> r = list(range(1, 4000))
72 >>> nums = [int_to_roman(i) for i in r]
73 >>> ints = [roman_to_int(n) for n in nums]
77 >>> roman_to_int('VVVIV')
78 Traceback (most recent call last):
80 ValueError: input is not a valid roman numeral: VVVIV
81 >>> roman_to_int(1) # doctest: +IGNORE_EXCEPTION_DETAIL
82 Traceback (most recent call last):
84 TypeError: expected string, got <type 'int'>
86 Traceback (most recent call last):
88 ValueError: input is not a valid roman numeral: A
89 >>> roman_to_int('IL')
90 Traceback (most recent call last):
92 ValueError: input is not a valid roman numeral: IL
94 if not isinstance(input, str):
95 raise TypeError("expected string, got %s" % type(input))
97 nums = ['M', 'D', 'C', 'L', 'X', 'V', 'I']
98 ints = [1000, 500, 100, 50, 10, 5, 1]
102 raise ValueError("input is not a valid roman numeral: %s" % input)
103 for i in range(len(input)):
105 value = ints[nums.index(c)]
106 # If the next place holds a larger number, this value is negative.
108 nextvalue = ints[nums.index(input[i + 1])]
109 if nextvalue > value:
112 # there is no next place.
118 # Easiest test for validity...
119 if int_to_roman(sum) == input:
122 raise ValueError('input is not a valid roman numeral: %s' % input)
126 if not os.path.isdir(path):
130 def get_translation(language):
132 from .functions import lang_code_3to2
134 return gettext.translation(
136 localedir=os.path.join(os.path.dirname(__file__), 'locale'),
137 languages=[lang_code_3to2(language), 'pl'],