Skip to content

Notation like &id001 in saved yaml #125

@smmaurer

Description

@smmaurer

Sometimes saved yaml files can include automatically generated notation like &id001:

saved_object:
    filters: &id001
    - rent_median_7 > 0
    - block_groups_mean_year_built > 0

What does this mean? Is it a problem? This issue was reported by @msoltadeo.

Diagnosis

This turns out to be a feature of the YAML spec called "anchors and aliases". When data (in this case a list of strings) is passed by name instead of value, and shows up in a YAML file multiple times, PyYAML automatically adds an identifier like &id001 in front of the first occurrence. In subsequent occurrences, only the corresponding alias appears:

    model:
        fit_filters: *id001

When the YAML file is read back into Python, the data is reconstructed like normal. My understanding is that this helps save space in YAML files, and can also help avoid potential recursion issues.

Resolution

If we want to, we can add a patch to urbansim_templates to disable this YAML feature. I am inclined to do this, since (a) it would make the yaml files a bit more human-readable, and (b) it might work more smoothly in case we need to read or modify the files using an interpreter that doesn't implement this aspect of the YAML spec.

Sample affected file: repm_rent7.yaml
Demonstration of patch: test.py

References

Discussion and workarounds:

YAML spec:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions