Skip to content

Instantly share code, notes, and snippets.

@puterleat
Last active December 22, 2015 21:39
Show Gist options
  • Save puterleat/6535136 to your computer and use it in GitHub Desktop.
Save puterleat/6535136 to your computer and use it in GitHub Desktop.
A shell utility (in python) to allow piping and redirection of stata commands and output.
#!/usr/bin/python
"""
For example:
echo "di 2^2" | statpipe
. di 2^2
4
Depends on envoy (run: pip install envoy).
To install statpipe, just put it on your path somewhere and make it executable.
Free to use under any license you want.
"""
import os
import envoy
import sys
STATA_EXECUTABLE = "/Applications/Stata/Stata.app/Contents/MacOS/Stata"
code = sys.stdin.read()
tmpdo = "/tmp/statpipe.do"
tmplog = "statpipe.log"
with open(tmpdo, 'w') as f:
f.write( code + "\n")
result = envoy.run('{} -q -b -e do {}'.format(STATA_EXECUTABLE, tmpdo))
with open(tmplog, 'r') as f:
# delete some crufty extra spaces
log = "\n".join(f.readlines()[2:-4])
map(os.remove, [tmpdo, tmplog])
sys.stdout.write(log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment