forked from bradmontgomery/alfred-percent-change
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpercent.py
260 lines (200 loc) · 7.86 KB
/
percent.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
# encoding: utf-8
"""
This is the python code for the calculations. To build the workflow, I just
copy & paste this code into Alfred.
Supported Calculations:
1. Percent Change (increase/decrease): `% 3 6` -> 100%
2. Percentage of; 3 is what percent of 100: `% 3 of 100` -> 3%
3. Percent of: 5 percent of 100 is 5: `% 5 percent of 100` -> 5, or `% 5% of 100` -> 5
4. Percent Decrease; What is 2 percent from 100: `% 100 - 2%`
5. Percent Increase; What is 100 + 2%: `% 100 + 2%`
6. Original number before Percent Decrease; What number is 100 2 percent less than?:`% 100 is 2% lt`
7. Original number before Percent Increase; What number is 100 2 percent more than?:`% 100 is 2% gt`
"""
__version__ = "1.5.1"
import sys
import types
from workflow import Workflow, ICON_INFO
def percent_increase(a, b):
"""What is `a` + `b`%?"""
return str(round(a + (a * (b / 100.0)), 2))
def percent_decrease(a, b):
"""What is `a` - `b`%?"""
return str(round(a - (a * (b / 100.0)), 2))
def percentage_of(a, b):
"""`a` of `b` is what percent?"""
return "{0}%".format(str(round((a / b) * 100, 2)))
def percent_of(a, b):
"""Calculates `a` percent of `b` is what percent?"""
return str(round((a / 100.0) * b, 2))
def percent_change(a, b):
"""Given two floats, calculate the percent change and return a rounded
string representation of the change."""
if a > 0:
result = ((b - a) / a) * 100
else:
result = 0
return str(round(result, 2))
def before_percent_decrease(a, b):
"""What is `a` `b`% less than"""
percent = b / 100.0
result = a / (1 - percent)
return str(round(result, 2))
def before_percent_increase(a, b):
"""What is `a` `b`% more than"""
percent = b / 100
result = a * (1 - percent)
return str(round(result, 2))
def parse(args):
"""Inspects the input arguments and calls the correct function.
Returns a Tuple:
(the calculated value, a human-readable version of the function used)
"""
try:
values = ''
if len(args) == 1 and type(args) == list:
values = args[0].strip().split(' ')
if len(values) == 2:
# `% a b`. percent_change.
result = percent_change(float(values[0]), float(values[1]))
return (result, 'Percent Change')
elif len(values) == 4 and 'percent' in values and 'of' in values:
# `% a percent of b`. percent_of
a = float(values[0])
b = float(values[-1])
result = percent_of(a, b)
return (result, 'Percentage of')
elif len(values) == 3 and values[0].endswith("%"):
# `% a% of b`. percent_of
a = float(values[0].strip("%"))
b = float(values[-1])
result = percent_of(a, b)
return (result, 'Percentage of')
elif len(values) == 3 and 'of' in values:
# `% a of b`.percentage_of
a = float(values[0])
b = float(values[-1])
result = percentage_of(a, b)
return (result, 'Percentage of')
elif len(values) == 3 and '-' in values:
# `% a - b%`. percent_decrease
a = float(values[0])
b = float(values[-1].replace("%", ""))
result = percent_decrease(a, b)
return (result, 'Percent decrease')
elif len(values) == 3 and '+' in values:
# `% a + b%`. percent_increase
a = float(values[0])
b = float(values[-1].replace("%", ""))
result = percent_increase(a, b)
return (result, 'Percent increase')
elif (len(values) == 3 or len(values) == 4) and 'lt' in values:
# `% a is b% lt` . original_number_before_decrease
# `is` verb optional
a = float(values[0])
b = float(values[-2].replace("%", ""))
result = before_percent_decrease(a, b)
return (result, 'Before decrease')
elif (len(values) == 3 or len(values) == 4) and 'gt' in values:
# `% a is b% lt` . original_number_before_decrease
# `is` verb optional
a = float(values[0])
b = float(values[-2].replace("%", ""))
result = before_percent_increase(a, b)
return (result, 'Before increase')
elif len(values) == 1 and "help" in values:
# `% help`
return [
("Increase / Decrease:", "% 3 6"),
("Add/Subtract:", "% 100 + 2%"),
("Percentage of:", "% 3 of 100"),
("Num before % increase:", "% 100 is 2% gt"),
("Num before % decrease:", "% 100 is 2% lt"),
]
else:
return ("What?", "I don't know what you mean.")
except ValueError:
return ("What?", "...")
def main(wf):
results = parse(wf.args)
if isinstance(results, types.TupleType):
title, subtitle = results
wf.add_item(title=title, subtitle=subtitle, arg=title)
elif isinstance(results, types.ListType):
results = parse(wf.args)
for i in results:
title, subtitle = i
wf.add_item(title=title, subtitle=subtitle, arg=title)
wf.send_feedback() # Send results back to Alfred as XML
# -----------------------------------------------------------------------------
# Some Tests. To run these, do:
#
# $ python script.py test
#
# (yes, I reinvented a wheel, here)
# -----------------------------------------------------------------------------
FAILURES = []
def _eq(a, b):
global FAILURES
try:
assert a == b
except AssertionError:
sys.stderr.write("F")
FAILURES.append("{0} is not equal to {1}".format(a, b))
else:
sys.stdout.write(".")
def test_percent_increase():
_eq(percent_increase(100.0, 2.0), "102.0")
_eq(percent_increase(12.34, 2.0), "12.59")
_eq(percent_increase(12.34, 2.5), "12.65")
_eq(percent_increase(14.0, 2.0), "14.28")
def test_percent_decrease():
_eq(percent_decrease(100.0, 2.0), "98.0")
_eq(percent_decrease(12.34, 2.0), "12.09")
_eq(percent_decrease(12.34, 2.5), "12.03")
_eq(percent_decrease(14.0, 2.0), "13.72")
def test_percentage_of():
_eq(percentage_of(3.0, 100.00), "3.0%")
_eq(percentage_of(2.0, 5.0), "40.0%")
_eq(percentage_of(5.0, 3.0), "166.67%")
def test_percent_of():
_eq(percent_of(3, 10), "0.3")
_eq(percent_of(100, 10), "10.0")
_eq(percent_of(5, 100), "5.0")
_eq(percent_of(110, 3), "3.3")
def test_percent_change():
_eq(percent_change(3.0, 6.0), "100.0")
_eq(percent_change(5.0, 2.0), "-60.0")
_eq(percent_change(3.5, 3.52), "0.57")
def test_before_percent_decrease():
_eq(before_percent_decrease(3.0, 2), "3.06")
_eq(before_percent_decrease(5.0, 1.5), "5.08")
_eq(before_percent_decrease(2000, 4.6), "2096.44")
def test_before_percent_increase():
_eq(before_percent_increase(3.0, 2), "2.94")
_eq(before_percent_increase(5.0, 1.5), "4.92")
_eq(before_percent_increase(2000, 4.6), "1908.0")
def test_parse():
_eq(parse("3 6"), "100.0") # percent_change
_eq(parse("2 of 5"), "40.0") # percentage_of
_eq(parse("100 - 2%"), "98.0") # percent_decrease
_eq(parse("100 + 2%"), "102.0") # percent_increase
_eq(parse("100 is 2% lt"), "102.04") # before_percent_decrease
_eq(parse("100 is 2% gt"), "98.0") # before_percent_decrease
if __name__ == "__main__":
if len(sys.argv) == 2 and sys.argv[1] == "test":
print("Running Tests:\n")
test_percent_increase()
test_percent_decrease()
test_percentage_of()
test_percent_of()
test_percent_change()
print("\n\nDone.")
if len(FAILURES):
print("ERRORS:")
print("\n{0}\n".format("\n".join(FAILURES)))
else:
wf = Workflow(libraries=['./lib'])
# uncomment to log via log.debug(var)
# log = wf.logger
sys.exit(wf.run(main))