forked from jenkins-docs/simple-python-pyinstaller-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.py
28 lines (26 loc) · 1001 Bytes
/
calc.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
'''
The 'calc' library contains the 'add2' function that takes 2 values and adds
them together. If either value is a string (or both of them are) 'add2' ensures
they are both strings, thereby resulting in a concatenated result.
NOTE: If a value submitted to the 'add2' function is a float, it must be done so
in quotes (i.e. as a string).
'''
# If 'value' is not an integer, convert it to a float and failing that, a string.
def conv(value):
try:
return int(value)
except ValueError:
try:
return float(value)
except ValueError:
return str(value)
# The 'add2' function itself
def add2(arg1, arg2):
# Convert 'arg1' and 'arg2' to their appropriate types
arg1conv = conv(arg1)
arg2conv = conv(arg2)
# If either 'arg1' or 'arg2' is a string, ensure they're both strings.
if isinstance(arg1conv, str) or isinstance(arg2conv, str):
arg1conv = str(arg1conv)
arg2conv = str(arg2conv)
return arg1conv + arg2conv