Add Qube.load

This commit is contained in:
Tom 2025-04-12 16:06:29 +02:00
parent 11014b07ea
commit bf47401e6c

View File

@ -1,9 +1,11 @@
import dataclasses import dataclasses
import functools import functools
import json
from collections import defaultdict from collections import defaultdict
from collections.abc import Callable from collections.abc import Callable
from dataclasses import dataclass from dataclasses import dataclass
from functools import cached_property from functools import cached_property
from pathlib import Path
from typing import Any, Iterable, Iterator, Literal, Sequence from typing import Any, Iterable, Iterator, Literal, Sequence
from frozendict import frozendict from frozendict import frozendict
@ -63,6 +65,11 @@ class Qube:
def root_node(cls, children: Iterable["Qube"]) -> "Qube": def root_node(cls, children: Iterable["Qube"]) -> "Qube":
return cls.make("root", QEnum(("root",)), children) return cls.make("root", QEnum(("root",)), children)
@classmethod
def load(cls, path: str | Path) -> "Qube":
with open(path, "r") as f:
return Qube.from_json(json.load(f))
@classmethod @classmethod
def from_datacube(cls, datacube: dict[str, str | Sequence[str]]) -> "Qube": def from_datacube(cls, datacube: dict[str, str | Sequence[str]]) -> "Qube":
key_vals = list(datacube.items())[::-1] key_vals = list(datacube.items())[::-1]