Skip to main content

A Python module for converting numbers between numeral systems (2-36) and evaluating cross-base mathematical expressions.

Project description

Numeral System Converter and Expression Evaluator

A Python module for converting numbers between numeral systems (2-36) and evaluating cross-base mathematical expressions.
Key Features:

  • Integer/fractional conversions
  • Mixed-base calculations (+, -, *, /, ^, parentheses)
  • Minimum base detection
  • Custom error handling

Quick Start

1. Convert Numbers

from BaseKit import rebase

# Integer conversions
print(rebase("1011_2", to_base=10))  # "11_10" (binary -> decimal)
print(rebase("FF_16", to_base=2))  # "11111111_2" (hex -> binary)

# Fractional conversions
print(rebase("3.14_10", to_base=16))  # "3.23D7_16" (rounded to 4 digits)
print(rebase("A.8_16", to_base=10))  # "10.5_10" (hex -> decimal)

# Custom precision
print(rebase("0.1_2", to_base=10, rounding=6))  # "0.5_10"

2. Evaluate Cross-Base Expressions

from BaseKit import eval2dec, rebase

# Mixed-base arithmetic
print(eval2dec("10_2 + A_16"))  # "12_10" (2 + 10 = 12)
print(eval2dec("FF_16 - 10_10"))  # "245_10" (255 - 10 = 245)

# Advanced operations
print(eval2dec("(10_8 + 20_16) / 2_10"))  # "20.0_10" (8 + 32 = 40; 40 / 2 = 20)
print(eval2dec("2_10 ^ 4_10"))  # "16_10" (2вЃґ = 16)

print(rebase("f_30*10010_2", to_base=5))  # "2040_5" 30 + 33 = 270

3. Find Minimum Valid Base

from BaseKit import minBase

print(minBase("Z"))  # 36 (max symbol in base-36)
print(minBase("1A"))  # 11 (symbol 'A' requires base ≥11)

Error Handling

Common Errors and Exceptions

Error Class (Custom) Standard Exception Trigger Example Error Message
Error.DigitInvalidForBase ValueError rebase("G_16") "Invalid digit 'G' for base 16"
Error.BelongingAlphabet ValueError rebase("?@_16") "The symbol ? is not supported..."
Error.BelongingFromBase/ToBase ValueError rebase("3.14_10", to_base=37) "to_base must be in (2:36). to_base=37"
Error.NoBaseGiven SyntaxError rebase("1011") "Missing base specification"
Error.BaseIntError ValueError rebase("10_2", from_base="hex") "Invalid from_base: hex"

Example Tests from tests.py:

# tests.py
from BaseKit import rebase


def test_error_handling(self):
    with self.assertRaises(ValueError):  # Error.DigitInvalidForBase
        rebase("G_16")
    with self.assertRaises(SyntaxError):  # Error.NoBaseGiven
        rebase("1011")
    with self.assertRaises(ValueError):  # Error.BelongingToBase
        rebase("3.14_10", to_base=37)

Testing

Run unit tests to verify functionality:

python -m unittest tests.py -v

Test Coverage:
вњ”пёЏ Conversions: Integers, fractions, edge cases (e.g., 0_10 -> 0_2).
вњ”пёЏ Expressions: Mixed-base operations, parentheses, error propagation.
вњ”пёЏ Errors: Invalid digits, missing bases, out-of-range bases.


Notes

  • Precision: Fractional results are truncated to 10 decimal places.
  • Security: Uses eval() internally. Avoid untrusted input.
  • Alphabet: Default: 0-9, A-Z (case-insensitive). Extend via abc parameter.

Author: Fedor Tyulpin
Version: 1.0

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

basekit-1.0.1.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.

basekit-1.0.1-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file basekit-1.0.1.tar.gz.

File metadata

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

File hashes

Hashes for basekit-1.0.1.tar.gz
Algorithm Hash digest
SHA256 dbd301fc58ab77ef4e236fe14163bb965716774b42d2285c5375f6df13f87785
MD5 c9dbe9dfa4b60c4ba5d6277ae6494e4a
BLAKE2b-256 65af4c16749124aa699543a9f0fbae05e111a7ace5dcb367ced1c82d163e320e

See more details on using hashes here.

File details

Details for the file basekit-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: basekit-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for basekit-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c00bc1c119b8e5c0f7bc548a1412b32222a5c7145887bb52476bfe017ec103a5
MD5 f586f916398cff837119af44bcc8e8d2
BLAKE2b-256 5550195b92d57fcd9c7920d6b532548f2296d9d8c4afc27128720a0497e1433b

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