Skip to main content

Execute do-files with Stata and pytask.

Project description

PyPI PyPI - Python Version https://img.shields.io/conda/vn/conda-forge/pytask-stata.svg https://img.shields.io/conda/pn/conda-forge/pytask-stata.svg PyPI - License https://img.shields.io/github/workflow/status/pytask-dev/pytask-stata/Continuous%20Integration%20Workflow/main https://codecov.io/gh/pytask-dev/pytask-stata/branch/main/graph/badge.svg pre-commit.ci status https://img.shields.io/badge/code%20style-black-000000.svg

pytask-stata

Run Stata’s do-files with pytask.

Installation

pytask-stata is available on PyPI and Anaconda.org. Install it with

$ pip install pytask-stata

# or

$ conda install -c conda-forge pytask-stata

You also need to have Stata installed on your system and have the executable on your system’s PATH. If you do not know how to do it, here is an explanation.

Usage

Similarly to normal task functions which execute Python code, you define tasks to execute scripts written in Stata with Python functions. The difference is that the function body does not contain any logic, but the decorator tells pytask how to handle the task.

Here is an example where you want to run script.do.

import pytask


@pytask.mark.stata
@pytask.mark.depends_on("script.do")
@pytask.mark.produces("auto.dta")
def task_run_do_file():
    pass

When executing a do-file, the current working directory changes to the directory of the script which is executed.

Multiple dependencies and products

What happens if a task has more dependencies? Using a list, the do-file which should be executed must be found in the first position of the list.

@pytask.mark.stata
@pytask.mark.depends_on(["script.do", "input.dta"])
@pytask.mark.produces("output.dta")
def task_run_do_file():
    pass

If you use a dictionary to pass dependencies to the task, pytask-stata will, first, look for a "source" key in the dictionary and, secondly, under the key 0.

@pytask.mark.depends_on({"source": "script.do", "input": "input.dta"})
def task_run_do_file():
    pass


# or


@pytask.mark.depends_on({0: "script.do", "input": "input.dta"})
def task_run_do_file():
    pass


# or two decorators for the function, if you do not assign a name to the input.


@pytask.mark.depends_on({"source": "script.do"})
@pytask.mark.depends_on("input.dta")
def task_run_do_file():
    pass

Command Line Arguments

The decorator can be used to pass command line arguments to your Stata executable. For example, pass the path of the product with

@pytask.mark.stata("auto.dta")
@pytask.mark.depends_on("script.do")
@pytask.mark.produces("auto.dta")
def task_run_do_file():
    pass

And in your script.do, you can intercept the value with

* Intercept command line argument and save to macro named 'produces'.
args produces

sysuse auto, clear
save "`produces'"

The relative path inside the do-file works only because the pytask-stata switches the current working directory to the directory of the do-file before the task is executed. This is necessary precaution.

To make the task independent from the current working directory, pass the full path as an command line argument. Here is an example.

# Absolute path to the build directory.
from src.config import BLD


@pytask.mark.stata(BLD / "auto.dta")
@pytask.mark.depends_on("script.do")
@pytask.mark.produces(BLD / "auto.dta")
def task_run_do_file():
    pass

Parametrization

You can also parametrize the execution of scripts, meaning executing multiple do-files as well as passing different command line arguments to the same do-file.

The following task executes two do-files which produce different outputs.

@pytask.mark.stata
@pytask.mark.parametrize(
    "depends_on, produces", [("script_1.do", "1.dta"), ("script_2.do", "2.dta")]
)
def task_execute_do_file():
    pass

If you want to pass different command line arguments to the same do-file, you have to include the @pytask.mark.stata decorator in the parametrization just like with @pytask.mark.depends_on and @pytask.mark.produces.

@pytask.mark.depends_on("script.do")
@pytask.mark.parametrize(
    "produces, stata",
    [("output_1.dta", ("1",)), ("output_2.dta", ("2",))],
)
def task_execute_do_file():
    pass

Configuration

pytask-stata can be configured with the following options.

stata_keep_log

Use this option to keep the .log files which are produced for every task. This option is useful to debug Stata tasks. Set the option via the configuration file with

stata_keep_log = (True|true|1|False|false|0)

The option is also available in the command line interface via the --stata-keep-log flag.

stata_check_log_lines

Use this option to vary the number of lines in the log file which are checked for error codes. It also controls the number of lines displayed on errors. Use any integer greater than zero. Here is the entry in the configuration file

stata_check_log_lines = 10

and here via the command line interface

$ pytask build --stata-check-log-lines 10
stata_source_key

If you want to change the name of the key which identifies the do file, change the following default configuration in your pytask configuration file.

stata_source_key = source

Changes

Consult the release notes to find out about what is new.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytask_stata-0.1.0.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytask_stata-0.1.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file pytask_stata-0.1.0.tar.gz.

File metadata

  • Download URL: pytask_stata-0.1.0.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for pytask_stata-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cc1c117ca280d834daaa13490203cdd16e918b87564fd6521dbe6ec05c647ff6
MD5 64d8b345eec172919dc413e5cc1547f7
BLAKE2b-256 0f65e6d8d5984937cfe586283d6b3550f5335bb68f74d32e65c5454605f05aa0

See more details on using hashes here.

File details

Details for the file pytask_stata-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pytask_stata-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for pytask_stata-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 32dbc7cb7aa0386e57ca1e7fcdf5761b3535a045b464653417bd6b9015bc9a39
MD5 d7a8a573f7d9ad6c433ea7e439210564
BLAKE2b-256 d34855490352db91f07fbadcea1311961fb37d2255c633d82dc85eccd7e45ad1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page