Skip to content

Commit

Permalink
Merge pull request #6 from giovannicoppola/master
Browse files Browse the repository at this point in the history
updated to Python 3
  • Loading branch information
bradmontgomery authored Mar 30, 2022
2 parents 76b9d1a + 807fd5d commit 897c525
Show file tree
Hide file tree
Showing 22 changed files with 107 additions and 5,968 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
*.pyc
__pycache__
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

- [Brad Montgomery](mailto:brad@bradmontgomery.net)
- [Adam Wagner](https://github.com/AdamWagner)
- [Giovanni Coppola](https://github.com/giovannicoppola)
Binary file removed Percent-Change.alfredworkflow
Binary file not shown.
Binary file added PercentChange1-6.alfredworkflow
Binary file not shown.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ Percent Change
This is an Alfred Workflow to help you do percentage calculations. You can see
some background info on the original [forum post](http://www.alfredforum.com/topic/4731-percent-change/).

March 2022 updated to Python 3
--------------
<a href="https://github.com/giovannicoppola/alfred-percent-change/releases/latest/">

<img alt="Downloads"
src="https://img.shields.io/github/downloads/giovannicoppola/alfred-percent-change/total?color=purple&label=Downloads"><br/>
</a>

----------------


The supported calculations include:

1. *Percent Change* (increase/decrease): `% 3 6` -> 100%
Expand Down
Binary file removed icon.png
Binary file not shown.
Binary file removed percent.png
Binary file not shown.
19 changes: 19 additions & 0 deletions source/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2018, Brad Montgomery <brad@bradmontgomery.net>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Binary file added source/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 29 additions & 21 deletions info.plist → source/info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
<plist version="1.0">
<dict>
<key>bundleid</key>
<string>net.bradmontgomery.percent_change</string>
<string>giovanni.percent_change</string>
<key>category</key>
<string>Tools</string>
<key>connections</key>
<dict/>
<key>createdby</key>
<string>Brad Montgomery</string>
<string>giovanni from Brad Montgomery</string>
<key>description</key>
<string>Easily do percentage calculations.</string>
<key>disabled</key>
Expand All @@ -25,6 +25,8 @@
<false/>
<key>alfredfiltersresultsmatchmode</key>
<integer>0</integer>
<key>argumenttreatemptyqueryasnil</key>
<false/>
<key>argumenttrimmode</key>
<integer>0</integer>
<key>argumenttype</key>
Expand All @@ -44,17 +46,17 @@
<key>runningsubtext</key>
<string>calculating</string>
<key>script</key>
<string>python percent.py "{query}"</string>
<string>python3 percent.py "$1"</string>
<key>scriptargtype</key>
<integer>0</integer>
<integer>1</integer>
<key>scriptfile</key>
<string></string>
<key>subtext</key>
<string>Use "% help" to see examples.</string>
<key>title</key>
<string>Percent Calculations</string>
<key>type</key>
<integer>0</integer>
<integer>5</integer>
<key>withspace</key>
<true/>
</dict>
Expand All @@ -63,7 +65,7 @@
<key>uid</key>
<string>ABB6BA81-8665-40FE-A867-448DF94386CA</string>
<key>version</key>
<integer>2</integer>
<integer>3</integer>
</dict>
</array>
<key>readme</key>
Expand All @@ -73,34 +75,36 @@
This is an Alfred Workflow to help you do percentage calculations. You can see
some background info on the original [forum post](http://www.alfredforum.com/topic/4731-percent-change/).
Based on https://github.com/bradmontgomery/alfred-percent-change
Calculations #6 and #7 added by me.
Get the source from: https://github.com/bradmontgomery/alfred-percent-change
The supported calculations include:
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
1. *Percent Change* (increase/decrease): `% 3 6` -&gt; 100%
2. *Percentage Of*: 3 is what percent of 100: `% 3 of 100` -&gt; 3%
3. *Percent Of*: 5 percent of 100 is 5: `% 5 percent of 100` -&gt; 5, or `% 5% of 100` -&gt; 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`
## Build:
- Building uses the [workflow-build.py script](https://gist.github.com/AdamWagner/38228953422e830c4484e62ff116466a)
bundled in this repo.
- To build, run: `python workflow-build.py -f -d . -o .`
-------
Build:
## License
- workflow-build.py https://gist.github.com/AdamWagner/38228953422e830c4484e62ff116466a
- `python workflow-build.py -d . -o .`
This Workflow is available under the terms of the MIT License. See the full
[LICENSE](LICENSE.TXT) for more details.
License
-------
## Contributing
This Workflow is available under the terms of the MIT License.</string>
Contributions to this project are welcome. To contribute, feel free to fork
this repo, add your changes/features/improvements, then open a pull request.
Don't for get to add your name to the [AUTHORS file](AUTHORS.md).</string>
<key>uidata</key>
<dict>
<key>ABB6BA81-8665-40FE-A867-448DF94386CA</key>
Expand All @@ -111,7 +115,11 @@ This Workflow is available under the terms of the MIT License.</string>
<real>80</real>
</dict>
</dict>
<key>variablesdontexport</key>
<array/>
<key>version</key>
<string>1.6</string>
<key>webaddress</key>
<string>http://bradmontgomery.net</string>
<string></string>
</dict>
</plist>
Binary file added source/percent.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
145 changes: 44 additions & 101 deletions percent.py → source/percent.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# encoding: utf-8
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Partly cloudy ⛅️ 🌡️+43°F (feels +39°F, 22%) 🌬️0mph 🌑 Wed Mar 30 16:19:59 2022


"""
This is the python code for the calculations. To build the workflow, I just
copy & paste this code into Alfred.
Expand All @@ -21,10 +25,17 @@
"""

__version__ = "1.5.1"
__version__ = "1.6"
import sys
import types
from workflow import Workflow, ICON_INFO
import json


def log(s, *args):
if args:
s = s % args
print(s, file=sys.stderr)



def percent_increase(a, b):
Expand Down Expand Up @@ -80,8 +91,8 @@ def parse(args):
"""
try:
values = ''
if len(args) == 1 and type(args) == list:
values = args[0].strip().split(' ')
if len(args) == 2 and type(args) == list:
values = args[1].strip().split(' ')

if len(values) == 2:
# `% a b`. percent_change.
Expand Down Expand Up @@ -155,109 +166,41 @@ def parse(args):
return ("What?", "...")


def main(wf):
results = parse(wf.args)
if isinstance(results, types.TupleType):
def main():
result = {"items": []}
results = parse(sys.argv)

if isinstance(results, tuple):
title, subtitle = results
wf.add_item(title=title, subtitle=subtitle, arg=title)

elif isinstance(results, types.ListType):
results = parse(wf.args)
result["items"].append({
"title": title,
'subtitle': subtitle,
'valid': True,
'arg': title
})



elif isinstance(results, list):
results = parse(sys.argv)


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 percent.py test
#
# -----------------------------------------------------------------------------
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(".")

result["items"].append({
"title": title,
'subtitle': subtitle,
'valid': True,
'arg': title
})


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")
print (json.dumps(result))



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))
main()


Loading

0 comments on commit 897c525

Please sign in to comment.