-
Notifications
You must be signed in to change notification settings - Fork 521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to dump the formats Fold (>) or Block (|) using the pyyaml #693
Comments
There's a work-around described in https://stackoverflow.com/questions/8640959/how-can-i-control-what-scalar-form-pyyaml-uses-for-my-data However: the fact that this (barely documented) work-around is necessary to begin with is currently my biggest complaint with PyYAML and YAML generally: it is seemingly not possible to parse a document into data, then serialize that data into YAML and get an identical document as output. Now, probably, that's little more than an annoyance in the majority of cases... but ultimately it's the application which must use the string content of a scalar, and messing with the quoting could mess with the application. Eg., an unintentionally added chomp operator could strip a newline that might have been important (though admittedly most applications should be resilient to this, how could we forget mousepad bug 4824 that was open for 10 years?). Or, changing Kinda getting into yaml/yaml-spec#289 on this, wishing for a round-trip in which no modifications are made to produce "string-equal" representations on both ends. Personally I consider "semantic equivalence" to be determined by the consumer of the document, and the only way for PyYAML to ensure semantic equivalence for all consumers is to guarantee string equivalence between source and output in a no-change round-trip. That means (at least) retaining scalar style. Ideally this would also extend so that if a scalar is modified by the application but its style is not, the same style should be used in the output document, eg.
Unfortunately, retaining the original quote style through a round trip won't be possible as long as the data is being loaded into a regular Probably the best solution would be to ditch Some drawbacks:
Those drawbacks can be (at least initially) addressed by making this opt-in as new |
There's no great way to do this today with stock PyYAML, but it's also not terribly difficult to do some lightweight customizations to smuggle parser details like this either on expando/subclass attrs of the various native representations, or in a weakref'd lookup table that relies on object identity (which can then be consulted by an associated dumper attempting to re-create state). It's also one of the ways I've hacked in comment roundtrip support before (no, I'm not shipping it 😆 ). As you've alluded, ultimately, there are several spec gray-areas (and not-so-gray areas) that kinda prevent doing this stuff generically, as well as some limitations of Python (and other YAML impl/deserialization languages) that make parts of it tricky to do. It's not impossible, and a lot of projects have managed to hack it in for their own needs, but I also wouldn't hold my breath for a generic solution anytime soon... |
Hi,
I have a YAML syntax where I want to use the fold (>) property of the YAML in the original
I want to dump the same character (with the fold block of the set) using the library, is this possible?
EG:-
groups: >-
{
'{{PREFIX }}-sys':[
'read',
'write',
'annotate',
'distribute'
]
}
Also the format as
PREFIX: "{{ project_config.PROJECT_ACRONYM }}-
{{ project_config.PROJECT_CUSTOMER }}-
{{ project_config.PROJECT_ID }}"
Is it possible to dump the YAML with the format intact (or) can any arguments added to get this format
The text was updated successfully, but these errors were encountered: