Skip to main content

4 posts tagged with "yaml"

yaml tag description

View All Tags

A Guide to Preserving YAML Formatting with PyYAML

· 6 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

Python's PyYAML library is a powerful tool for working with YAML, but it has one common limitation: when you load a YAML file and dump it back, it doesn't preserve the original formatting. This can be a problem if you have multi-line strings formatted as literal blocks (|) and want to keep them that way for readability.

Fortunately, there's a straightforward and effective way to solve this by creating a custom representer for PyYAML. This guide will walk you through the process, using the code you provided, to ensure your multi-line strings are always dumped as literal blocks.

Preserve the original literal block format

· 5 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

No, it is not possible for standard Python YAML libraries like PyYAML to preserve the original literal block format (e.g., | or >) when you load a YAML file and then dump it back. This is because the parser converts the literal block content into a standard Python string, discarding the original formatting. When you dump the string back to YAML, the dumper uses its own rules to represent the string, which typically defaults to a quoted style or a folded block style, but not necessarily the original one.

Handling Environment Variables in OpenAPI Server URLs

· 4 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

When defining server URLs in OpenAPI specs, you often need to reference environment-specific values (like staging or production subdomains). But using raw environment variables can result in placeholder values like default: unknown, which may confuse tools like Swagger UI, ReDoc, or code generators.

This guide explains the issue and offers clean solutions - with practical examples.