-
Notifications
You must be signed in to change notification settings - Fork 0
/
PermUnit.py
295 lines (271 loc) · 10.3 KB
/
PermUnit.py
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# Flet is a Graphical User Interface design library that is licensed
# under the Apache 2.0 license. More information can be found at
# https://flet.dev
# FLET MUST BE INSTALLED FOR THE PROGRAM TO WORK.
# Install Flet using:
# pip install flet
import flet
# The decimal library had to be imported to provide accurate and
# precise calculations.
# Example:
# Without using Decimal: 0.1 + 0.1 + 0.1 returns 0.30000000000000004
# With using Decimal: Decimal("0.1") + Decimal("0.1") + Decimal("0.1") returns Decimal("0.3").
# The values are stored as strings inside Decimal to preserve their precision.
from decimal import Decimal, getcontext, ROUND_HALF_UP, ROUND_DOWN, localcontext
# Similar to Decimal, fractions uses Decimal but returns a fractional
# value.
from fractions import Fraction
# This represents the main procedure for the application that is run
# when the application starts
def main(page: flet.Page):
# Here are variables that configure the basic application (Title,
# light or dark mode, window height, window width, and padding)
page.title = "Unit Converter"
page.theme_mode = 'DARK' # 'DARK' or 'LIGHT'
page.window_height = 720
page.window_width = 640
page.padding = 50
# This list represents options for the user to choose between.
# The procedure getUnitNames takes one of these values as input,
# either Distance or Weight
inUnits = ["Distance", "Weight"]
# Here other lists are defined for use in storing values of items
# in dropdown menus.
possibleUnits = []
possibleConversions = []
# This is the standard width of a dropdown menu component, which
# ensures all text is nicely readable.
standardWidth = 200
# This procedure takes the parameter unitType, which is either
# Distance or Weight, and returns either units for distance or
# units for weight based on the input parameter
def getUnitNames(unitType):
if unitType == "Distance":
distanceUnits = [
'picometers',
'nanometers',
'millimeters',
'centimeters',
'decimeters',
'meters',
'decameters',
'hectometers',
'kilometers',
'megameters',
'gigameters',
'terameters',
'inches',
'feet',
'miles',
'lightyears'
]
return distanceUnits
elif unitType == "Weight":
weightUnits = [
'picograms',
'nanograms',
'milligrams',
'centigrams',
'decigrams',
'grams',
'decagrams',
'hectograms',
'kilograms',
'tons (metric)',
'gigagrams',
'terragrams',
'pounds',
'ounces',
'tons (imperial)'
]
return weightUnits
# This procedure takes the parameter unitType, which is either
# Distance or Weight, and returns either conversions for distance
# or conversions for weight based on the input parameter
# The conversions are based on a base unit which is meters for
# distance or grams for weight.
# The conversions for distance represent "how many" of the input
# unit are in one meter, and the conversions for weight
# represents "how many" of the input unit are in one gram.
def getConversions(unitType):
# if the parameter unitType was Distance
if unitType == "Distance":
distanceConversions = [
1000000000,
1000000,
1000,
100,
10,
1,
0.1,
0.01,
0.001,
0.000001,
0.000000001,
0.000000000001,
39.37008,
3.28084,
0.0006213712,
0.0000000000000001057004
]
return distanceConversions
# if the parameter unitType was Weight
elif unitType == "Weight":
weightConversions = [
1000000000,
1000000,
1000,
100,
10,
1,
0.1,
0.01,
0.001,
0.000001,
0.000000001,
0.000000000001,
0.002204623,
0.03527396,
1.102311e-6
]
return weightConversions
# This procedure is called when the unitTypeDrop dropdown is
# changed.
# The parameter e is not used but is required to be passed to a
# procedure by the Flet GUI library.
def unitTypeChanged(e):
# Global variables needed to be used as parameters cannot be
# passed in procedures that are called when data in a Flet
# component is changed.
# Otherwise, procedure parameters would be far better.
global possibleUnits
global possibleConversions
# Get the value of the unitTypeDrop dropdown menu (either
# Weight or Distance)
unitType = unitTypeDrop.value
# Create a possibleUnits list that contains the unit names in
# the selected unit category
if unitType == "Distance":
possibleUnits = getUnitNames("Distance")
elif unitType == "Weight":
possibleUnits = getUnitNames("Weight")
inDrop.options = (
flet.dropdown.Option(i) for i in possibleUnits
)
# Create a possibleConversions list that contains the
# conversions that correspond to the units in possibleUnits
if unitType == "Distance":
possibleConversions = getConversions("Distance")
elif unitType == "Weight":
possibleConversions = getConversions("Weight")
outDrop.options = (
flet.dropdown.Option(i) for i in possibleUnits
)
# update the page
page.update()
# This procedure is used when converting input to output.
# The parameter e is not used but is required to be passed to a
# procedure by the Flet GUI library.
def convert(e):
# By default, the Decimal library will round a 5 at the end
# of a decimal down, which is incorrectly rounded.
# Thus, to recieve a correct answer, Decimal must round 5 up
# to 10 instead of to 0.
getcontext().rounding=ROUND_HALF_UP
# Global variables needed to be used as parameters cannot be
# passed in procedures that are called when data in a Flet
# component is changed.
# Otherwise, procedure parameters would be far better.
global possibleUnits
global possibleConversions
# This part gets the values of all the user inputs. Firstly
# the input unit from inDrop, next the value of the input
# unit, and finally the output unit.
inUnit = inDrop.value
inputValue = inValue.value
outUnit = outDrop.value
# Here the appropriate conversions are obtained from the
# possibleConversions list for both the input and output
# units.
# The conversions are selected using the corresponding index
# of the input and output units in the possibleUnits list.
# The index of the conversions in the possibleConversions
# list corresponds with the index of each unit in the
# possibleUnits list.
inUnitConversion = possibleConversions[
possibleUnits.index(inUnit)
]
outUnitConversion = possibleConversions[
possibleUnits.index(outUnit)
]
decInVal = Decimal(str(inputValue))
decInUnitConv = Decimal(str(inUnitConversion))
decOutUnitConv = Decimal(str(outUnitConversion))
# Compute the output value by actually converting the unit.
outValue = decInVal * (Decimal("1") / decInUnitConv) * decOutUnitConv
# Find the ratio of the conversion
fracInVal = Fraction(str(inputValue))
fracInUnitConv = Fraction(str(inUnitConversion))
fracOutUnitConv = Fraction(str(outUnitConversion))
ratio = Fraction(str(fracInVal * 1 / fracInUnitConv * fracOutUnitConv))
conversionFactor = Fraction(str(Fraction(1) / fracInUnitConv * fracOutUnitConv))
conversionFactorRatio = Fraction(conversionFactor)
# Display the output and ratio to the user as a formatted
# string
resultLabel.value = f"""{str(inputValue)} {inUnit}
= {str(outValue)} {outUnit}
= {ratio.numerator} / {ratio.denominator} {outUnit} \n
Conversion Factor:
INPUT * (1 / {Decimal(str(inUnitConversion))}) * {Decimal(str(outUnitConversion))}
= INPUT * {conversionFactorRatio.numerator} / {conversionFactorRatio.denominator}"""
# Update the Graphical User Interface with the new label
# value for resultLabel
page.update()
# This is the dropdown menu that lets the user select the unit
# type (either Distance or Weight)
unitTypeDrop = flet.Dropdown(
label="Unit Type",
on_change=unitTypeChanged,
width=standardWidth,
options=[
flet.dropdown.Option(i) for i in inUnits
]
)
# This is the dropdown menu that lets the user select the input
# unit
inDrop = flet.Dropdown(
label="Input Unit",
#on_change=inChanged,
width=standardWidth
)
# Here the user gives the value of the input
inValue = flet.TextField(
label="Input Value",
width=standardWidth
)
# This is the dropdown menu that lets the user select the desired
# output unit
outDrop = flet.Dropdown(
label="Output Unit",
width=standardWidth
)
# This is the button that initiates the conversion
convertButton = flet.ElevatedButton(
text="Convert",
on_click=convert
)
# This label displays the conversion output
resultLabel = flet.Text(
value="Output will appear here."
)
# Here all the UI components are added to the page
page.add(
unitTypeDrop,
inDrop,
inValue,
outDrop,
convertButton,
resultLabel
)
# Start the app
flet.app(target=main)