1
1
import sys
2
+
3
+
2
4
class Definations :
3
5
'''Contains the actual logic for base conversion.
4
6
5
7
Parent class to Converter class
6
8
'''
7
- def __init__ (self ,number ):
9
+
10
+ def __init__ (self , number ):
8
11
self .number = number
12
+
9
13
def Decimal_to_Binary (self ):
10
14
return bin (int (self .number ))[2 :]
15
+
11
16
def Binary_to_Decimal (self ):
12
- return int (str (self .number ),2 )
17
+ return int (str (self .number ), 2 )
18
+
13
19
def Decimal_to_Octal (self ):
14
20
return oct (int (self .number ))[2 :]
21
+
15
22
def Octal_to_Decimal (self ):
16
- return int (str (self .number ),8 )
23
+ return int (str (self .number ), 8 )
24
+
17
25
def Decimal_to_Hexadecimal (self ):
18
26
return hex (self .number )[2 :]
27
+
19
28
def Hexadecimal_to_Decimal (self ):
20
- return int (str (self .number ),16 )
29
+ return int (str (self .number ), 16 )
30
+
21
31
def Hexadecimal_to_Binary (self ):
22
- num = int (str (self .number ),16 )
32
+ num = int (str (self .number ), 16 )
23
33
return bin (num )[2 :]
34
+
24
35
def Hexadecimal_to_Octal (self ):
25
- num = int (str (self .number ),16 )
36
+ num = int (str (self .number ), 16 )
26
37
return oct (num )[2 :]
38
+
27
39
def Binary_to_Octal (self ):
28
- num = int (str (self .number ),2 )
40
+ num = int (str (self .number ), 2 )
29
41
return oct (num )[2 :]
42
+
30
43
def Binary_to_Hexadecimal (self ):
31
- num = int (str (self .number ),2 )
44
+ num = int (str (self .number ), 2 )
32
45
return hex (num )[2 :]
46
+
33
47
def Octal_to_Hexadecimal (self ):
34
- num = int (str (self .number ),8 )
48
+ num = int (str (self .number ), 8 )
35
49
return hex (num )[2 :]
50
+
36
51
def Octal_to_Binary (self ):
37
- num = int (str (self .number ),8 )
52
+ num = int (str (self .number ), 8 )
38
53
return bin (num )[2 :]
39
54
55
+
40
56
class Converter (Definations ):
41
57
'''Inherits the Definations Class and converts the number based on user input'''
42
- def __init__ (self ,number ):
58
+
59
+ def __init__ (self , number ):
43
60
super ().__init__ (number )
44
61
45
62
def helper (self , func_name ):
46
- return getattr (Definations ,func_name )(self )
63
+ return getattr (Definations , func_name )(self )
47
64
48
- def convert (self ,FROM = "d" ,TO = "b" ):
65
+ def convert (self , FROM = "d" , TO = "b" ):
49
66
'''
50
67
By Default conversion takes place from decimal to binary.
51
68
specify the FROM and TO parameters from the following:
@@ -54,22 +71,27 @@ def convert(self,FROM="d",TO="b"):
54
71
x-hexadecimal,
55
72
o-octal
56
73
'''
57
- bases = {'d' :"Decimal" ,'b' :"Binary" ,'x' :'Hexadecimal' ,'o' :"Octal" }
74
+ bases = {'d' : "Decimal" , 'b' : "Binary" ,
75
+ 'x' : 'Hexadecimal' , 'o' : "Octal" }
58
76
to_call_function = bases [FROM ] + '_to_' + bases [TO ]
59
77
return f"\n { self .number } in { bases [FROM ]} = { self .helper (to_call_function )} in { bases [TO ]} "
60
78
79
+
61
80
def header_decoration ():
62
81
print ('''
63
82
-------------- WELCOME TO NUMBER CONVERTER --------------
64
83
-------------- --------------------------- --------------
65
84
''' )
85
+
86
+
66
87
def footer_decoration ():
67
88
print ('''
68
89
-------------- --------------------------- -----------
69
90
''' )
70
-
71
- if __name__ == '__main__' :
91
+
92
+
93
+ if __name__ == '__main__' :
72
94
header_decoration ()
73
95
Num = Converter (input ("Enter number = " ))
74
- print (Num .convert (input ("From = " ),input ("To = " )))
96
+ print (Num .convert (input ("From = " ), input ("To = " )))
75
97
footer_decoration ()
0 commit comments