Skip to main content

Convert between integers and Roman numerals

Project description

PyPi Version PyPi License Python Versions Build Status Code Coverage

Ancient

Convert between integers and roman numerals in Python

Install

Install from PyPi

$ pip install cnnclustering

or clone the developement version from GitHub

$ git clone https://github.com/janjoswig/Ancient.git
$ cd Ancient
$ pip install .

Usage

Import

from ancient import roman

Basic conversions

Convert integer values to Roman numerals

for i in range(10):
    print(roman.roman(i))
N
I
II
III
IV
V
VI
VII
VIII
IX

By default, the conversion follows the standard scheme using a subtractive representation for the values 4, 9, 14, etc. (e.g. IV instead of IIII). An additive representation can be selected via the mapping keyword (see also Custom Mappings).

for i in range(10):
    print(roman.roman(i, mapping="ascii-additive"))
N
I
II
III
IIII
V
VI
VII
VIII
VIIII

Composition of large numbers (>4999) can be improved using an extended mapping.

for i in [5000, 10000, 50000, 100000]:
    print(roman.roman(i, mapping="unicode-extended"))
ↁ
ↂ
ↇ
ↈ

Interpretation of Roman numerals

for i in ["I", "IV", "IIII", "XX", "XL", "C"]:
    print(roman.interpret_roman(i))
1
4
4
20
40
100

The Roman data type

The packag provides the Roman data type to handle Roman numerals

number = roman.Roman(5)

print(f"{number!r}")
print(f"{number!s}")
Roman(5, format='ascii-std')
V

The type behaves like an integer in arithmetic operations

print(number + 2)
print(number - roman.Roman(1))
print(number * 2)
print(number / 2)  # Integer division!
VII
IV
X
II

Custom Mappings

A mapping of Roman symbols to integer values used for interconversions has the form

mapping = {
    "M": 1000,
    "D": 500,
    "C": 100,
    "L": 50,
    }

For the conversion of integers to Roman numerals, such a mapping should have a decreasing order in the integer values. To ensure this, mappings can inherit from roman.Symbols. Note, that only one symbol is effectively used if the same value is mapped to more than one symbols.

custom_mapping = roman.Symbols()
custom_mapping.update({"ↆ": 50, "Ж": 100, "I": 1, "Ʌ": 5})
print(custom_mapping)
{'Ж': 100, 'ↆ': 50, 'Ʌ': 5, 'I': 1}

A cutsom mapping can be used in conversions instead of the default mappings

roman.roman(156, mapping=custom_mapping)
'ЖↆɅI'

A set of mappings is provided as instances of f roman.Symbols in roman.symbols

print(roman.symbols.keys())
dict_keys(['ascii-additive', 'ascii-std', 'ascii-variant', 'unicode-additive', 'unicode-std', 'unicode-extended', 'unicode-extended-claudian'])

Mappings stored in this place can be used by their key in conversions. Instances of type Roman have an attribute format that controls the conversion and should be a valid mapping key.

number = roman.Roman(100)
print(number)

roman.symbols["custom"] = custom_mapping
number.format = "custom"
print(number)
C
Ж

Zero and negative numbers

The package can handle negative numbers

number = roman.Roman(-10)
print(number)
-X

The symbol used to represent 0 is stored on the used mappings and can be changed.

print(roman.symbols["unicode-std"].nullum)
N

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

ancient-1.0.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

ancient-1.0.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ancient-1.0.0.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.5

File hashes

Hashes for ancient-1.0.0.tar.gz
Algorithm Hash digest
SHA256 2c7b8a86a578f6ceb135e741605a543aa4cf8d8551c54b0cfcd3749345e2d0d4
MD5 ba1f66001af812a33cb3b7f4def5798f
BLAKE2b-256 a4659178231ea05d02144bf2cafdbc636bdeb9272982d1271e1126e5750f7449

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ancient-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.5

File hashes

Hashes for ancient-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 34e6f5b36b8a6a0866ec10b620e45361e175229b34de2586cd771ee6b0e659fe
MD5 536e8ad0df663366d3d0e69ca87d139f
BLAKE2b-256 149717bc1e75915f67c6203b1d40d263bb42042188d2877dd4f3ead3e55fcba7

See more details on using hashes here.

Supported by

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