Package: datatask
General-purpose data structure and representation format for tasks (stand-alone or part of a larger data workflow) that involve multiple data resources.PurposeThis library provides a data structure and format for representing individual tasks or steps (within a larger data workflow) that involve one or more resources containing tabular data. Data resource paths and URIs can be associated with more concise resource names (calledreferences), and these references can then be used to specify task inputs and outputs (including their schemas). This library isnotintended to act or to be used as a query language, though a collection of individual tasks may constitute (or may be included within) a larger query or data workflow graph.Installation and UsageThis library is available as apackage on PyPI:python -m pip install datataskThe library can be imported in the usual ways:import datatask
from datatask import *ExamplesThis library makes it possible to define the input and output data resources involved in a data task:>>> from datatask import datatask
>>> dt = datatask({"inputs": ["transactions.csv"], "outputs": ["report.csv"]})Thedatataskclass is derived fromdict, making conversion to JSON straightforward (using either the built-injsonlibrary or the wrapper methods presented below):>>> dt.to_json()
'{"inputs": ["transactions.csv"], "outputs": ["report.csv"]}'
>>> datatask.from_json(
...     '{"inputs": ["transactions.csv"], "outputs": ["report.csv"]}'
... )
{'inputs': ['transactions.csv'], 'outputs': ['report.csv']}Typically, a data resource is a string consisting of a file path or a URI:>>> dt = datatask({
...    "inputs": [
...        "https://example.com/inventory.csv",
...        "https://example.com/transactions.csv"
...    ],
...    "outputs": [
...        "/home/user/report.csv"
...    ]
... })It is also possible to specify concise names, orreferences, for data resources. These references can then be used to specify input and output data resources:>>> dt = datatask({
...    "resources": {
...        "inv": "https://example.com/inventory.csv",
...        "rep": "/home/user/report.csv"
...    },
...    "inputs": ["inv"],
...    "outputs": ["rep"]
... })Each output data resource can be associated with its schema (which must be a list of dictionaries). In the example below, the task indicates that the output data resource’s schema has two columns (the first and second columns found in theinvinput data resource):>>> dt = datatask({
...    "resources": {
...        "inv": "https://example.com/inventory.csv",
...        "rep": "/home/user/report.csv"
...    },
...    "inputs": ["inv"],
...    "outputs": {"rep": [{"inv": 0}, {"inv": 1}]}
... })It is also possible to specify the schema (an ordered list of column names) of an input data resource, and then to reference individual columns in the input data resource using that schema:>>> dt = datatask({
...    "resources": {
...        "inv": "https://example.com/data.csv",
...        "rep": "/home/user/report.csv"
...    },
...    "inputs": {"inv": ["item", "quantity", "price"]},
...    "outputs": {"rep": [{"inv": "item"}, {"inv": "quantity"}]}
... })Within bothinputsandoutputsentries, each reference can be associated with aspecificationrather than a schema. This specification can optionally contain the schema. The below instance is semantically equivalent to the example immediately above:>>> dt = datatask({
...    "inputs": {
...        "https://example.com/inventory.csv": {
...            "schema": ["item", "quantity", "price"]
...        }
...    },
...    "outputs": {
...        "/home/user/report.csv": {
...            "schema": [
...                {"https://example.com/inventory.csv": "item"},
...                {"https://example.com/inventory.csv": "quantity"}
...            ]
...        }
...    }
... })A specification can also optionally contain aheaderattribute associated with a boolean value. This can be used to indicate whether a data resource has a header row. If aheaderattribute is not present, it is by default assumed that the data resource has no header row:>>> dt = datatask({
...    "inputs": {
...        "https://example.com/inventory.csv": {
...            "schema": ["item", "quantity", "price"],
...            "header": True
...        }
...    },
...    "outputs": {
...        "/home/user/report.csv": {
...            "schema": [
...                {"https://example.com/inventory.csv": "item"},
...                {"https://example.com/inventory.csv": "quantity"}
...            ],
...            "header": False
...        }
...    }
... })RecommendationsThis subsection presents recommended patterns for a few common task types. These recommendations are not enforced by the library.Especially in larger instances or in instances that may be automatically processed (e.g., to perform expansion of references into their corresponding data resource paths or URIs), it may be useful to explicitly distinguish reference strings using a special character:>>> dt = datatask({
...    "resources": {
...        "@inv": "https://example.com/inventory.csv",
...        "@rep": "/home/user/report.csv"
...    },
...    "inputs": {"@inv": ["item", "quantity", "price"]},
...    "outputs": {"@rep": [{"@inv": "item"}, {"@inv": "quantity"}]}
... })To specify the column names within an output schema, nested dictionaries of the form{"column_name": ... }can be used:>>> dt = datatask({
...    "resources": {
...        "@inv": "https://example.com/data.csv",
...        "@rep": "/home/user/report.csv"
...    },
...    "inputs": {"@inv": ["item", "quantity", "price"]},
...    "outputs": {
...        "@rep": {
...            "schema": [
...                {"product": {"@inv": "item"}},
...                {"remaining": {"@inv": "quantity"}}
...            ],
...            "header": True
...        }
...    }
... })To indicate that the values of a particular column in an output schema are computed by applying an operator to one or more column values from an input data resource, nested dictionaries of the form{"$operation_name":... }can be used:>>> dt = datatask({
...    "resources": {
...        "@inv": "https://example.com/data.csv",
...        "@rep": "/home/user/report.csv"
...    },
...    "inputs": {"@inv": ["item", "quantity", "price"]},
...    "outputs": {
...        "@rep": {
...            "schema": [
...                {"item": {"@inv": "item"}},
...                {"cost": {"$mul": [{"@inv": "quantity"}, {"@inv": "price"}]}},
...            ],
...            "header": True
...        }
...    }
... })DevelopmentAll installation and development dependencies are fully specified inpyproject.toml. Theproject.optional-dependenciesobject is used tospecify optional requirementsfor various development tasks. This makes it possible to specify additional options (such asdocs,lint, and so on) when performing installation usingpip:python -m pip install .[docs,lint]DocumentationThe documentation can be generated automatically from the source files usingSphinx:python -m pip install .[docs]
cd docs
sphinx-apidoc -f -E --templatedir=_templates -o _source .. && make htmlTesting and ConventionsAll unit tests are executed and their coverage is measured when usingpytest(see thepyproject.tomlfile for configuration details):python -m pip install .[test]
python -m pytestAlternatively, all unit tests are included in the module itself and can be executed usingdoctest:python src/datatask/datatask.py -vStyle conventions are enforced usingPylint:python -m pip install .[lint]
python -m pylint src/datataskContributionsIn order to contribute to the source code, open an issue or submit a pull request on theGitHub pagefor this library.VersioningThe version number format for this library and the changes to the library associated with version number increments conform withSemantic Versioning 2.0.0.PublishingThis library can be published as apackage on PyPIby a package maintainer. First, install the dependencies required for packaging and publishing:python -m pip install .[publish]Ensure that the correct version number appears inpyproject.toml, and that any links in this README document to the Read the Docs documentation of this package (or its dependencies) have appropriate version numbers. Also ensure that the Read the Docs project for this library has anautomation rulethat activates and sets as the default all tagged versions. Create and push a tag for this version (replacing?.?.?with the version number):git tag ?.?.?
git push origin ?.?.?Remove any old build/distribution files. Then, package the source into a distribution archive:rm -rf build dist src/*.egg-info
python -m build --sdist --wheel .Finally, upload the package distribution archive toPyPI:python -m twine upload dist/*