Skip to content

Commit

Permalink
Add setup.py and rearrange package structure
Browse files Browse the repository at this point in the history
- Allows installing via pip (once we register it)
- Allows using onelinerizer as a command line tool
- Allows importing onelinerize from onelinerizer
  • Loading branch information
csvoss committed May 20, 2018
1 parent 35284dd commit ca71ac5
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 73 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -1,2 +1,6 @@
*.pyc
stocks.csv
*.egg-info
build/
dist/
.ve
17 changes: 17 additions & 0 deletions .setup.py
@@ -0,0 +1,17 @@
from setuptools import setup

setup(
name="onelinerizer",
version="1.0.0",
author="Chelsea Voss",
author_email="csvoss@mit.edu",
maintainer="Chelsea Voss and Anders Kaseorg",
maintainer_email="onelinerizer@mit.edu",
url='https://github.com/csvoss/onelinerizer',
description='Convert any Python file into a single line of code.',
packages=['onelinerizer'],
entry_points={
'console_scripts': ['onelinerizer=onelinerizer.__main__:main'],
},
test_suite='onelinerizer.runtests',
)
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,4 +1,4 @@
language: python
python:
- "2.7"
script: python runtests.py
script: python setup.py test
37 changes: 31 additions & 6 deletions README.md
Expand Up @@ -13,14 +13,26 @@ No newlines allowed. No semicolons allowed, either.
[Presentation at PyCon 2016](https://www.youtube.com/watch?v=DsUxuz_Rt8g), and [slide deck](https://speakerdeck.com/pycon2016/chelsea-voss-oneliner-izer-an-exercise-in-constrained-coding).


Installation and Usage
----------------------
User Installation and Usage
---

Install via `pip` from PyPI:

```sh
$ git clone https://github.com/csvoss/onelinerizer
$ cd onelinerizer
$ python main.py target_file.py --debug
$ python target_file_ol.py
$ pip install onelinerizer
```

Use either the command line function or the Python module:

```sh
$ echo "def f(x):\n print x\nf(4)" > sandbox.py
$ onelinerizer sandbox.py --debug
$ onelinerizer sandbox_ol.py
```

```python
from onelinerizer import onelinerize
onelinerize("def f(x):\n print x\nf(4)")
```

Examples
Expand Down Expand Up @@ -150,6 +162,19 @@ Open Problems
* with
* yield

Developer Installation and Testing
---
```sh
$ git clone https://github.com/csvoss/onelinerizer
$ cd onelinerizer
$ python -m onelinerizer .setup.py setup.py
$ python setup.py test
```

To install the local module:
```sh
$ pip install .
```

Further Reading
---------------
Expand Down
Empty file removed __init__.py
Empty file.
1 change: 1 addition & 0 deletions onelinerizer/__init__.py
@@ -0,0 +1 @@
from .onelinerizer import onelinerize
66 changes: 66 additions & 0 deletions onelinerizer/__main__.py
@@ -0,0 +1,66 @@
import argparse
import sys

from .onelinerizer import onelinerize

def main():
usage = ['onelinerizer --help',
'onelinerizer [--debug] [infile.py [outfile.py]]',
]
parser = argparse.ArgumentParser(usage='\n '.join(usage),
description=("if infile is given and outfile is not, outfile will be "
"infile_ol.py"))
parser.add_argument('infile', nargs='?')
parser.add_argument('outfile', nargs='?')
parser.add_argument('--debug', action='store_true')
args = parser.parse_args()
original = None
if args.infile is None:
# I have gotten no arguments. Look at sys.stdin
original = sys.stdin.read()
outfilename = None
elif args.outfile is None:
# I have gotten one argument. If there's something to read from
# sys.stdin, read from there.
if args.infile.endswith('.py'):
outfilename = '_ol.py'.join(args.infile.rsplit(".py", 1))
else:
outfilename = args.infile + '_ol.py'
else:
outfilename = args.outfile

if original is None:
infile = open(args.infile)
original = infile.read().strip()
infile.close()
onelinerized = onelinerize(original)
if outfilename is None:
print onelinerized
else:
outfi = open(outfilename, 'w')
outfi.write(onelinerized + '\n')
outfi.close()

if args.debug:
if outfilename is None:
# redirect to sys.stderr if I'm writing outfile to sys.stdout
sys.stdout = sys.stderr
print '--- ORIGINAL ---------------------------------'
print original
print '----------------------------------------------'
scope = {}
try:
exec(original, scope)
except Exception as e:
traceback.print_exc(e)
print '--- ONELINERIZED -----------------------------'
print onelinerized
print '----------------------------------------------'
scope = {}
try:
exec(onelinerized, scope)
except Exception as e:
traceback.print_exc(e)

if __name__ == '__main__':
main()
65 changes: 2 additions & 63 deletions main.py → onelinerizer/onelinerizer.py
Expand Up @@ -10,12 +10,11 @@
one-line infile.py, put the result in outfile.py
"""

import argparse
import ast
import symtable
import sys
import traceback
from template import T

from .template import T


def lambda_function(arguments_to_values):
Expand Down Expand Up @@ -803,63 +802,3 @@ def onelinerize(original):
return original

return get_init_code(t, table)


if __name__ == '__main__':
usage = ['python main.py --help',
'python main.py [--debug] [infile.py [outfile.py]]',
]
parser = argparse.ArgumentParser(usage='\n '.join(usage),
description=("if infile is given and outfile is not, outfile will be "
"infile_ol.py"))
parser.add_argument('infile', nargs='?')
parser.add_argument('outfile', nargs='?')
parser.add_argument('--debug', action='store_true')
args = parser.parse_args()
original = None
if args.infile is None:
# I have gotten no arguments. Look at sys.stdin
original = sys.stdin.read()
outfilename = None
elif args.outfile is None:
# I have gotten one argument. If there's something to read from
# sys.stdin, read from there.
if args.infile.endswith('.py'):
outfilename = '_ol.py'.join(args.infile.rsplit(".py", 1))
else:
outfilename = args.infile + '_ol.py'
else:
outfilename = args.outfile

if original is None:
infile = open(args.infile)
original = infile.read().strip()
infile.close()
onelinerized = onelinerize(original)
if outfilename is None:
print onelinerized
else:
outfi = open(outfilename, 'w')
outfi.write(onelinerized + '\n')
outfi.close()

if args.debug:
if outfilename is None:
# redirect to sys.stderr if I'm writing outfile to sys.stdout
sys.stdout = sys.stderr
print '--- ORIGINAL ---------------------------------'
print original
print '----------------------------------------------'
scope = {}
try:
exec(original, scope)
except Exception as e:
traceback.print_exc(e)
print '--- ONELINERIZED -----------------------------'
print onelinerized
print '----------------------------------------------'
scope = {}
try:
exec(onelinerized, scope)
except Exception as e:
traceback.print_exc(e)
3 changes: 2 additions & 1 deletion runtests.py → onelinerizer/runtests.py
Expand Up @@ -3,7 +3,8 @@
import random
import sys
from StringIO import StringIO
from main import onelinerize

from .onelinerizer import onelinerize

TEST_DIRECTORY = 'tests'

Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions setup.py
@@ -0,0 +1 @@
(lambda __g: (lambda __mod: [(setup(name='onelinerizer', version='1.0.0', author='Chelsea Voss', author_email='csvoss@mit.edu', maintainer='Chelsea Voss and Anders Kaseorg', maintainer_email='onelinerizer@mit.edu', url='https://github.com/csvoss/onelinerizer', description='Convert any Python file into a single line of code.', packages=['onelinerizer'], entry_points={'console_scripts': ['onelinerizer=onelinerizer.__main__:main']}, test_suite='onelinerizer.runtests'), None)[1] for __g['setup'] in [(__mod.setup)]][0])(__import__('setuptools', __g, __g, ('setup',), 0)))(globals())
2 changes: 0 additions & 2 deletions unittest.sh

This file was deleted.

0 comments on commit ca71ac5

Please sign in to comment.