API reference

dawgtools

Tools for DAWG queries and data processing

This script is intended to be run using uv (https://github.com/astral-sh/uv); follow instructions to install if you don’t have it already. Recommended method for macos and linux is

dawgtools.package_data(fname, pattern=None)[source]

Return the absolute path to a file included in package data, raising ValueError if no such file exists. If pattern is provided, return a list of matching files in package data (ignoring fname).

dawgtools.main

Assembles subcommands and provides top-level script.

class dawgtools.main.MyRawDescriptionHelpFormatter(prog, indent_increment=2, max_help_position=24, width=None)[source]

Subclass RawDescriptionHelpFormatter to prevent duplication of choice list when nargs is ‘*’ or ‘+’.

dawgtools.main.parse_arguments(argv)[source]

Create the argument parser

dawgtools.db

dawgtools.db.as_dicts(headers: list, rows: list)[source]

Converts a list of rows and headers into a list of dictionaries

dawgtools.db.create_and_load_temp_table(cursor, sql_cmd: str, rows: list)[source]

Create and load a temporary table using the provided schema and data files. Rows is a list of dicts.

dawgtools.db.render_template(template: str, params: dict) tuple[str, list][source]

Renders a query template that uses a combination of python string formatting directives and jinja2 expressions using the given parameters. Returns a tuple of the modified template with “?” placeholders and a list of positional parameters.

dawgtools.db.sql_query(query: str, params: dict | None = None, callback: LambdaType | None = None) tuple[list, list][source]

Executes a SQL query using the given parameters.

Uses jinjasql to render the query and perform parameter substitution. Optional callable object ‘callback’ with a single argument ‘cursor’ will be executed before the query.

Returns a tuple (headers, rows)

dawgtools.utils

class dawgtools.utils.MyJSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]
default(obj)[source]

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return super().default(o)