@@ -195,27 +195,12 @@ def to_dec(cls, Hexadecimal_Number: str) -> int:
195
195
return int (Hexadecimal_Number , 16 )
196
196
197
197
class Roman :
198
- @staticmethod
199
- def to_dec ( Roman : str ) -> int :
198
+ @classmethod
199
+ def __init__ ( cls ) :
200
200
"""
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.
205
202
"""
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 = {
219
204
"I" : 1 ,
220
205
"V" : 5 ,
221
206
"X" : 10 ,
@@ -231,8 +216,33 @@ def __get_roman_to_numerical_mapping() -> dict[str, int]:
231
216
"CM" : 900 ,
232
217
}
233
218
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
+
234
237
@staticmethod
235
238
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
+ """
236
246
i , num = 0 , 0
237
247
while i < len (Roman ):
238
248
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:
318
328
raise Exception ("No temperature provided" )
319
329
return (fahrenheit - 32 ) * 5 / 9
320
330
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 :
323
333
"""
324
334
Convert a memory size from one unit to another.
325
335
@@ -328,16 +338,20 @@ def memory(number: int, input_unit: str, output_unit: str) -> str:
328
338
:param output_unit: The unit of the output memory size.
329
339
:return: The converted memory size in the output unit.
330
340
"""
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 )
333
342
if input_unit == output_unit :
334
343
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 )
336
345
return f"{ final_number :.15f} " .rstrip ("0" ).rstrip ("." ) + f" { output_unit } "
337
346
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 = {
341
355
"bit" : 1 ,
342
356
"byte" : 8 ,
343
357
"kilobyte" : 8000 ,
@@ -382,21 +396,45 @@ def __get_memory_dict() -> dict:
382
396
"Pib" : 1024 ** 5 ,
383
397
}
384
398
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
+ """
387
410
if not all ([number , input_unit , output_unit ]):
388
411
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 )
391
414
if not isinstance (number , int ) or input_unit not in memory_dict or output_unit not in memory_dict :
392
415
raise Exception (f"Invalid input: { number } { input_unit } -> { output_unit } " )
393
416
394
417
@staticmethod
395
418
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
+ """
396
425
return unit .lower () if len (unit ) > 3 and unit .lower () != "bit" else unit
397
426
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 )
402
440
return (number * memory_dict [input_unit ]) / memory_dict [output_unit ]
0 commit comments