Skip to content

format_partition_key

kedro_dagster.utils.format_partition_key(partition_key)

Format a partition key into a Dagster-safe suffix (^[A-Za-z0-9_]+$).

Parameters

Name Type Description Default
partition_key Any

Partition key to serialize.

required

Returns

Type Description
str

Serialized partition key.

See Also

kedro_dagster.utils.format_dataset_name : Formats dataset names under the same naming convention.

Source Code

Show/Hide source
def format_partition_key(partition_key: Any) -> str:
    """Format a partition key into a Dagster-safe suffix (``^[A-Za-z0-9_]+$``).

    Parameters
    ----------
    partition_key : Any
        Partition key to serialize.

    Returns
    -------
    str
        Serialized partition key.

    See Also
    --------
    `kedro_dagster.utils.format_dataset_name` :
        Formats dataset names under the same naming convention.
    """
    dagster_partition_key = re.sub(r"[^A-Za-z0-9_]", "_", partition_key)
    dagster_partition_key = dagster_partition_key.strip("_")

    if not dagster_partition_key:
        raise ValueError(f"Partition key `{partition_key}` cannot be formatted into a valid Dagster key.")
    return dagster_partition_key