Skip to content

Commit e6518c4

Browse files
Initial commit
0 parents  commit e6518c4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2408
-0
lines changed

.github/workflows/pythonapp.yml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# This workflow will install Python dependencies, run tests and lint with a single version of Python
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
3+
4+
name: Python application
5+
6+
on:
7+
push:
8+
branches: [ master ]
9+
pull_request:
10+
branches: [ master ]
11+
12+
jobs:
13+
build:
14+
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- uses: actions/checkout@v2
19+
- name: Set up Python 3.8
20+
uses: actions/setup-python@v1
21+
with:
22+
python-version: 3.8
23+
- name: Install dependencies
24+
run: |
25+
python -m pip install --upgrade pip
26+
pip install -r requirements/dev.txt
27+
- name: Lint with flake8 black isort
28+
run: make check-lint
29+
- name: Check static types with mypy
30+
run: make mypy
31+
- name: Test with pytest
32+
run: make test
33+
- name: check if code examples files are up to date
34+
run: python scripts/shorten_example.py --check

.github/workflows/pythonpublish.yml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Upload Python Package
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*'
7+
8+
jobs:
9+
release:
10+
name: Create Release
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@master
15+
- name: Create Release
16+
id: create_release
17+
uses: actions/create-release@v1
18+
env:
19+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
20+
with:
21+
tag_name: ${{ github.ref }}
22+
release_name: Release ${{ github.ref }}
23+
draft: false
24+
prerelease: false
25+
deploy:
26+
needs: release
27+
runs-on: ubuntu-latest
28+
steps:
29+
- uses: actions/checkout@v1
30+
- name: Set up Python
31+
uses: actions/setup-python@v1
32+
with:
33+
python-version: '3.6'
34+
- name: Install dependencies
35+
run: |
36+
python -m pip install --upgrade pip
37+
pip install setuptools wheel twine
38+
- name: Build and publish
39+
env:
40+
TWINE_USERNAME: __token__
41+
TWINE_PASSWORD: ${{ secrets.PYGOLF_TOKEN }}
42+
run: |
43+
python setup.py sdist bdist_wheel
44+
twine upload dist/*

.gitignore

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
MANIFEST
27+
28+
# PyInstaller
29+
# Usually these files are written by a python script from a template
30+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
31+
*.manifest
32+
*.spec
33+
34+
# Installer logs
35+
pip-log.txt
36+
pip-delete-this-directory.txt
37+
38+
# Unit test / coverage reports
39+
htmlcov/
40+
.tox/
41+
.coverage
42+
.coverage.*
43+
.cache
44+
nosetests.xml
45+
coverage.xml
46+
*.cover
47+
.hypothesis/
48+
.pytest_cache/
49+
50+
# Translations
51+
*.mo
52+
*.pot
53+
54+
# Django stuff:
55+
*.log
56+
local_settings.py
57+
db.sqlite3
58+
59+
# Flask stuff:
60+
instance/
61+
.webassets-cache
62+
63+
# Scrapy stuff:
64+
.scrapy
65+
66+
# Sphinx documentation
67+
docs/_build/
68+
69+
# PyBuilder
70+
target/
71+
72+
# Jupyter Notebook
73+
.ipynb_checkpoints
74+
75+
# pyenv
76+
.python-version
77+
78+
# celery beat schedule file
79+
celerybeat-schedule
80+
81+
# SageMath parsed files
82+
*.sage.py
83+
84+
# Environments
85+
.env
86+
.venv
87+
env/
88+
venv/
89+
ENV/
90+
env.bak/
91+
venv.bak/
92+
93+
# Spyder project settings
94+
.spyderproject
95+
.spyproject
96+
97+
# Rope project settings
98+
.ropeproject
99+
100+
# mkdocs documentation
101+
/site
102+
103+
# mypy
104+
.mypy_cache/

.isort.cfg

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[settings]
2+
line_length=120
3+
skip=code_example/,scripts/shorten_example.py
4+
indent=' '
5+
multi_line_output=3

CONTRIBUTING.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Contributing
2+
3+
## Issues
4+
5+
Issues are very valuable to this project.
6+
7+
* Ideas are a valuable source of contributions others can make.
8+
* Problems show where this project is lacking.
9+
* With a question you show where contributors can improve the user experience.
10+
11+
## Pull Requests
12+
13+
Pull requests are a great way to get your ideas into this repository.
14+
15+
### How to add a `Rule`
16+
17+
The new rule needs to inherit from `AstroidRule`.
18+
19+
Once the class is created, you need to make it visible in the `__init__.py` file.
20+
21+
After that you might want to add it either in `AlwaysApplyPhase` or via an `Optimizer`
22+
23+
### How to add an `Optimizer`
24+
25+
The new optimizer needs to inherit from `Optimizer`, it needs to implement method `visit_[ClassName]` to apply something on this node.
26+
27+
For instance, if you need to count the number of times a method is called, you need to implement the method `visit_Call`. Such as in [RenameOptimizer](pygolf/optimizers/rename_method_optimizer.py)
28+
29+
### Tests
30+
31+
To test you can simply use the target `check` in the [Makefile](Makefile).
32+
It will reproduce every steps in the [github workflow](.github/workflows/pythonapp.yml) to merge a PR.
33+
34+
## Thanks
35+
36+
Thank you for any contribution you might make :).

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 bluesheeptoken
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
check: test check-lint mypy check-examples
2+
3+
test:
4+
pytest
5+
6+
check-lint:
7+
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics --exclude code_example/
8+
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics --exclude code_example/,scripts/shorten_example.py
9+
black . --check --exclude code_example/ --line-length=127
10+
isort --check
11+
12+
mypy:
13+
mypy pygolf
14+
15+
check-examples:
16+
python3 scripts/shorten_example.py --check
17+
18+
lint:
19+
black . --exclude code_example/ --line-length=127
20+
isort
21+
22+
generate-examples:
23+
python3 scripts/shorten_example.py --generate

README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# PyGolf
2+
3+
PyGolf is a python code shortener.
4+
5+
PyGolf takes a valid python code as input and outputs an equivalent code with fewer characters.
6+
7+
You can find examples of reduced code in the folder [code_example](code_example). These examples mainly come from [CodinGame](https://www.codingame.com/home)'s Clash of Code game.
8+
9+
## Getting Started
10+
11+
### Installing
12+
13+
You can install this project directly from pypi with `pip install pygolf`
14+
15+
### How to use it
16+
17+
You can call it using either `pygolf` or `python -m pygolf`.
18+
19+
You can:
20+
- Give some code with `-c`, `pygolf -c "print( 2 )"`
21+
- Give an input and output file, `pygolf -i input_file -o output_file`
22+
- Shorten code in clipboard with `pygolf -cb` (usefull while doing a clash of code)
23+
24+
To modify the clipboard, `pygolf` requires [pyperclip](https://pypi.org/project/pyperclip/). You might have some issues such as `Could not find a copy/paste mechanism for your system`. If so, refer to [pyperclip guidelines](https://github.com/asweigart/pyperclip/blob/master/README.md).
25+
26+
### How does it work
27+
28+
`PyGolf` uses [astroid]() to parse and apply transformations on the abstract syntax tree (AST).
29+
30+
`PyGolf` parses and unparses the AST several times through [phases](pygolf/optimization_phases). Each phase comes with [rules](pygolf/rules).
31+
32+
If you want to contribute, please refer to the [CONTRIBUTING.md](CONTRIBUTING.md) file.

code_example/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
| example name | example length | example shortened length |
2+
| --- | --- | --- |
3+
| spongebob_meme_generator | 189 | 105 |
4+
| trolley | 207 | 113 |
5+
| number_to_base_26 | 119 | 83 |

code_example/number_to_base_26.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
n = int(input())
2+
l = ""
3+
while n > 0:
4+
l = chr(65 + n % 26) + l
5+
n //= 26
6+
if l:
7+
print(l)
8+
else:
9+
print("A")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Z=print
2+
n=int(input())
3+
l=''
4+
while n>0:l=chr(65+n%26)+l;n//=26
5+
if l:Z(l)
6+
else:Z('A')
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
_input = input()
2+
counter = 0
3+
ans = ""
4+
for x in _input:
5+
if x.isalpha():
6+
ans += x.lower() if counter % 2 else x.upper()
7+
counter += 1
8+
else:
9+
ans += x
10+
print(ans)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Z=input()
2+
Y=0
3+
X=''
4+
for x in Z:
5+
if x.isalpha():X+=x.lower()if Y%2 else x.upper();Y+=1
6+
else:X+=x
7+
print(X)

code_example/targets.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| example | target_length |
2+
| --- | --- |
3+
| number_to_base_26 | 77 |
4+
| spongebob_meme_generator | 96 |

code_example/trolley.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
n = int(input())
2+
min_val = float("inf")
3+
min_path = 0
4+
for i in range(n):
5+
q, v = [int(j) for j in input().split()]
6+
if q * v < min_val:
7+
min_val = q * v
8+
min_path = i + 1
9+
10+
print(min_path)

code_example/trolley_shorten.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
V=input
2+
n=int(V())
3+
Z=float('inf')
4+
Y=0
5+
for i in range(n):
6+
q,v=map(int,V().split())
7+
if q*v<Z:Z=q*v;Y=i+1
8+
print(Y)

mypy.ini

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[mypy]
2+
python_version = 3.7
3+
warn_return_any = True
4+
warn_unused_configs = True
5+
6+
[mypy-astroid.*]
7+
ignore_missing_imports = True

pygolf/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)