Skip to content

Definitions and catalogue

Language-neutral definition files and the registry of the ones installed packages ship.

write_scale

write_scale(
    x: NestedScale,
    path: str | PathLike,
    format: str = "json",
    tables: str = "auto",
    overwrite: bool = False,
) -> Path

Write a scale as a definition: one file (.json/.yaml) with inline tables, or a bundle directory with scale.json and table files.

Schema version 2 is written. The label table is written when the scale has labels. A scale held in meta is written as a reference, its name and scale_id().

meta may hold strings, numbers (numpy scalars included), booleans, None, lists, dicts and tables. As in R, a list of one scalar is written as that scalar and so reads back as a scalar, not as a one-element list; NaN is written as null; an infinite number, or a value of another type, is an error raised before any file is written.

Examples

import os, tempfile import nestedscales as ns s = ns.scale_example() with tempfile.TemporaryDirectory() as d: ... path = ns.write_scale(s, os.path.join(d, "example.json")) ... back = ns.read_scale(path) ns.scale_id(back) == ns.scale_id(s) True

read_scale

read_scale(
    path: str | PathLike,
    package: str | None = None,
    cls: str = "stored",
) -> NestedScale

Read a definition file, a bundle directory or a catalogue id.

Schema versions 1 and 2 are read. The scale comes back as the class it was written from when that class is registered; cls="core" gives the plain NestedScale (R spells the argument class, a reserved word in Python). A definition written by either binding is read, with a JSON or a YAML header.

scale_catalog

scale_catalog() -> pa.Table

Every definition the registered directories hold (headers only).

A table with one row per definition: id (what read_scale() takes), name, kind (the class, without its package), leaf (the name of the leaf column), frames (joined by /), n_leaves, package and path, sorted by id.

register_scale_dir

register_scale_dir(
    path: str | PathLike, package: str | None = None
) -> str

Register a directory of definition files or bundles, so that scale_catalog() lists them and read_scale() opens one by its id.

package names the package that ships the directory and is the registry key; without it the key is the resolved path, and the directory is listed with a null package. Registering a key again replaces it. Returns the key.

list_scale_dirs

list_scale_dirs() -> pa.Table

The registered directories: a table with the columns package (null for a directory registered without one) and path, sorted by registry key.

clear_scale_dirs

clear_scale_dirs() -> None

Forget every registered directory, the binding's own included; the files are left as they are.