Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 9c50d60

Browse files
Documenting - convert.py
Also fixing minor bugs
1 parent 2e2d424 commit 9c50d60

File tree

1 file changed

+73
-35
lines changed

1 file changed

+73
-35
lines changed

algopy/convert.py

Lines changed: 73 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -195,27 +195,12 @@ def to_dec(cls, Hexadecimal_Number: str) -> int:
195195
return int(Hexadecimal_Number, 16)
196196

197197
class Roman:
198-
@staticmethod
199-
def to_dec(Roman: str) -> int:
198+
@classmethod
199+
def __init__(cls):
200200
"""
201-
Convert a Roman numeral to its decimal representation.
202-
203-
:param Roman: The Roman numeral to convert.
204-
:return: The decimal representation of the Roman numeral.
201+
Initialize the Roman numeral dictionary.
205202
"""
206-
if not isinstance(Roman, str):
207-
raise Exception("Input must be a string.")
208-
elif not Roman.isupper():
209-
raise Exception("Input must be uppercase.")
210-
elif Roman is None:
211-
raise Exception("Input cannot be None.")
212-
213-
roman_to_numerical = Convert.Roman.__get_roman_to_numerical_mapping()
214-
return Convert.Roman.__convert_roman_to_decimal(Roman, roman_to_numerical)
215-
216-
@staticmethod
217-
def __get_roman_to_numerical_mapping() -> dict[str, int]:
218-
return {
203+
cls.roman_dict = {
219204
"I": 1,
220205
"V": 5,
221206
"X": 10,
@@ -231,8 +216,33 @@ def __get_roman_to_numerical_mapping() -> dict[str, int]:
231216
"CM": 900,
232217
}
233218

219+
@classmethod
220+
def to_dec(cls, Roman: str) -> int:
221+
"""
222+
Convert a Roman numeral to its decimal representation.
223+
224+
:param Roman: The Roman numeral to convert.
225+
:return: The decimal representation of the Roman numeral.
226+
"""
227+
if not isinstance(Roman, str):
228+
raise Exception("Input must be a string.")
229+
elif not Roman.isupper():
230+
raise Exception("Input must be uppercase.")
231+
elif Roman is None:
232+
raise Exception("Input cannot be None.")
233+
234+
roman_to_numerical = cls.roman_dict
235+
return cls.__convert_roman_to_decimal(Roman, roman_to_numerical)
236+
234237
@staticmethod
235238
def __convert_roman_to_decimal(Roman: str, roman_to_numerical: dict) -> int:
239+
"""
240+
Convert a Roman numeral string to its decimal representation.
241+
242+
:param Roman: The Roman numeral string to convert.
243+
:param roman_to_numerical: A dictionary mapping Roman numeral strings to their decimal values.
244+
:return: The decimal representation of the Roman numeral.
245+
"""
236246
i, num = 0, 0
237247
while i < len(Roman):
238248
if i + 1 < len(Roman) and Roman[i: i + 2] in roman_to_numerical:
@@ -318,8 +328,8 @@ def to_celsius(fahrenheit: float | int) -> float:
318328
raise Exception("No temperature provided")
319329
return (fahrenheit - 32) * 5 / 9
320330

321-
@staticmethod
322-
def memory(number: int, input_unit: str, output_unit: str) -> str:
331+
@classmethod
332+
def memory(cls, number: int, input_unit: str, output_unit: str) -> str:
323333
"""
324334
Convert a memory size from one unit to another.
325335
@@ -328,16 +338,20 @@ def memory(number: int, input_unit: str, output_unit: str) -> str:
328338
:param output_unit: The unit of the output memory size.
329339
:return: The converted memory size in the output unit.
330340
"""
331-
memory_dict = Convert.__get_memory_dict()
332-
Convert.__validate_memory_input(number, input_unit, output_unit, memory_dict)
341+
cls.__validate_memory_input(number, input_unit, output_unit, cls.mem_values)
333342
if input_unit == output_unit:
334343
return f"{number} {output_unit}"
335-
final_number = Convert.__convert_memory_size(number, input_unit, output_unit, memory_dict)
344+
final_number = cls.__convert_memory_size(number, input_unit, output_unit, cls.mem_values)
336345
return f"{final_number:.15f}".rstrip("0").rstrip(".") + f" {output_unit}"
337346

338-
@staticmethod
339-
def __get_memory_dict() -> dict:
340-
return {
347+
@classmethod
348+
def __init__(cls):
349+
"""
350+
Get a dictionary mapping memory units to their bit equivalents.
351+
352+
:return: A dictionary with memory units as keys and their bit equivalents as values.
353+
"""
354+
cls.mem_values = {
341355
"bit": 1,
342356
"byte": 8,
343357
"kilobyte": 8000,
@@ -382,21 +396,45 @@ def __get_memory_dict() -> dict:
382396
"Pib": 1024 ** 5,
383397
}
384398

385-
@staticmethod
386-
def __validate_memory_input(number: int, input_unit: str, output_unit: str, memory_dict: dict):
399+
@classmethod
400+
def __validate_memory_input(cls, number: int, input_unit: str, output_unit: str, memory_dict: dict):
401+
"""
402+
Validate the input parameters for memory conversion.
403+
404+
:param number: The memory size to convert.
405+
:param input_unit: The unit of the input memory size.
406+
:param output_unit: The unit of the output memory size.
407+
:param memory_dict: The dictionary mapping memory units to their bit equivalents.
408+
:raises Exception: If any input is invalid.
409+
"""
387410
if not all([number, input_unit, output_unit]):
388411
raise Exception(f"Invalid input: {number} {input_unit} -> {output_unit}")
389-
input_unit = Convert.__normalize_unit(input_unit)
390-
output_unit = Convert.__normalize_unit(output_unit)
412+
input_unit = cls.__normalize_unit(input_unit)
413+
output_unit = cls.__normalize_unit(output_unit)
391414
if not isinstance(number, int) or input_unit not in memory_dict or output_unit not in memory_dict:
392415
raise Exception(f"Invalid input: {number} {input_unit} -> {output_unit}")
393416

394417
@staticmethod
395418
def __normalize_unit(unit: str) -> str:
419+
"""
420+
Normalize the memory unit to a standard format.
421+
422+
:param unit: The memory unit to normalize.
423+
:return: The normalized memory unit.
424+
"""
396425
return unit.lower() if len(unit) > 3 and unit.lower() != "bit" else unit
397426

398-
@staticmethod
399-
def __convert_memory_size(number: int, input_unit: str, output_unit: str, memory_dict: dict) -> float:
400-
input_unit = Convert.__normalize_unit(input_unit)
401-
output_unit = Convert.__normalize_unit(output_unit)
427+
@classmethod
428+
def __convert_memory_size(cls, number: int, input_unit: str, output_unit: str, memory_dict: dict) -> float:
429+
"""
430+
Convert a memory size from one unit to another.
431+
432+
:param number: The memory size to convert.
433+
:param input_unit: The unit of the input memory size.
434+
:param output_unit: The unit of the output memory size.
435+
:param memory_dict: The dictionary mapping memory units to their bit equivalents.
436+
:return: The converted memory size in the output unit.
437+
"""
438+
input_unit = cls.__normalize_unit(input_unit)
439+
output_unit = cls.__normalize_unit(output_unit)
402440
return (number * memory_dict[input_unit]) / memory_dict[output_unit]

0 commit comments

Comments
 (0)