-
Notifications
You must be signed in to change notification settings - Fork 0
/
Base_Converter.html
179 lines (155 loc) · 4.2 KB
/
Base_Converter.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Base Converter</title>
<link rel="stylesheet" href="./style.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
</head>
<body>
<div class="centered-container">
<div class="unload main">
<h1> Base Converter Made in PyScript </h1>
<div>
<!-- text input for the number -->
<label for="a">Input Number</label>
<input id="a">
<!-- text input for the starting base -->
<label for="base">Starting Base(Max 32)</label>
<input id="basep">
<!-- text input for the arrival base -->
<label for="base">Arrival Base</label>
<input id="base">
<!-- convert button,this will call the funcion calculate using pys-onClick attribute -->
<button id="submit" pys-onClick="calculate">Convert</button>
<!-- this will display the error and result -->
<p style="color:red" id="err"></p>
<p id="res"></p>
<!-- the id attribute will be used to modify the DOM and for obataining the input -->
</div>
</div>
</div>
<py-script>
for e in document.querySelectorAll(".unload"):
e.classList.replace("unload","load")
# dictionary for converting numbers in letters(upto base 32)
conv_dictionary = {
0 : "0",
1 : "1",
2 : "2",
3 : "3",
4 : "4",
5 : "5",
6 : "6",
7 : "7",
8 : "8",
9 : "9",
10 : "A",
11 : "B",
12 : "C",
13 : "D",
14 : "E",
15 : "F",
16 : "G",
17 : "H",
18 : "I",
27 : "R",
19 : "J",
28 : "S",
20 : "K",
29 : "T",
21 : "L",
30 : "U",
22 : "M",
31 : "V",
23 : "N",
24 : "O",
25 : "P",
26 : "Q",
}
# reversed key-value dictionary
rev_conv_dictionary = dict(zip(conv_dictionary.values(),conv_dictionary.keys()))
# function that converts numbers in letters
def convert(a):
if type(a) is int:
if a > 9:
return conv_dictionary[a]
else:
return a
else:
if(a == "-"):
return 0
return rev_conv_dictionary[a]
#main event called from the button calculate(see the HTML tag)
def calculate(*args, **kwargs):
# DOM manipulation,resets the output of precedent calculation
# notice the first parameter, in this case the id of the HTML element
pyscript.write('res',"")
pyscript.write('err',"")
# getting the values from text input
# using try/except for invalid values
try:
# getting the input from the user
base_p = int(Element("basep").value,10)
base_a = int(Element("base").value,10)
# understanding if the input value base is valid
if(base_p <= 10):
if(base_p > 1):
a = int(Element("a").value,10)
else:
# base invalid,so i do DOM manipulation to modify the string of the
# <p> tag with id "err" with a new string
pyscript.write('err', "ERROR invalid base")
return
except:
# invalid input, could be null string o strange charaters
pyscript.write('err', "ERROR invalid input")
return
# controlling the arriving base
if(base_a < 2):
pyscript.write('err', "ERROR invalid base")
return
# this part is only about conversion algorithms
save = []
tmp = 0
negative = ""
if(base_p != 10):
try:
a = str(Element("a").value)
except:
pyscript.write('err', "ERROR invalid input")
return
i = len(a) - 1
try:
for letter in a:
tmp = int(convert(letter))*base_p**i + tmp
i = i - 1
if(a[0] == "-"):
negative = " -"
except:
pyscript.write('err', "ERROR letters outside range conversion")
a = tmp
if(a<0):
negative = " -"
a = -a
if (base_a != 10):
tmp = ""
while a != 0:
save.append(int(a%base_a))
a = int(a/base_a)
try:
for i in range(len(save)-1,-1,-1):
tmp += str(convert(save[i]))
tmp = negative + tmp
except:
tmp = negative + str(save[::-1])
pyscript.write('err',"base was too large for the avaiable charset,the conversion will be written in single digit output")
else:
tmp = negative + str(a)
# after the calculation code DOM is again modified to display the result
pyscript.write('res',"result: " + str(tmp) )
</py-script>
</body>
<!-- by LoreBadTime -->
</html>