Skip to main content

An provenance tracking library for simple Python workflows

Project description

makeprov: Pythonic Provenance Tracking

This library provides a way to track file provenance in Python workflows using PROV (W3C Provenance) semantics. Decorators declare inputs and outputs, provenance is written automatically, and templated targets can be resolved on demand.

Features

  • Use decorators to define rules for workflows.
  • Resolve templated targets (results/{sample}.txt) via parse-style patterns.
  • Support phony/meta rules for orchestration alongside file-producing rules.
  • Automatically generate RDF-based provenance metadata (rdflib optional).
  • Handles input and output streams.
  • Integrates with Python's type hints for easy configuration.
  • Outputs provenance data in TRIG format if rdflib is installed; otherwise outputs json-ld.

Installation

You can install the module directly from PyPI:

pip install makeprov

Usage

Here’s an example of how to use this package in your Python scripts:

from makeprov import rule, InPath, OutPath, build

@rule()
def process_data(
    sample: int | None = None,
    input_file: InPath = InPath('data/{sample:d}.txt'),
    output_file: OutPath = OutPath('results/{sample:d}.txt')
):
    with input_file.open('r') as infile, output_file.open('w') as outfile:
        data = infile.read()
        outfile.write(data.upper())

if __name__ == '__main__':
    # Build a specific templated target and its prerequisites
    from makeprov import build
    build('results/1.txt')

    # Or expose rules via a command line interface
    import defopt
    defopt.run(process_data)

You can execute example.py via the CLI like so:

python example.py build-all

# Or set configuration through the CLI
python example.py build-all --conf='{"base_iri": "http://mybaseiri.org/", "prov_dir": "my_prov_directory"}' --force --input_file input.txt --output_file final_output.txt

# Or set configuration through a TOML file
python example.py build-all -c @my_config.toml

# Inspect dependency resolution without executing rules
python example.py --explain results/1.txt
python example.py --to-dot results/1.txt

Complex CSV-to-RDF Workflow

For a more involved scenario, see complex_example.py. It creates multiple CSV files, aggregates their contents, and emits an RDF graph that is both serialized to disk and embedded into the provenance dataset because the function returns an rdflib.Graph.

@rule()
def export_totals_graph(
    totals_csv: InPath = InPath("data/region_totals.csv"),
    graph_ttl: OutPath = OutPath("data/region_totals.ttl"),
) -> Graph:
    graph = Graph()
    graph.bind("sales", SALES)

    with totals_csv.open("r", newline="") as handle:
        for row in csv.DictReader(handle):
            region_key = row["region"].lower().replace(" ", "-")
            subject = SALES[f"region/{region_key}"]

            graph.add((subject, RDF.type, SALES.RegionTotal))
            graph.add((subject, SALES.regionName, Literal(row["region"])))
            graph.add((subject, SALES.totalUnits, Literal(row["total_units"], datatype=XSD.integer)))
            graph.add((subject, SALES.totalRevenue, Literal(row["total_revenue"], datatype=XSD.decimal)))

    with graph_ttl.open("w") as handle:
        handle.write(graph.serialize(format="turtle"))

    return graph

Run the entire workflow, including CSV generation and RDF export, with:

python complex_example.py build-sales-report

Bundling nested provenance and directory outputs

Rules can merge the provenance from any rules they invoke by passing merge=True to makeprov.rule. Pair this with makeprov.OutDir to declare a directory and then materialize multiple outputs beneath it while keeping them linked to a single provenance record. Use makeprov.InDir for the same tracked-directory semantics on inputs. See merge_outdir_example.py for an example.

Configuration

You can customize the provenance tracking with the following options:

  • base_iri (str): Base IRI for new resources
  • prov_dir (str): Directory for writing PROV .json-ld or .trig files
  • force (bool): Force running of dependencies
  • dry_run (bool): Only check workflow, don't run anything

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

makeprov-0.4.2.tar.gz (26.9 kB view details)

Uploaded Source

Built Distribution

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

makeprov-0.4.2-py3-none-any.whl (23.4 kB view details)

Uploaded Python 3

File details

Details for the file makeprov-0.4.2.tar.gz.

File metadata

  • Download URL: makeprov-0.4.2.tar.gz
  • Upload date:
  • Size: 26.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.13

File hashes

Hashes for makeprov-0.4.2.tar.gz
Algorithm Hash digest
SHA256 cd9ebcb51185f0ffc255d63f888b1c9ee8f93375de10f9e5d8444393f028aec9
MD5 3d854204513771586b5fc2a11ae134c7
BLAKE2b-256 9cb6f4d363359549c0a3c25fc9f0424783a0e62b1c641820b9d69fd7173dfc73

See more details on using hashes here.

File details

Details for the file makeprov-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: makeprov-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 23.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.13

File hashes

Hashes for makeprov-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 412431f00d391b0167fbe0b4ea463c623cbeac32abac57789b31ddb0815f56fd
MD5 fec8ff80bfba9f80d72950e960b5f1bf
BLAKE2b-256 dfafe38b027fac4783c17b897f235a38d09648b7474c0beb0c6a9313c6758244

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