Back to index

python3.2  3.2.2
Classes | Functions | Variables
roman Namespace Reference

Classes

class  RomanError
class  OutOfRangeError
class  NotIntegerError
class  InvalidRomanNumeralError

Functions

def toRoman
def fromRoman

Variables

string __author__ = "Mark Pilgrim (f8dy@diveintopython.org)"
string __version__ = "1.4"
string __date__ = "8 August 2001"
string __copyright__
tuple romanNumeralMap
tuple romanNumeralPattern = re.compile(""" ^ # 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 """ ,re.VERBOSE)

Detailed Description

Convert to and from Roman numerals

Function Documentation

def roman.fromRoman (   s)
convert Roman numeral to integer

Definition at line 67 of file roman.py.

00067 
00068 def fromRoman(s):
00069     """convert Roman numeral to integer"""
00070     if not s:
00071         raise InvalidRomanNumeralError('Input can not be blank')
00072     if not romanNumeralPattern.search(s):
00073         raise InvalidRomanNumeralError('Invalid Roman numeral: %s' % s)
00074 
00075     result = 0
00076     index = 0
00077     for numeral, integer in romanNumeralMap:
00078         while s[index:index+len(numeral)] == numeral:
00079             result += integer
00080             index += len(numeral)
00081     return result
def roman.toRoman (   n)
convert integer to Roman numeral

Definition at line 40 of file roman.py.

00040 
00041 def toRoman(n):
00042     """convert integer to Roman numeral"""
00043     if not (0 < n < 5000):
00044         raise OutOfRangeError("number out of range (must be 1..4999)")
00045     if int(n) != n:
00046         raise NotIntegerError("decimals can not be converted")
00047 
00048     result = ""
00049     for numeral, integer in romanNumeralMap:
00050         while n >= integer:
00051             result += numeral
00052             n -= integer
00053     return result
00054 
#Define pattern to detect valid Roman numerals

Variable Documentation

string roman.__author__ = "Mark Pilgrim (f8dy@diveintopython.org)"

Definition at line 3 of file roman.py.

Initial value:
00001 """Copyright (c) 2001 Mark Pilgrim
00002 
00003 This program is part of "Dive Into Python", a free Python tutorial for
00004 experienced programmers.  Visit http://diveintopython.org/ for the
00005 latest version.
00006 
00007 This program is free software; you can redistribute it and/or modify
00008 it under the terms of the Python 2.1.1 license, available at
00009 http://www.python.org/2.1.1/license.html
00010 """

Definition at line 6 of file roman.py.

string roman.__date__ = "8 August 2001"

Definition at line 5 of file roman.py.

Definition at line 4 of file roman.py.

Initial value:
00001 (('M',  1000),
00002                    ('CM', 900),
00003                    ('D',  500),
00004                    ('CD', 400),
00005                    ('C',  100),
00006                    ('XC', 90),
00007                    ('L',  50),
00008                    ('XL', 40),
00009                    ('X',  10),
00010                    ('IX', 9),
00011                    ('V',  5),
00012                    ('IV', 4),
00013                    ('I',  1))

Definition at line 26 of file roman.py.

tuple roman.romanNumeralPattern = re.compile(""" ^ # 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 """ ,re.VERBOSE)

Definition at line 55 of file roman.py.