Skip to content

Instantly share code, notes, and snippets.

@apoorvalal
Last active May 3, 2017 22:27
Show Gist options
  • Save apoorvalal/f76b177738f324222da9f77874d761f5 to your computer and use it in GitHub Desktop.
Save apoorvalal/f76b177738f324222da9f77874d761f5 to your computer and use it in GitHub Desktop.
import os, itertools
from subprocess import run
from multiprocessing.dummy import Pool
from functools import partial
def set_globs(stexec,scname):
global script_name, stata_exec
stata_exec = stexec
script_name = scname
def create_command_list(*args):
commands = []
combinations = list(itertools.product(*args))
for combination in combinations:
arguments = " ".join(combination)
command = stata_exec + ' /e /q do ' + script_name + ' ' + arguments
commands.append(command)
return commands
def run_do_file(
script_location,
commands,
script_name = 'caller.do',
nprocesses=2,
keeplog = False):
os.chdir(script_location)
pool = Pool(nprocesses)
for i, returncode in enumerate(pool.imap(partial(run,shell=True),commands)):
print(i, 'command executed, Returned', returncode)
if __name__ == '__main__':
stexec = 'StataMP-64' # short exec because Stata is already in my Path, else wrap in quotes on windows
scname = 'caller.do'
set_globs(stexec,scname)
script_location = r'C:\Users\alal\Desktop\Research\temp'
files = ['all','sub1','sub2']
yvars = ['life_expectancy','income']
xvars = ['1','2','3']
commands = create_command_list(files,yvars,xvars)
print(commands)
run_do_file(script_location, commands, script_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment