1
+ import sys
1
2
class Definations :
2
3
'''Contains the actual logic for base conversion.
3
4
@@ -40,6 +41,10 @@ class Converter(Definations):
40
41
'''Inherits the Definations Class and converts the number based on user input'''
41
42
def __init__ (self ,number ):
42
43
super ().__init__ (number )
44
+
45
+ def helper (self , func_name ):
46
+ return getattr (Definations ,func_name )(self )
47
+
43
48
def convert (self ,FROM = "d" ,TO = "b" ):
44
49
'''
45
50
By Default conversion takes place from decimal to binary.
@@ -49,36 +54,9 @@ def convert(self,FROM="d",TO="b"):
49
54
x-hexadecimal,
50
55
o-octal
51
56
'''
52
- bases = {'d' :"DECIMAL" ,'b' :"BINARY" ,'x' :'HEXADECIMAL' ,'o' :"OCTAL" }
53
- if FROM == 'd' :
54
- if TO == 'b' :
55
- ans = Definations .Decimal_to_Binary (self )
56
- elif TO == 'x' :
57
- ans = Definations .Decimal_to_Hexadecimal (self )
58
- elif TO == 'o' :
59
- ans = Definations .Decimal_to_Octal (self )
60
- if FROM == 'b' :
61
- if TO == 'd' :
62
- ans = Definations .Binary_to_Decimal (self )
63
- elif TO == 'x' :
64
- ans = Definations .Binary_to_Hexadecimal (self )
65
- elif TO == 'o' :
66
- ans = Definations .Binary_to_Octal (self )
67
- if FROM == 'x' :
68
- if TO == 'b' :
69
- ans = Definations .Hexadecimal_to_Binary (self )
70
- elif TO == 'd' :
71
- ans = Definations .Hexadecimal_to_Decimal (self )
72
- elif TO == 'o' :
73
- ans = Definations .Hexadecimal_to_Octal (self )
74
- if FROM == 'o' :
75
- if TO == 'b' :
76
- ans = Definations .Octal_to_Binary (self )
77
- elif TO == 'd' :
78
- ans = Definations .Octal_to_Decimal (self )
79
- elif TO == 'x' :
80
- ans = Definations .Octal_to_Hexadecimal (self )
81
- return f"\n { self .number } in { bases [FROM ]} = { ans } in { bases [TO ]} "
57
+ bases = {'d' :"Decimal" ,'b' :"Binary" ,'x' :'Hexadecimal' ,'o' :"Octal" }
58
+ to_call_function = bases [FROM ] + '_to_' + bases [TO ]
59
+ return f"\n { self .number } in { bases [FROM ]} = { self .helper (to_call_function )} in { bases [TO ]} "
82
60
83
61
def header_decoration ():
84
62
print ('''
@@ -90,7 +68,8 @@ def footer_decoration():
90
68
-------------- --------------------------- -----------
91
69
''' )
92
70
93
- header_decoration ()
94
- Num = Converter (input ("Enter number = " ))
95
- print (Num .convert (input ("From = " ),input ("To = " )))
96
- footer_decoration ()
71
+ if __name__ == '__main__' :
72
+ header_decoration ()
73
+ Num = Converter (input ("Enter number = " ))
74
+ print (Num .convert (input ("From = " ),input ("To = " )))
75
+ footer_decoration ()
0 commit comments