Skip to main content

Execute do-files with Stata and pytask.

Project description

pytask-stata

PyPI PyPI - Python Version image image PyPI - License image image pre-commit.ci status image


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(script="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 where the script is located. This allows you, for example, to reference every data set you want to read with a relative path from the script.

Dependencies and Products

Dependencies and products can be added as with a normal pytask task using the @pytask.mark.depends_on and @pytask.mark.produces decorators. which is explained in this tutorial.

Accessing dependencies and products in the script

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(script="script.do", options="auto.dta")
@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.

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(script="script.do", options=BLD / "auto.dta")
@pytask.mark.produces(BLD / "auto.dta")
def task_run_do_file():
    pass

Repeating tasks with different scripts or inputs

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.

for i in range(2):

    @pytask.mark.task
    @pytask.mark.stata(script=f"script_{i}.do", options=f"{i}.dta")
    @pytask.mark.produces(f"{i}.dta")
    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

[tool.pytask.ini_options]
stata_keep_log = true

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

[tool.pytask.ini_options]
stata_check_log_lines = 10

and here via the command line interface

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

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.5.0.tar.gz (8.0 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.5.0-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytask_stata-0.5.0.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pytask_stata-0.5.0.tar.gz
Algorithm Hash digest
SHA256 2044f8d68200dd085fb14f220d0387a1c783d9575d69cd7e744b9578c0ad68e2
MD5 36fcb5b64b1158f5a384864bef8db69c
BLAKE2b-256 cf684d5a5fa5197f5388d014e495a68d9b0232b2000d251534a62dbd64454d37

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytask_stata-0.5.0.tar.gz:

Publisher: publish-to-pypi.yml on pytask-dev/pytask-stata

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: pytask_stata-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pytask_stata-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 53cd0e809c3ae0a108ca8e3b1296647469181f3baf8ec067e6879f9b0bf6a5ec
MD5 c710700ad169ddfb9f0d6b52269a6d36
BLAKE2b-256 255eae55a94fe8cc992b85fb9d80efeffcae19efdcf9ec1fa8241696c1b04299

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytask_stata-0.5.0-py3-none-any.whl:

Publisher: publish-to-pypi.yml on pytask-dev/pytask-stata

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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