Skip to main content

Roman Number demonstration class.

This demonstration program defines and tests a programmer created object class. romanclass.py [1]

A “Roman” object is stored in the computer as a binary integer, but is displayed in Roman numerals. (In technical terms, it is a subset of the built-in class int with a _str_() method which converts to a Roman numeral string.)

Roman objects act very much like the built-in Decimal objects in Python, they can be added, subtracted, multiplied, or divided and the result will be another object of the same class. So a programmer can say:

>>> from romanclass import Roman
>>> two = Roman(2)
>>> five = Roman('V')
>>> print(two+five)
VII

The permitted range of a Roman object is [0 to 699_999]. [2]

Unicode Roman Symbols

If you try to create a Roman number larger than 3999, you may have difficulty displaying the result, because Roman digits for 5000 and larger cannot be represented in ASCII and must be output in Unicode characters. Fonts which define all of the Roman numerals (especially 50,000 and 100,000) are uncommon. [The “Code2000” font does include them.]

If you attempt to ‘print(Roman(‘MMMCMXCIX’) + 1)’, it will emit the unicode for: Mↁ which is how you write 4000 (5000 – 1000).

Other Unicode numeric characters exist, and you may send any numeric-valued character (from any language) to Roman(), and it will use the value defined by the Unicode standard, adding all the values together, subject to the Roman smaller-values-to-the-left rule, so 1V converts to 4, not 6. While 9V would convert to 14.

Installation:

python3 -m pip install romanclass

This module has been tested using CPython versions 3.10 and 3.12, and IronPython version 3.4. If you run test/test_romanclass.py, it will run and print a self-check.

Usage:

A call to Roman() will create an new instance of a Roman object. The argument may be either a string of Roman Digits, or any input (string or numeric) which is accepted by the int() built in function. In other words, the following calls are equivalent:

Roman('MCMLXVI')
Roman('mdccccxxxxxxvj')
Roman('1966')
Roman(1966)
Roman(1966.4)

Notice that the input routine for Roman numerals makes no requirement that the number be normalized according to modern standards. It will attempt to render any jumbled combination of suitable characters.

However, some ancient sources use ‘IIXX’ for 22 and others use XIIX for 18. Such things will not work correctly.

Class methods:

fromRoman() Accepts a string of Latin numeric characters [IVXLDCM] and/or Unicode characters which have a numeric value (that is, where the value of unicodedata.numeric() is defined) and attempts to parse them using a simple rule for the formation of Roman numbers.

  • A blank input, or the strings ‘N’ or ‘Nulla’, will be stored as Zero.

  • Returns an integer number.

  • Values less than zero or greater than 699,999 will throw an OutOfRangeError exception.

  • Invalid characters in the input stream will give an InvalidRomanNumeralError exception.

    • Both are subtypes of ValueError.

ToRoman() Accepts an integer and formats a Unicode string as a normalized Roman number. The usual ASCII characters will be used for values in the range 1 … 3999.

  • Zero will be rendered as ‘Nulla’ which is the Latin word for ‘nothing’.

  • If the value is greater than 3999, Unicode characters will be used for the high order digits.

  • Values less than zero or greater than 699,999 will give an OutOfRangeError exception.

toUnicodeRoman() Accepts an integer and formats a Unicode string as a normalized Roman number.

  • Unicode characters will be used for all digits.

  • If the value is between 1 and 12 a single Unicode character with the appropriate value will be returned. This feature could be used, for example, to generate label values for a clock face.

  • Values outside that range will be rendered as strings of one or more Unicode characters in the usual manner.

  • Values less than zero or greater than 699,999 will give an OutOfRangeError exception.

Mathematical Operations:

Addition, subtraction, multiplication and floor division are defined for the Class.

Caution: The result will be the type of the left-hand operand. Thus:

>>> two = Roman(2)
>>> print(two + 2)
IV
>>> print(2 + two)
4

Updates and maintenance:

Source code is complete in the “sdist” tarball.

python -m pip download –no-binary :all: romanclass –no-build-isolation

The version control repository is at https://launchpad.net/romanclass and has been converted to a git repository as of __version__ = 1.2.1

The original work was done using the bazaar VCS. The bazaar repo is now obsolete.

CaeserCalc.py sample application

The program “CaesarCalc.py” is a simple console mode calculator which accepts a typed line with a numbers (Roman or Arabic) or two such numbers separated by ‘+’ ‘-’ ‘*’ or ‘/’. It will calculate and print the result.

It can be found in the romanclass/test subdirectory of a source distribution.

.

Release files for romanclass 1.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for romanclass 1.2.3
File Size Uploaded
romanclass-1.2.3.tar.gz 10.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for romanclass 1.2.3
File Interpreter ABI Platform
romanclass-1.2.3-py3-none-any.whl Python 3 none any Details

Total release size: 18.4 kB

Release files / romanclass-1.2.3.tar.gz

Download URL romanclass-1.2.3.tar.gz
Size 10.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a5ba10d93ce136f2d9e2e1041bd4e37a37753c5440c191f3977b9845efd7790b
BLAKE2b-256 checksum
How to use checksums
0f08055bb8d77a10ef621c5b0771c1b2fb12a35082e36a682ffbc9dd8ecaae00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.3

Release files / romanclass-1.2.3-py3-none-any.whl

Download URL romanclass-1.2.3-py3-none-any.whl
Size 8.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5cb0dcf0a1e3828b72a210a781c9532e772796876c3ffc11c5544d745b6ebc30
BLAKE2b-256 checksum
How to use checksums
94b465bc56fbbeb82ade8cf69bfab3c06848f42fca86ce06fc292a9fd7381ce5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

1.2.3 This release

2 release files

1.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page