Skip to content

YAML Loader

YamlLoader turns a YAML string into a canonical YadsSpec.

    from yads.loaders import YamlLoader

    yaml_string = """
name: "prod.assessments.submissions"
version: 1
yads_spec_version: "0.0.2"

columns:
  - name: "submission_id"
    type: "bigint"
    constraints:
      primary_key: true
      not_null: true

  - name: "completion_percent"
    type: "decimal"
    params:
      precision: 5
      scale: 2
    constraints:
      default: 0.00

  - name: "time_taken_seconds"
    type: "integer"

  - name: "submitted_at"
    type: "timestamptz"
    params:
      tz: "UTC"
    """

    loader = YamlLoader()
    spec = loader.load(yaml_string)
    print(spec)
spec prod.assessments.submissions(version=1)(
  columns=[
    submission_id: integer(bits=64)(
      constraints=[PrimaryKeyConstraint(), NotNullConstraint()]
    )
    completion_percent: decimal(precision=5, scale=2)(
      constraints=[DefaultConstraint(value=0.0)]
    )
    time_taken_seconds: integer(bits=32)
    submitted_at: timestamptz(unit=ns, tz=UTC)
  ]
)

YamlLoader

Bases: BaseLoader

Loads a YadsSpec from a YAML string.

Source code in src/yads/loaders/yaml_loader.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class YamlLoader(BaseLoader):
    """Loads a `YadsSpec` from a YAML string."""

    def load(self, content: str) -> YadsSpec:
        """Parses the YAML content and builds the spec.

        Args:
            content: The YAML string content.

        Returns:
            A `YadsSpec` instance.

        Raises:
            SpecParsingError: If the YAML content is invalid or does not
                              parse to a dictionary.
        """
        raw_data = yaml.safe_load(content)
        if not isinstance(raw_data, dict):
            raise SpecParsingError("Loaded YAML content did not parse to a dictionary.")
        data = cast(dict[str, Any], raw_data)
        return DictLoader().load(data)

load(content)

Parses the YAML content and builds the spec.

Parameters:

Name Type Description Default
content str

The YAML string content.

required

Returns:

Type Description
YadsSpec

A YadsSpec instance.

Raises:

Type Description
SpecParsingError

If the YAML content is invalid or does not parse to a dictionary.

Source code in src/yads/loaders/yaml_loader.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def load(self, content: str) -> YadsSpec:
    """Parses the YAML content and builds the spec.

    Args:
        content: The YAML string content.

    Returns:
        A `YadsSpec` instance.

    Raises:
        SpecParsingError: If the YAML content is invalid or does not
                          parse to a dictionary.
    """
    raw_data = yaml.safe_load(content)
    if not isinstance(raw_data, dict):
        raise SpecParsingError("Loaded YAML content did not parse to a dictionary.")
    data = cast(dict[str, Any], raw_data)
    return DictLoader().load(data)