Skip to content

Snippets

rpguiteras edited this page May 27, 2026 · 6 revisions

Python dictionary

How to create several similar objects using a Python for loop and a dictionary or dict object (for "dictionary"). Here, the dict object is main_specs_estim:


# define set of values to loop over
HCH_specs = ["noCovars","interact", "density"]

# create empty dict object
HCH_specs_estim = {}

# fill out the entries in HCH_specs_estim 
for SPEC in HCH_specs:
    HCH_specs_estim[SPEC] = env.StataBuild(
        target = [
            f'output/sters/gmm-HCH-{SPEC}.sters',
            f'output/data/estimation/gmm-HCH-{SPEC}-hh.dta'
        ],
        source = f'code/gmm-2step-HCH-{SPEC}.do',
        depends = [dataprep, GMM_ado]
    )
    
# create a variable with all entries 
HCH_specs_estim_all = list(HCH_specs_estim.values())

# use as dependency for next target 
HCH_tabfig = env.StataBuild(
      target = ['output/tab/tex/gmm-HCH.tex','output/tab/tex/gmm-HCH-notes.tex'],
      source = 'code/gmm-2step-HCH-tabfig.do',
      depends = [HCH_specs_estim_all]
)

If we wanted to refer to just one of the entries, we would use the key, e.g., HCH_specs_estim[noCovars]

R Builder

A simple builder (doesn't clean-up the log file, and if run in parallel it should output to different log files)

env.Append(BUILDERS={'RBuilder': Builder(action='Rscript --vanilla $SOURCE > out.log')})

Usage example: code_bld_obj = env.RBuilder(target="output_file", source="code.R")

Python Builder

A simple builder (doesn't clean-up the log file, and if run in parallel it should output to different log files)

env.Append(BUILDERS={'PyBuilder': Builder(action='python $SOURCE > out.log')})

Usage example: code_bld_obj = env.PyBuilder(target="output_file", source="code.py")

PHONY target

These are targets that don't correspond to built files. They are used just to run arbitrary commands (that don't fit well into the build-a-file mentality). The following executes dir when someone specifies the target show_dir. "dir" could be replaced with a real python function

env.AlwaysBuild(env.Alias("show_dir", action="dir"))

Loading dependencies from a JSON file

This is a way to store you dependencies in a simple data structure and loaded programmatically in your SConstruct

You can save the following to a file, e.g., deps.json

[["a.do", ["i1", "i2"], ["o1", "o2"]], ["b.do", ["i3"], ["o4"]]]

Then in your SConstruct you can do this:

def deps_from_json(fname):
    import json
    with open(fname, "r") as fhandle:
        return json.load(fhandle)

# dep_list = deps_from_json("deps.json")
#Generate builder instances from list
for do_file, inputs, outputs from dep_list:
    env.StataBuild(target = outputs, source = do_file, depends=inputs)

...

Clone this wiki locally