Skip to main content

Execute do-files with Stata and pytask.

Project description

PyPI PyPI - Python Version https://anaconda.org/pytask/pytask-stata/badges/version.svg https://anaconda.org/pytask/pytask-stata/badges/platforms.svg PyPI - License https://github.com/pytask-dev/pytask-stata/workflows/Continuous%20Integration%20Workflow/badge.svg?branch=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 config --add channels conda-forge --add channels pytask
$ conda install 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.0.6.tar.gz (27.8 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.0.6-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file pytask-stata-0.0.6.tar.gz.

File metadata

  • Download URL: pytask-stata-0.0.6.tar.gz
  • Upload date:
  • Size: 27.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.9.2

File hashes

Hashes for pytask-stata-0.0.6.tar.gz
Algorithm Hash digest
SHA256 5e9f0f13c8e58d7a48498985b2d2606beac1f8e16187cf29023c54fb8700d076
MD5 4792a78f4fdba7082386d165bc081cbe
BLAKE2b-256 6ed9756877ba6f50125ace6ce134a82d9e12aa75893ee3abcef7764e1a3d4039

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pytask_stata-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.9.2

File hashes

Hashes for pytask_stata-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 5474629d1656a0ac0fa351e5b4576fc222c5d02a2d35877a979cefb3ae0710bd
MD5 831ac72e899b2faf772d487d1259c9a3
BLAKE2b-256 dc10fdf75aaef4e9cfa9f2d20b8964a32f6af65f8e5551af321c716fd3f953b9

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