1 # Functions to convert between integers and Roman numerals.
3 # http://code.activestate.com/recipes/81611-roman-numerals/
4 # PSFL (GPL compatible)
5 from __future__ import print_function, unicode_literals
11 def int_to_roman(input):
13 Convert an integer to Roman numerals.
17 Traceback (most recent call last):
18 ValueError: Argument must be between 1 and 3999
21 Traceback (most recent call last):
22 ValueError: Argument must be between 1 and 3999
24 >>> int_to_roman(1.5) # doctest: +IGNORE_EXCEPTION_DETAIL
25 Traceback (most recent call last):
26 TypeError: expected integer, got <type 'float'>
28 >>> for i in range(1, 21): print(int_to_roman(i))
50 >>> print(int_to_roman(2000))
52 >>> print(int_to_roman(1999))
55 if not isinstance(input, int):
56 raise TypeError("expected integer, got %s" % type(input))
57 if not 0 < input < 4000:
58 raise ValueError("Argument must be between 1 and 3999")
59 ints = (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1)
60 nums = ('M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV',
63 for i in range(len(ints)):
64 count = int(input / ints[i])
65 result += nums[i] * count
66 input -= ints[i] * count
70 def roman_to_int(input):
72 Convert a roman numeral to an integer.
74 >>> r = list(range(1, 4000))
75 >>> nums = [int_to_roman(i) for i in r]
76 >>> ints = [roman_to_int(n) for n in nums]
80 >>> roman_to_int('VVVIV')
81 Traceback (most recent call last):
83 ValueError: input is not a valid roman numeral: VVVIV
84 >>> roman_to_int(1) # doctest: +IGNORE_EXCEPTION_DETAIL
85 Traceback (most recent call last):
87 TypeError: expected string, got <type 'int'>
89 Traceback (most recent call last):
91 ValueError: input is not a valid roman numeral: A
92 >>> roman_to_int('IL')
93 Traceback (most recent call last):
95 ValueError: input is not a valid roman numeral: IL
97 if not isinstance(input, six.text_type):
98 raise TypeError("expected string, got %s" % type(input))
100 nums = ['M', 'D', 'C', 'L', 'X', 'V', 'I']
101 ints = [1000, 500, 100, 50, 10, 5, 1]
105 raise ValueError("input is not a valid roman numeral: %s" % input)
106 for i in range(len(input)):
108 value = ints[nums.index(c)]
109 # If the next place holds a larger number, this value is negative.
111 nextvalue = ints[nums.index(input[i + 1])]
112 if nextvalue > value:
115 # there is no next place.
121 # Easiest test for validity...
122 if int_to_roman(sum) == input:
125 raise ValueError('input is not a valid roman numeral: %s' % input)
129 if not os.path.isdir(path):
133 def get_translation(language):
135 from .functions import lang_code_3to2
137 return gettext.translation(
139 localedir=os.path.join(os.path.dirname(__file__), 'locale'),
140 languages=[lang_code_3to2(language), 'pl'],