Skip to content

Json

load_batch

load_batch(filename_or_io, *backend_args, **backend_kwargs)

load batch from json file or string io to batch object

Parameters:

Name Type Description Default
filename_or_io Union[str, TextIO]

filename or file object pointing to

required
*backend_args

args to pass to backend construction.

()
**backend_kwargs

kwargs to pass to backend construction.

{}

Returns:

Type Description
Union[RemoteBatch, LocalBatch]

Union[RemoteBatch, LocalBatch]: the resulting batch object

Note

The backend args are not always required for LocalBatch objects, but for RemoteBatch objects they are required.

Source code in src/bloqade/task/json.py
def load_batch(
    filename_or_io: Union[str, TextIO], *backend_args, **backend_kwargs
) -> Union[RemoteBatch, LocalBatch]:
    """load batch from json file or string io to batch object

    Args:
        filename_or_io (Union[str, TextIO]): filename or file object pointing to
        json file.
        *backend_args: args to pass to backend construction.
        **backend_kwargs: kwargs to pass to backend construction.

    Returns:
        Union[RemoteBatch, LocalBatch]: the resulting batch object

    Note:
        The backend args are not always required for `LocalBatch` objects, but
        for `RemoteBatch` objects they are required.
    """
    deserializer = BatchDeserializer(*backend_args, **backend_kwargs)
    if isinstance(filename_or_io, str):
        with open(filename_or_io, "r") as f:
            return json.load(f, object_hook=deserializer.object_hook)
    else:
        return json.load(filename_or_io, object_hook=deserializer.object_hook)

save_batch

save_batch(filename_or_io, batch)

save batch to json file or string io

Parameters:

Name Type Description Default
filename_or_io Union[str, TextIO]

filename or file object pointing to

required
batch Union[RemoteBatch, LocalBatch]

batch object to save.

required
Source code in src/bloqade/task/json.py
def save_batch(
    filename_or_io: Union[str, TextIO], batch: Union[RemoteBatch, LocalBatch]
) -> None:
    """save batch to json file or string io

    Args:
        filename_or_io (Union[str, TextIO]): filename or file object pointing to
        json file.
        batch (Union[RemoteBatch, LocalBatch]): batch object to save.
    """
    if isinstance(filename_or_io, str):
        with open(filename_or_io, "w") as f:
            json.dump(batch, f, cls=BatchSerializer)
    else:
        json.dump(batch, filename_or_io, cls=BatchSerializer)