m gEc@sdZdZdZdZdZdkZdefdYZdefd YZd efd YZ d efd YZ d+d,d-d.d/d0d1d2d3d4d5d6d7f Z d(Z ei d)eiZd*ZdS(8s"Convert to and from Roman numeralss&Mark Pilgrim (f8dy@diveintopython.org)s1.4s 8 August 2001sgCopyright (c) 2001 Mark Pilgrim This program is part of "Dive Into Python", a free Python tutorial for experienced programmers. Visit http://diveintopython.org/ for the latest version. This program is free software; you can redistribute it and/or modify it under the terms of the Python 2.1.1 license, available at http://www.python.org/2.1.1/license.html Nt RomanErrorcBstZRS(N(t__name__t __module__(((t9/data/zmath/zope/lib/python/docutils/parsers/rst/roman.pyRstOutOfRangeErrorcBstZRS(N(RR(((RRstNotIntegerErrorcBstZRS(N(RR(((RRstInvalidRomanNumeralErrorcBstZRS(N(RR(((RRstMitCMitDitCDitCidtXCiZtLi2tXLi(tXi tIXi tVitIVitIicCsd|jo djnp tdnt||jo tdnd}x=tD]5\}}x&||jo||7}||8}qgWqXW|S(s convert integer to Roman numeraliis%number out of range (must be 1..4999)sdecimals can not be convertedtN(tnRtintRtresulttromanNumeralMaptnumeraltinteger(RRRR((RttoRoman(s     s8 ^ # beginning of string M{0,4} # thousands - 0 to 4 M's (CM|CD|D?C{0,3}) # hundreds - 900 (CM), 400 (CD), 0-300 (0 to 3 C's), # or 500-800 (D, followed by 0 to 3 C's) (XC|XL|L?X{0,3}) # tens - 90 (XC), 40 (XL), 0-30 (0 to 3 X's), # or 50-80 (L, followed by 0 to 3 X's) (IX|IV|V?I{0,3}) # ones - 9 (IX), 4 (IV), 0-3 (0 to 3 I's), # or 5-8 (V, followed by 0 to 3 I's) $ # end of string cCs|p tdnti|ptd|nd}d}xTtD]L\}}x=|||t |!|jo||7}|t |7}qWWqHW|S(s convert Roman numeral to integersInput can not be blanksInvalid Roman numeral: %siN( tsRtromanNumeralPatterntsearchRtindexRRRtlen(RRRRR((Rt fromRomanCs   (Ri(Ri(R i(R i(R id(R iZ(R i2(Ri((Ri (Ri (Ri(Ri(Ri(t__doc__t __author__t __version__t__date__t __copyright__tret ExceptionRRRRRRtcompiletVERBOSERR!( R&RRRRRR#R%R'RRR$R!((Rt?s -