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.1.tar.gz (13.6 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.1-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytask_stata-0.1.1.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for pytask_stata-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2f113368d3986f9a6fbb63b5a304644767e2bcede0825b142015ee06d0e89964
MD5 bab994baf3c74e2540a767da0e6d086d
BLAKE2b-256 0c049eb2924ebba777381423ef5337f74b02d005721e89029bc6f01b333e599e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pytask_stata-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for pytask_stata-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4dc58d4ef963fd20c82fb82bd6d37c80a6689eb91221414b0e3a724a5fa51ee0
MD5 016801998abddc893df6900c3c59c6e8
BLAKE2b-256 d3a2f73bc0037f0bc7c0b0b5dc5a6bc99ef1f34ef5b26ed23428971260d0d8fa

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