Execute do-files with Stata and pytask.
Project description
pytask-stata
Run Stata's do-files with pytask.
Installation
pytask-stata is available on PyPI and Anaconda.org. Install it with
$ uv add pytask-stata
# or
$ pixi add 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.
from pathlib import Path
from pytask import mark
@mark.stata(script=Path("script.do"))
def task_run_do_file(produces: Path = Path("auto.dta")):
pass
When executing a do-file, the current working directory changes to the directory where the task module is located. This allows you, for example, to reference data sets with a relative path from the task module.
Dependencies and Products
Dependencies and products can be added as with a normal pytask task using the task function signature as explained in this tutorial.
Here is a task with one dependency and one product.
from pathlib import Path
from pytask import mark
@mark.stata(script=Path("script.do"))
def task_run_do_file(
depends_on: Path = Path("input.dta"),
produces: Path = Path("auto.dta"),
):
pass
pytask uses input.dta and auto.dta to decide whether the task needs to run. If
input.dta changes or auto.dta is missing, the Stata task is executed again.
You can also use @task(kwargs=...) to define dependencies which are not part of the
function signature.
from pathlib import Path
from pytask import mark
from pytask import task
@task(kwargs={"depends_on": Path("input.dta")})
@mark.stata(script=Path("script.do"))
def task_run_do_file(produces: Path = Path("auto.dta")):
pass
Accessing dependencies and products in the script
Dependencies and products registered in the task function signature are used by pytask
to order tasks and track whether they are up-to-date. They are not automatically passed
to the Stata script. Use the options argument of the decorator to pass paths or other
values as command line arguments to your Stata executable.
For example, pass paths for the dependency and product with
from pathlib import Path
from pytask import mark
@mark.stata(script=Path("script.do"), options=[Path("input.dta"), Path("auto.dta")])
def task_run_do_file(
depends_on: Path = Path("input.dta"),
produces: Path = Path("auto.dta"),
):
pass
And in your script.do, you can intercept the value with
* Intercept command line arguments and save them to macros.
args depends_on produces
sysuse auto, clear
save "`produces'"
The relative path inside the do-file works only because pytask-stata switches the current working directory to the directory of the task module 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 pathlib import Path
from pytask import mark
from src.config import BLD
@mark.stata(script=Path("script.do"), options=BLD / "auto.dta")
def task_run_do_file(produces: Path = BLD / "auto.dta"):
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.
from pathlib import Path
from pytask import mark
from pytask import task
for i in range(2):
@task
@mark.stata(script=Path(f"script_{i}.do"), options=f"{i}.dta")
def task_execute_do_file(produces: Path = Path(f"{i}.dta")):
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytask_stata-0.5.1.tar.gz.
File metadata
- Download URL: pytask_stata-0.5.1.tar.gz
- Upload date:
- Size: 9.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab1d0e9294fc1303dc0e0cdc5e349a293d6abc7330c2fa7dd12fa91d62ef7423
|
|
| MD5 |
42459ecb0115d7d79a63f0defab43927
|
|
| BLAKE2b-256 |
6117681178ef0f46414df53ef7af1831b4abf20df2c242d2260653b6166834a6
|
File details
Details for the file pytask_stata-0.5.1-py3-none-any.whl.
File metadata
- Download URL: pytask_stata-0.5.1-py3-none-any.whl
- Upload date:
- Size: 10.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d99e9b2474c59c140abfa9062826b5e794dbf7d6c8ae1a4daaaad9b11ede0e12
|
|
| MD5 |
baecca1a144bfbbd7c94cc90929bcb41
|
|
| BLAKE2b-256 |
9fc64416def635538f39efc2e3ccfa4521d0559cae1f41f780888fff884de96c
|