ยปCore Development>Code coverage>Doc/tools/roman.py

Python code coverage for Doc/tools/roman.py

#countcontent
1n/a"""Convert to and from Roman numerals"""
2n/a
3n/a__author__ = "Mark Pilgrim (f8dy@diveintopython.org)"
4n/a__version__ = "1.4"
5n/a__date__ = "8 August 2001"
6n/a__copyright__ = """Copyright (c) 2001 Mark Pilgrim
7n/a
8n/aThis program is part of "Dive Into Python", a free Python tutorial for
9n/aexperienced programmers. Visit http://diveintopython.org/ for the
10n/alatest version.
11n/a
12n/aThis program is free software; you can redistribute it and/or modify
13n/ait under the terms of the Python 2.1.1 license, available at
14n/ahttp://www.python.org/2.1.1/license.html
15n/a"""
16n/a
17n/aimport re
18n/a
19n/a#Define exceptions
20n/aclass RomanError(Exception): pass
21n/aclass OutOfRangeError(RomanError): pass
22n/aclass NotIntegerError(RomanError): pass
23n/aclass InvalidRomanNumeralError(RomanError): pass
24n/a
25n/a#Define digit mapping
26n/aromanNumeralMap = (('M', 1000),
27n/a ('CM', 900),
28n/a ('D', 500),
29n/a ('CD', 400),
30n/a ('C', 100),
31n/a ('XC', 90),
32n/a ('L', 50),
33n/a ('XL', 40),
34n/a ('X', 10),
35n/a ('IX', 9),
36n/a ('V', 5),
37n/a ('IV', 4),
38n/a ('I', 1))
39n/a
40n/adef toRoman(n):
41n/a """convert integer to Roman numeral"""
42n/a if not (0 < n < 5000):
43n/a raise OutOfRangeError("number out of range (must be 1..4999)")
44n/a if int(n) != n:
45n/a raise NotIntegerError("decimals can not be converted")
46n/a
47n/a result = ""
48n/a for numeral, integer in romanNumeralMap:
49n/a while n >= integer:
50n/a result += numeral
51n/a n -= integer
52n/a return result
53n/a
54n/a#Define pattern to detect valid Roman numerals
55n/aromanNumeralPattern = re.compile("""
56n/a ^ # beginning of string
57n/a M{0,4} # thousands - 0 to 4 M's
58n/a (CM|CD|D?C{0,3}) # hundreds - 900 (CM), 400 (CD), 0-300 (0 to 3 C's),
59n/a # or 500-800 (D, followed by 0 to 3 C's)
60n/a (XC|XL|L?X{0,3}) # tens - 90 (XC), 40 (XL), 0-30 (0 to 3 X's),
61n/a # or 50-80 (L, followed by 0 to 3 X's)
62n/a (IX|IV|V?I{0,3}) # ones - 9 (IX), 4 (IV), 0-3 (0 to 3 I's),
63n/a # or 5-8 (V, followed by 0 to 3 I's)
64n/a $ # end of string
65n/a """ ,re.VERBOSE)
66n/a
67n/adef fromRoman(s):
68n/a """convert Roman numeral to integer"""
69n/a if not s:
70n/a raise InvalidRomanNumeralError('Input can not be blank')
71n/a if not romanNumeralPattern.search(s):
72n/a raise InvalidRomanNumeralError('Invalid Roman numeral: %s' % s)
73n/a
74n/a result = 0
75n/a index = 0
76n/a for numeral, integer in romanNumeralMap:
77n/a while s[index:index+len(numeral)] == numeral:
78n/a result += integer
79n/a index += len(numeral)
80n/a return result