DefinetlyNotAI/AlgoPy

View on GitHub
jupyter/convert.ipynb

Summary

Maintainability
Test Coverage
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class Convert:\n",
    "    class Binary:\n",
    "        @staticmethod\n",
    "        def __check_input_type(value, expected_type) -> bool:\n",
    "            \"\"\"\n",
    "            Check if the input value is of the expected type.\n",
    "\n",
    "            :param value: The value to check.\n",
    "            :param expected_type: The expected type of the value.\n",
    "            :return: True if the value is of the expected type, otherwise raises an Exception.\n",
    "            \"\"\"\n",
    "            if not isinstance(value, expected_type):\n",
    "                raise Exception(\n",
    "                    f\"Expected {expected_type.__name__}, got {type(value).__name__}\"\n",
    "                )\n",
    "            return True\n",
    "\n",
    "        @classmethod\n",
    "        def to_hex(cls, Binary_Number: int) -> str:\n",
    "            \"\"\"\n",
    "            Convert a binary number to its hexadecimal representation.\n",
    "\n",
    "            :param Binary_Number: The binary number to convert.\n",
    "            :return: The hexadecimal representation of the binary number.\n",
    "            \"\"\"\n",
    "            if Binary_Number is None:\n",
    "                raise Exception(\"No binary number provided\")\n",
    "            cls.__check_input_type(str(Binary_Number), str)\n",
    "            return hex(int(str(Binary_Number), 2))[2:].upper()\n",
    "\n",
    "        @classmethod\n",
    "        def to_dec(cls, Binary_Number: int) -> int:\n",
    "            \"\"\"\n",
    "            Convert a binary number to its decimal representation.\n",
    "\n",
    "            :param Binary_Number: The binary number to convert.\n",
    "            :return: The decimal representation of the binary number.\n",
    "            \"\"\"\n",
    "            if Binary_Number is None:\n",
    "                raise Exception(\"No binary number provided\")\n",
    "            cls.__check_input_type(str(Binary_Number), str)\n",
    "            return int(str(Binary_Number), 2)\n",
    "\n",
    "    class Decimal:\n",
    "        @staticmethod\n",
    "        def __check_input_type(value, expected_type) -> bool:\n",
    "            \"\"\"\n",
    "            Check if the input value is of the expected type.\n",
    "\n",
    "            :param value: The value to check.\n",
    "            :param expected_type: The expected type of the value.\n",
    "            :return: True if the value is of the expected type, otherwise raises an Exception.\n",
    "            \"\"\"\n",
    "            if not isinstance(value, expected_type):\n",
    "                raise Exception(\n",
    "                    f\"Expected {expected_type.__name__}, got {type(value).__name__}\"\n",
    "                )\n",
    "            return True\n",
    "\n",
    "        @staticmethod\n",
    "        def to_roman(Number: int) -> str:\n",
    "            \"\"\"\n",
    "            Convert a decimal number to its Roman numeral representation.\n",
    "\n",
    "            :param Number: The decimal number to convert.\n",
    "            :return: The Roman numeral representation of the decimal number.\n",
    "            \"\"\"\n",
    "            mapping = {\n",
    "                1000: \"M\",\n",
    "                900: \"CM\",\n",
    "                500: \"D\",\n",
    "                400: \"CD\",\n",
    "                100: \"C\",\n",
    "                90: \"XC\",\n",
    "                50: \"L\",\n",
    "                40: \"XL\",\n",
    "                10: \"X\",\n",
    "                9: \"IX\",\n",
    "                5: \"V\",\n",
    "                4: \"IV\",\n",
    "                1: \"I\",\n",
    "            }\n",
    "            if Number is None or Number < 1:\n",
    "                raise Exception(\"Invalid input.\")\n",
    "            result = \"\"\n",
    "            for num, roman in sorted(mapping.items(), reverse=True):\n",
    "                while Number >= num:\n",
    "                    result += roman\n",
    "                    Number -= num\n",
    "            return result\n",
    "\n",
    "        @staticmethod\n",
    "        def to_ascii(Number: int | str) -> str:\n",
    "            \"\"\"\n",
    "            Convert a decimal number to its ASCII art representation.\n",
    "\n",
    "            :param Number: The decimal number to convert.\n",
    "            :return: The ASCII art representation of the decimal number.\n",
    "            \"\"\"\n",
    "            digits = [\n",
    "                [\n",
    "                    \"  ***  \",\n",
    "                    \" *   * \",\n",
    "                    \"*     *\",\n",
    "                    \"*     *\",\n",
    "                    \"*     *\",\n",
    "                    \" *   * \",\n",
    "                    \"  ***  \",\n",
    "                ],\n",
    "                [\" * \", \"** \", \" * \", \" * \", \" * \", \" * \", \"***\"],\n",
    "                [\" *** \", \"*   *\", \"*  * \", \"  *  \", \" *   \", \"*    \", \"*****\"],\n",
    "                [\" *** \", \"*   *\", \"    *\", \"  ** \", \"    *\", \"*   *\", \" *** \"],\n",
    "                [\"   *  \", \"  **  \", \" * *  \", \"*  *  \", \"******\", \"   *  \", \"   *  \"],\n",
    "                [\"*****\", \"*    \", \"*    \", \" *** \", \"    *\", \"*   *\", \" *** \"],\n",
    "                [\" *** \", \"*    \", \"*    \", \"**** \", \"*   *\", \"*   *\", \" *** \"],\n",
    "                [\"*****\", \"    *\", \"   * \", \"  *  \", \" *   \", \"*    \", \"*    \"],\n",
    "                [\" *** \", \"*   *\", \"*   *\", \" *** \", \"*   *\", \"*   *\", \" *** \"],\n",
    "                [\" ****\", \"*   *\", \"*   *\", \" ****\", \"    *\", \"    *\", \"    *\"],\n",
    "            ]\n",
    "            Number = str(Number)\n",
    "            if Number is None:\n",
    "                raise Exception(\"No input given.\")\n",
    "            ascii_art_lines = [\n",
    "                \"\".join(digits[int(digit)][i] + \"  \" for digit in Number)\n",
    "                for i in range(7)\n",
    "            ]\n",
    "            return \"\\n\".join(ascii_art_lines)\n",
    "\n",
    "        @classmethod\n",
    "        def to_hex(cls, Decimal_Number: int) -> str:\n",
    "            \"\"\"\n",
    "            Convert a decimal number to its hexadecimal representation.\n",
    "\n",
    "            :param Decimal_Number: The decimal number to convert.\n",
    "            :return: The hexadecimal representation of the decimal number.\n",
    "            \"\"\"\n",
    "            if Decimal_Number is None:\n",
    "                raise Exception(\"No decimal number provided\")\n",
    "            cls.__check_input_type(Decimal_Number, (int, str))\n",
    "            return hex(Decimal_Number)[2:].upper()\n",
    "\n",
    "        @classmethod\n",
    "        def to_bin(cls, Decimal_Number: int) -> int:\n",
    "            \"\"\"\n",
    "            Convert a decimal number to its binary representation.\n",
    "\n",
    "            :param Decimal_Number: The decimal number to convert.\n",
    "            :return: The binary representation of the decimal number.\n",
    "            \"\"\"\n",
    "            if Decimal_Number is None:\n",
    "                raise Exception(\"No decimal number provided\")\n",
    "            cls.__check_input_type(Decimal_Number, (int, str))\n",
    "            return int(bin(Decimal_Number)[2:])\n",
    "\n",
    "    class Hexadecimal:\n",
    "        @staticmethod\n",
    "        def __check_input_type(value, expected_type) -> bool:\n",
    "            \"\"\"\n",
    "            Check if the input value is of the expected type.\n",
    "\n",
    "            :param value: The value to check.\n",
    "            :param expected_type: The expected type of the value.\n",
    "            :return: True if the value is of the expected type, otherwise raises an Exception.\n",
    "            \"\"\"\n",
    "            if not isinstance(value, expected_type):\n",
    "                raise Exception(\n",
    "                    f\"Expected {expected_type.__name__}, got {type(value).__name__}\"\n",
    "                )\n",
    "            return True\n",
    "\n",
    "        @classmethod\n",
    "        def to_bin(cls, Hexadecimal_Number: str) -> int:\n",
    "            \"\"\"\n",
    "            Convert a hexadecimal number to its binary representation.\n",
    "\n",
    "            :param Hexadecimal_Number: The hexadecimal number to convert.\n",
    "            :return: The binary representation of the hexadecimal number.\n",
    "            \"\"\"\n",
    "            if Hexadecimal_Number is None:\n",
    "                raise Exception(\"No hexadecimal number provided\")\n",
    "            cls.__check_input_type(Hexadecimal_Number, str)\n",
    "            return int(bin(int(Hexadecimal_Number, 16))[2:])\n",
    "\n",
    "        @classmethod\n",
    "        def to_dec(cls, Hexadecimal_Number: str) -> int:\n",
    "            \"\"\"\n",
    "            Convert a hexadecimal number to its decimal representation.\n",
    "\n",
    "            :param Hexadecimal_Number: The hexadecimal number to convert.\n",
    "            :return: The decimal representation of the hexadecimal number.\n",
    "            \"\"\"\n",
    "            if Hexadecimal_Number is None:\n",
    "                raise Exception(\"No hexadecimal number provided\")\n",
    "            cls.__check_input_type(Hexadecimal_Number, str)\n",
    "            return int(Hexadecimal_Number, 16)\n",
    "\n",
    "    class Roman:\n",
    "        @classmethod\n",
    "        def __init__(cls):\n",
    "            \"\"\"\n",
    "            Initialize the Roman numeral dictionary.\n",
    "            \"\"\"\n",
    "            cls.roman_dict = {\n",
    "                \"I\": 1,\n",
    "                \"V\": 5,\n",
    "                \"X\": 10,\n",
    "                \"L\": 50,\n",
    "                \"C\": 100,\n",
    "                \"D\": 500,\n",
    "                \"M\": 1000,\n",
    "                \"IV\": 4,\n",
    "                \"IX\": 9,\n",
    "                \"XL\": 40,\n",
    "                \"XC\": 90,\n",
    "                \"CD\": 400,\n",
    "                \"CM\": 900,\n",
    "            }\n",
    "\n",
    "        @classmethod\n",
    "        def to_dec(cls, Roman: str) -> int:\n",
    "            \"\"\"\n",
    "            Convert a Roman numeral to its decimal representation.\n",
    "\n",
    "            :param Roman: The Roman numeral to convert.\n",
    "            :return: The decimal representation of the Roman numeral.\n",
    "            \"\"\"\n",
    "            if getattr(cls, \"roman_dict\", None) is None:\n",
    "                cls.__init__()\n",
    "            if not isinstance(Roman, str):\n",
    "                raise Exception(\"Input must be a string.\")\n",
    "            elif not Roman.isupper():\n",
    "                raise Exception(\"Input must be uppercase.\")\n",
    "            elif Roman is None:\n",
    "                raise Exception(\"Input cannot be None.\")\n",
    "\n",
    "            roman_to_numerical = cls.roman_dict\n",
    "            return cls.__convert_roman_to_decimal(Roman, roman_to_numerical)\n",
    "\n",
    "        @staticmethod\n",
    "        def __convert_roman_to_decimal(Roman: str, roman_to_numerical: dict) -> int:\n",
    "            \"\"\"\n",
    "            Convert a Roman numeral string to its decimal representation.\n",
    "\n",
    "            :param Roman: The Roman numeral string to convert.\n",
    "            :param roman_to_numerical: A dictionary mapping Roman numeral strings to their decimal values.\n",
    "            :return: The decimal representation of the Roman numeral.\n",
    "            \"\"\"\n",
    "            i, num = 0, 0\n",
    "            while i < len(Roman):\n",
    "                if i + 1 < len(Roman) and Roman[i: i + 2] in roman_to_numerical:\n",
    "                    num += roman_to_numerical[Roman[i: i + 2]]\n",
    "                    i += 2\n",
    "                else:\n",
    "                    num += roman_to_numerical[Roman[i]]\n",
    "                    i += 1\n",
    "            return num\n",
    "\n",
    "    class Celsius:\n",
    "        @staticmethod\n",
    "        def to_fahrenheit(celsius: float | int) -> float:\n",
    "            \"\"\"\n",
    "            Convert a temperature from Celsius to Fahrenheit.\n",
    "\n",
    "            :param celsius: The temperature in Celsius.\n",
    "            :return: The temperature in Fahrenheit.\n",
    "            \"\"\"\n",
    "            if celsius is None:\n",
    "                raise Exception(\"No temperature provided\")\n",
    "            return (celsius * 9 / 5) + 32\n",
    "\n",
    "        @staticmethod\n",
    "        def to_kelvin(celsius: float | int) -> float:\n",
    "            \"\"\"\n",
    "            Convert a temperature from Celsius to Kelvin.\n",
    "\n",
    "            :param celsius: The temperature in Celsius.\n",
    "            :return: The temperature in Kelvin.\n",
    "            \"\"\"\n",
    "            if celsius is None:\n",
    "                raise Exception(\"No temperature provided\")\n",
    "            return celsius + 273.15\n",
    "\n",
    "    class Kelvin:\n",
    "        @staticmethod\n",
    "        def to_celsius(kelvin: float | int) -> float:\n",
    "            \"\"\"\n",
    "            Convert a temperature from Kelvin to Celsius.\n",
    "\n",
    "            :param kelvin: The temperature in Kelvin.\n",
    "            :return: The temperature in Celsius.\n",
    "            \"\"\"\n",
    "            if kelvin is None:\n",
    "                raise Exception(\"No temperature provided\")\n",
    "            return kelvin - 273.15\n",
    "\n",
    "        @staticmethod\n",
    "        def to_fahrenheit(kelvin: float | int) -> float:\n",
    "            \"\"\"\n",
    "            Convert a temperature from Kelvin to Fahrenheit.\n",
    "\n",
    "            :param kelvin: The temperature in Kelvin.\n",
    "            :return: The temperature in Fahrenheit.\n",
    "            \"\"\"\n",
    "            if kelvin is None:\n",
    "                raise Exception(\"No temperature provided\")\n",
    "            return (kelvin - 273.15) * 9 / 5 + 32\n",
    "\n",
    "    class Fahrenheit:\n",
    "        @staticmethod\n",
    "        def to_kelvin(fahrenheit: float | int) -> float:\n",
    "            \"\"\"\n",
    "            Convert a temperature from Fahrenheit to Kelvin.\n",
    "\n",
    "            :param fahrenheit: The temperature in Fahrenheit.\n",
    "            :return: The temperature in Kelvin.\n",
    "            \"\"\"\n",
    "            if fahrenheit is None:\n",
    "                raise Exception(\"No temperature provided\")\n",
    "            return (fahrenheit - 32) * 5 / 9 + 273.15\n",
    "\n",
    "        @staticmethod\n",
    "        def to_celsius(fahrenheit: float | int) -> float:\n",
    "            \"\"\"\n",
    "            Convert a temperature from Fahrenheit to Celsius.\n",
    "\n",
    "            :param fahrenheit: The temperature in Fahrenheit.\n",
    "            :return: The temperature in Celsius.\n",
    "            \"\"\"\n",
    "            if fahrenheit is None:\n",
    "                raise Exception(\"No temperature provided\")\n",
    "            return (fahrenheit - 32) * 5 / 9\n",
    "\n",
    "    @classmethod\n",
    "    def memory(cls, number: int, input_unit: str, output_unit: str) -> str:\n",
    "        \"\"\"\n",
    "        Convert a memory size from one unit to another.\n",
    "\n",
    "        :param number: The memory size to convert.\n",
    "        :param input_unit: The unit of the input memory size.\n",
    "        :param output_unit: The unit of the output memory size.\n",
    "        :return: The converted memory size in the output unit.\n",
    "        \"\"\"\n",
    "        mem_values = {\n",
    "            \"bit\": 1,\n",
    "            \"byte\": 8,\n",
    "            \"kilobyte\": 8000,\n",
    "            \"megabyte\": 8 * (1000 ** 2),\n",
    "            \"gigabyte\": 8 * (1000 ** 3),\n",
    "            \"terrabyte\": 8 * (1000 ** 4),\n",
    "            \"petabyte\": 8 * (1000 ** 5),\n",
    "            \"kibibyte\": 8192,\n",
    "            \"mebibyte\": 8 * (1024 ** 2),\n",
    "            \"gibibyte\": 8 * (1024 ** 3),\n",
    "            \"tebibyte\": 8 * (1024 ** 4),\n",
    "            \"pebibyte\": 8 * (1024 ** 5),\n",
    "            \"kilobit\": 1000,\n",
    "            \"megabit\": 1000 ** 2,\n",
    "            \"gigabit\": 1000 ** 3,\n",
    "            \"terrabit\": 1000 ** 4,\n",
    "            \"petabit\": 1000 ** 5,\n",
    "            \"kibibit\": 1024,\n",
    "            \"mebibit\": 1024 ** 2,\n",
    "            \"gibibit\": 1024 ** 3,\n",
    "            \"tebibit\": 1024 ** 4,\n",
    "            \"pebibit\": 1024 ** 5,\n",
    "            \"KB\": 8000,\n",
    "            \"MB\": 8 * (1000 ** 2),\n",
    "            \"GB\": 8 * (1000 ** 3),\n",
    "            \"TB\": 8 * (1000 ** 4),\n",
    "            \"PB\": 8 * (1000 ** 5),\n",
    "            \"KiB\": 8192,\n",
    "            \"MiB\": 8 * (1024 ** 2),\n",
    "            \"GiB\": 8 * (1024 ** 3),\n",
    "            \"TiB\": 8 * (1024 ** 4),\n",
    "            \"PiB\": 8 * (1024 ** 5),\n",
    "            \"Kb\": 1000,\n",
    "            \"Mb\": 1000 ** 2,\n",
    "            \"Gb\": 1000 ** 3,\n",
    "            \"Tb\": 1000 ** 4,\n",
    "            \"Pb\": 1000 ** 5,\n",
    "            \"Kib\": 1024,\n",
    "            \"Mib\": 1024 ** 2,\n",
    "            \"Gib\": 1024 ** 3,\n",
    "            \"Tib\": 1024 ** 4,\n",
    "            \"Pib\": 1024 ** 5,\n",
    "        }\n",
    "        cls.__validate_memory_input(number, input_unit, output_unit, mem_values)\n",
    "        if input_unit == output_unit:\n",
    "            return f\"{number} {output_unit}\"\n",
    "        final_number = cls.__convert_memory_size(number, input_unit, output_unit, mem_values)\n",
    "        return f\"{final_number:.15f}\".rstrip(\"0\").rstrip(\".\") + f\" {output_unit}\"\n",
    "\n",
    "    @classmethod\n",
    "    def __validate_memory_input(cls, number: int, input_unit: str, output_unit: str, memory_dict: dict):\n",
    "        \"\"\"\n",
    "        Validate the input parameters for memory conversion.\n",
    "\n",
    "        :param number: The memory size to convert.\n",
    "        :param input_unit: The unit of the input memory size.\n",
    "        :param output_unit: The unit of the output memory size.\n",
    "        :param memory_dict: The dictionary mapping memory units to their bit equivalents.\n",
    "        :raises Exception: If any input is invalid.\n",
    "        \"\"\"\n",
    "        if not all([number, input_unit, output_unit]):\n",
    "            raise Exception(f\"Invalid input: {number} {input_unit} -> {output_unit}\")\n",
    "        input_unit = cls.__normalize_unit(input_unit)\n",
    "        output_unit = cls.__normalize_unit(output_unit)\n",
    "        if not isinstance(number, int) or input_unit not in memory_dict or output_unit not in memory_dict:\n",
    "            raise Exception(f\"Invalid input: {number} {input_unit} -> {output_unit}\")\n",
    "\n",
    "    @staticmethod\n",
    "    def __normalize_unit(unit: str) -> str:\n",
    "        \"\"\"\n",
    "        Normalize the memory unit to a standard format.\n",
    "\n",
    "        :param unit: The memory unit to normalize.\n",
    "        :return: The normalized memory unit.\n",
    "        \"\"\"\n",
    "        return unit.lower() if len(unit) > 3 and unit.lower() != \"bit\" else unit\n",
    "\n",
    "    @classmethod\n",
    "    def __convert_memory_size(cls, number: int, input_unit: str, output_unit: str, memory_dict: dict) -> float:\n",
    "        \"\"\"\n",
    "        Convert a memory size from one unit to another.\n",
    "\n",
    "        :param number: The memory size to convert.\n",
    "        :param input_unit: The unit of the input memory size.\n",
    "        :param output_unit: The unit of the output memory size.\n",
    "        :param memory_dict: The dictionary mapping memory units to their bit equivalents.\n",
    "        :return: The converted memory size in the output unit.\n",
    "        \"\"\"\n",
    "        input_unit = cls.__normalize_unit(input_unit)\n",
    "        output_unit = cls.__normalize_unit(output_unit)\n",
    "        return (number * memory_dict[input_unit]) / memory_dict[output_unit]\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}