Skip to main content

A collection of numeral system converters.

Project description

Swopy

A Python library for converting between different numeral systems.

Overview

Swopy provides a simple and extensible interface to convert numbers between various numeral systems. Swopy supports bidirectional conversion — you can convert from any supported system to any other.

Supported Numeral Systems

  • Roman, in the forms:
    • Early, supporting integers between 1 and 899
    • Standard, supporting integers between 1 and 3,999
    • Apostrophus, supporting integers between 1 and 100,000
  • Egyptian, supporting integers between 1 and 1,000,000/many
  • Arabic, supporting integers between -int(sys.float_info.max) and int(sys.float_info.max)

Installation

Install the package:

pip install swopy # or
uv add swopy

Usage

Basic Conversion

import swopy
from swopy import systems

# Convert integer Roman numeral to Egyptian hieroglyphic
swopy.swop('IX', systems.roman.Standard, systems.egyptian.Egyptian)
# '𓏺𓏺𓏺𓏺𓏺𓏺𓏺𓏺𓏺'

# Convert Apostrophus to an Arabic integer
swopy.swop('IↃI', systems.roman.Apostrophus, systems.arabic.Arabic)
# 501

Available Systems

import swopy
import pprint
systems = swopy.get_all_systems()
pprint.pprint(systems)
#{'arabic.Arabic': <class 'swopy.systems.arabic.Arabic'>,
# 'egyptian.Egyptian': <class 'swopy.systems.egyptian.Egyptian'>,
# 'roman.Apostrophus': <class 'swopy.systems.roman.Apostrophus'>,
# 'roman.Early': <class 'swopy.systems.roman.Early'>,
# 'roman.Standard': <class 'swopy.systems.roman.Standard'>}

swopy.swop(42, systems['arabic.Arabic'], systems['roman.Early'])
# 'XLII'

Error Handling

Swopy will raise a ValueError if there is a number is not representable in a numeral system

import swopy
from swopy import systems

swopy.swop(4000, systems.arabic.Arabic, systems.roman.Standard)
# ValueError: Number must be less than or equal to 3999.

systems.roman.Early.to_numeral(900)
# ValueError: Number must be less than or equal to 899.

or if a numeral is invalid

import swopy
from swopy import systems

swopy.swop('IIIII', systems.egyptian.Egyptian, systems.roman.Early)
# ValueError: Invalid Egyptian hieroglyph: I

systems.roman.Apostrophus.from_numeral('P')
# ValueError: Invalid Apostrophus characters at position 0

and will raise a TypeError if a numeral is not representable in a system.

import swopy
from swopy import systems

swopy.swop('I', systems.arabic.Arabic, systems.roman.Early)
# TypeError: 1.2 of type <class 'float'> cannot be represented in Early.

swopy.swop('I', systems.arabic.Arabic, systems.roman.Early)
# TypeError: I of type <class 'str'> cannot be represented in Arabic.

systems.egyptian.Egyptian.to_numeral(1.2)
# TypeError: 1.2 of type <class 'float'> cannot be represented in Egyptian.

Requirements

Swopy only relies on the standard library and needs Python 3.13 or higher. Swopy is currently tested on the latest versions of Windows, MacOS and Ubuntu for Python versions 3.13, 3.14.0, 3.14.3, and 3.15.0a5.

Development

Dependencies

Development dependencies are managed through pyproject.toml:

# Install development dependencies
sh scripts/startup.sh

Dev tools include:

  • pytest / pytest-cov: Testing framework
  • ruff: Fast Python linter and formatter
  • pyright: Static type checker
  • deptry: Dependency validation
  • hypothesis: Property-based testing
  • pre-commit: Git hooks framework
  • tox, with tox-uv: Test runner
  • uv

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

swopy-0.1.2.tar.gz (30.2 kB view details)

Uploaded Source

Built Distribution

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

swopy-0.1.2-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file swopy-0.1.2.tar.gz.

File metadata

  • Download URL: swopy-0.1.2.tar.gz
  • Upload date:
  • Size: 30.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for swopy-0.1.2.tar.gz
Algorithm Hash digest
SHA256 600584d8435550cb8b665022da0a2389f65200028df3c801b7feb32f898d5f44
MD5 3df8b44538576a5e1768635943f00b3a
BLAKE2b-256 16d4728b9d0bbbdd5c2a44f0821421faeb85769ca6c734d2ff526642587c7cb8

See more details on using hashes here.

File details

Details for the file swopy-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: swopy-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for swopy-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1f98b31ea7fdb3fed9ba086873bbde9599ecbc40add98bbd966df7c3537698fc
MD5 16bf62a6b6400d390d1e1cc91584e478
BLAKE2b-256 f4a8ec7d629af73d023c0431a9014e07fc771d453cb19d1e943482d87ff43a1e

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