Skip to main content

Manipulate well-formed Roman numerals

Project description

A library for manipulating well-formed Roman numerals.

Integers between 1 and 3,999 (inclusive) are supported. Numbers beyond this range will return an OutOfRangeError.

The classical system of roman numerals requires that the same character may not appear more than thrice consecutively, meaning that ‘MMMCMXCIX’ (3,999) is the largest well-formed Roman numeral. The smallest is ‘I’ (1), as there is no symbol for zero in Roman numerals.

Both upper- and lower-case formatting of roman numerals are supported, and likewise for parsing strings, although the entire string must be of the same case. Numerals that do not adhere to the classical form are rejected with an InvalidRomanNumeralError.

Example usage

Creating a roman numeral

from roman_numerals import RomanNumeral

num = RomanNumeral(16)
assert str(num) == 'XVI'

num = RomanNumeral.from_string("XVI")
assert int(num) == 16

Convert a roman numeral to a string

from roman_numerals import RomanNumeral

num = RomanNumeral(16)
assert str(num) == 'XVI'
assert num.to_uppercase() == 'XVI'
assert num.to_lowercase() == 'xvi'
assert repr(num) == 'RomanNumeral(16)'

Extract the decimal value of a roman numeral

from roman_numerals import RomanNumeral

num = RomanNumeral(42)
assert int(num) == 42

Invalid input

from roman_numerals import RomanNumeral, InvalidRomanNumeralError

num = RomanNumeral.from_string("Spam!")  # raises InvalidRomanNumeralError
num = RomanNumeral.from_string("CLL")  # raises InvalidRomanNumeralError
num = RomanNumeral(0)  # raises OutOfRangeError
num = RomanNumeral(4_000)  # raises OutOfRangeError

Licence

This project is placed in the public domain or under the terms of the ‘Zero Clause BSD licence’, whichever is more permissive.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

roman_numerals-1.0.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

roman_numerals-1.0.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file roman_numerals-1.0.0.tar.gz.

File metadata

  • Download URL: roman_numerals-1.0.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for roman_numerals-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0b1b1643a1286588a4182bdaf2f2af4056835c1e0cdf147d3ad937af0ee0714c
MD5 5e8515b43307cf987f07a0de82e55ba6
BLAKE2b-256 b72770e58191a9355b9f2d4644c242c3345d95fa6b1d7ef9510638aa8f260740

See more details on using hashes here.

File details

Details for the file roman_numerals-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: roman_numerals-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for roman_numerals-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d210bdc4d82339d91297a4911ddeee3f34aa53b99090227a826db46ccfcf4407
MD5 ba4b6a837254ff95c07ff7f04c180f4c
BLAKE2b-256 331f2bd2bd5b4d3c1628e9e6c50dea779b06f6e6387facbf7f80cf001c15b535

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page