Skip to main content

Effortlessly convert decimal numbers to Roman numerals in Python

Project description

RomanPy

release pypi develop master gitlab github

RomanPy is a Python library and CLI for converting decimal numbers to Roman numerals. It provides:

  • ASCII or Unicode output (with full support for Unicode numeral symbols)
  • Upper- and lowercase variants
  • Configurable numeral variants (subtractive, extended, apostrophus, medieval, zero, or custom mappings)
  • Arithmetic operations (+, -, *, //) between Roman numerals and integers
  • Equality and comparison with ints and strings

Licensed under the AGPLv3.0.

Installation

RomanPy is available on PyPI and can be installed using pip:

pip install RomanPy

Alternatively, you can pull the repository and install it straight from the source:

git clone git@forgejo.parcifal.dev:parcifal/roman-py.git
cd roman-py
pip install .

Usage (API)

Basic Conversion

from roman import roman

print(roman(207))  # CCVII

Encoding and Case Variants

n = roman(1776)

print(n.encode("ascii").upper())   # MDCCLXXVI
print(n.encode("ascii").lower())   # mdcclxxvi
print(n.encode("unicode").upper()) # ⅯⅮⅭⅭⅬⅩⅩⅥ
print(n.encode("unicode").lower()) # ⅿⅾⅽⅽⅼⅹⅹⅵ

Arithmetic with Roman Numerals

Roman numerals can be added, subtracted, multiplied, or divided with
other Roman numerals or integers:

print(roman(100) + roman(60))   # CLX
print(roman(45) - roman(7))     # XXXVIII
print(roman(533) * 2)           # MLXVI
print(roman(2460) // 10)        # CCXLVI

# mix with ints
print(100 + roman(60))          # CLX
print(roman(1954) == "MCMLIV")  # True

Custom Variants

You can extend or override numeral variants:

from roman.roman import VARIANT_ZERO

n = roman(0).extend_variant(VARIANT_ZERO)
print(n)  # N

Built-in variants include:

  • VARIANT_BASE (I, V, X, …)
  • VARIANT_SUBTRACTIVE (IV, IX, XL, …)
  • VARIANT_SUBTRACTIVE_EXTENDED (IIX, IC, …)
  • VARIANT_ZERO (N)
  • VARIANT_APOSTROPHUS (large numbers with I) and ((I)))
  • VARIANT_MEDIEVAL (A, Z, O, …)

By default, VARIANT_SUBTRACTIVE is used.

Usage (CLI)

Installing RomanPy also provides a roman command-line tool:

roman 42
# = XLII

Options

usage: roman [-h] [-a | -A | -u | -U] [-b | -s | -e] [-z] [-p] [-m] [-c DECIMAL NUMERAL] [-v] [-V] value

Convert a decimal number to roman numeral.

positional arguments:
  value                 a decimal number to convert to a roman numeral

optional arguments:
  -h, --help            show this help message and exit
  -a, --ascii           output encoding of roman numerals in lowercase ascii
  -A, --ASCII           output encoding of roman numerals in uppercase ascii (default)
  -u, --unicode         output encoding of roman numerals in lowercase unicode
  -U, --UNICODE         output encoding of roman numerals in uppercase unicode
  -b, --no-base         do not use the base variant
  -s, --subtractive     use the subtractive variant (includes base) (default)
  -e, --subtractive-extended
                        use the extended subtractive variant(includes subtractive)
  -z, --zero            use the zero variant N
  -p, --apostrophus     use the apostrophus method for large numbers
  -m, --medieval        use the medieval variant
  -c DECIMAL NUMERAL, --custom DECIMAL NUMERAL
                        map a decimal number to a roman numeral
  -v, --verbose         increase verbosity
  -V, --version         show program's version number and exit

Contributing

Found a bug? Have a suggestion? Open an issue or submit a merge request at the Forgejo repository. All contributions are welcome.

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

romanpy-1.3.5.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

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

romanpy-1.3.5-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file romanpy-1.3.5.tar.gz.

File metadata

  • Download URL: romanpy-1.3.5.tar.gz
  • Upload date:
  • Size: 19.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.2

File hashes

Hashes for romanpy-1.3.5.tar.gz
Algorithm Hash digest
SHA256 5492bd2e4f81661fc579c7863a47549b3bf902d675737b53c6e20cad726a8b52
MD5 5b11231d3ea71a4b3fb4f408baeb7652
BLAKE2b-256 b76c907983369bf61766f704839b21bc579bb6f82897e5c142caffee77086226

See more details on using hashes here.

File details

Details for the file romanpy-1.3.5-py3-none-any.whl.

File metadata

  • Download URL: romanpy-1.3.5-py3-none-any.whl
  • Upload date:
  • Size: 18.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.2

File hashes

Hashes for romanpy-1.3.5-py3-none-any.whl
Algorithm Hash digest
SHA256 b4019184e5ae9612b95b24ad6b07a4d026e8f13a71576ed2ae9cd6ac523fc7c4
MD5 51376c9f3c65357ec6b954df2bd1b71f
BLAKE2b-256 87b0e5a2f9749c88711017c0366f2b1cec246d1a4a5a5387d82cbce239da34b8

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