Skip to content

serialization

load_from_dict(template)

Reads a template (a class serialized) and returns a the instance contained.

Parameters:

Name Type Description Default
template Dict[str, Any]

Dict containing the template, the dict serialized.

required

Returns:

Type Description
Generic[T]

Generic[T]: Instance contained in the template

Source code in src/distilabel/utils/serialization.py
def load_from_dict(template: Dict[str, Any]) -> Generic[T]:
    """Reads a template (a class serialized) and returns a the instance
    contained.

    Args:
        template (Dict[str, Any]): Dict containing the template, the dict serialized.

    Returns:
        Generic[T]: Instance contained in the template
    """
    type_info = template.pop("__type_info__")
    mod = importlib.import_module(type_info["module"])
    cls = getattr(mod, type_info["name"])
    instance = cls(**template)
    return instance

load_task_from_disk(path)

Loads a task from disk.

Parameters:

Name Type Description Default
path Path

The path to the task.

required

Returns:

Name Type Description
Task Task

The task.

Source code in src/distilabel/utils/serialization.py
def load_task_from_disk(path: Path) -> "Task":
    """Loads a task from disk.

    Args:
        path: The path to the task.

    Returns:
        Task: The task.
    """
    task_path = path / TASK_FILE_NAME
    if not task_path.exists():
        raise FileNotFoundError(f"The task file does not exist: {task_path}")

    task_content = read_json(task_path)
    task = load_from_dict(task_content)
    return task

read_json(filename)

Read a json file from disk.

Parameters:

Name Type Description Default
filename Path

Name of the json file.

required

Returns:

Type Description
Dict[str, Any]

Dict[str, Any]: Dict containing the json data.

Source code in src/distilabel/utils/serialization.py
def read_json(filename: Path) -> Dict[str, Any]:
    """Read a json file from disk.

    Args:
        filename (Path): Name of the json file.

    Returns:
        Dict[str, Any]: Dict containing the json data.
    """
    with open(filename, "r") as file:
        return json.load(file)

write_json(filename, data)

Writes a json file to the given path, creates the parent dir.

Parameters:

Name Type Description Default
filename Path

Name of the file.

required
data Dict[str, Any]

Dict to be written as json.

required
Source code in src/distilabel/utils/serialization.py
def write_json(filename: Path, data: Dict[str, Any]) -> None:
    """Writes a json file to the given path, creates the parent dir.

    Args:
        filename (Path): Name of the file.
        data (Dict[str, Any]): Dict to be written as json.
    """
    filename.parent.mkdir(parents=True, exist_ok=True)
    with open(filename, "w") as f:
        json.dump(data, f, indent=2)