Parsing YAML, return with line number

Here’s an improved version of puzzlet’s answer: import yaml from yaml.loader import SafeLoader class SafeLineLoader(SafeLoader): def construct_mapping(self, node, deep=False): mapping = super(SafeLineLoader, self).construct_mapping(node, deep=deep) # Add 1 so line numbering starts at 1 mapping[‘__line__’] = node.start_mark.line + 1 return mapping You can use it like this: data = yaml.load(whatever, Loader=SafeLineLoader)

Any yaml libraries in Python that support dumping of long strings as block literals or folded blocks?

import yaml class folded_unicode(unicode): pass class literal_unicode(unicode): pass def folded_unicode_representer(dumper, data): return dumper.represent_scalar(u’tag:yaml.org,2002:str’, data, style=”>”) def literal_unicode_representer(dumper, data): return dumper.represent_scalar(u’tag:yaml.org,2002:str’, data, style=”|”) yaml.add_representer(folded_unicode, folded_unicode_representer) yaml.add_representer(literal_unicode, literal_unicode_representer) data = { ‘literal’:literal_unicode( u’by hjw ___\n’ ‘ __ /.-.\\\n’ ‘ / )_____________\\\\ Y\n’ ‘ /_ /=== == === === =\\ _\\_\n’ ‘( /)=== == === === == Y … Read more

dump json into yaml

pyyaml.dump() has an allow_unicode option that defaults to None (all non-ASCII characters in the output are escaped). If allow_unicode=True, then it writes raw Unicode strings. yaml.dump(data, ff, allow_unicode=True) Bonus You can dump JSON without encoding as follows: json.dump(data, outfile, ensure_ascii=False)

RSA public/private keys in YAML

You can store your keys as text (“ASCII-armored” / base 64 encoded). From Wikipedia, the syntax for multiline strings in YAML is: – title: An example multi-line string in YAML body : | This is a multi-line string. “special” metacharacters may appear here. The extent of this string is indicated by indentation.

Performing arithmetic operation in YAML?

I don’t think there is. At least not on spec (http://yaml.org/spec/1.2/spec.html). People add non-official tags to yaml (and wikipedia seems to say there’s proposal for a yield tag, though they don’t say who proposed or where: http://en.wikipedia.org/wiki/YAML#cite_note-16), but nothing like you need seems to be available in pyyaml. Looking at pyyaml specific tags there doesn’t … Read more

How to generate OpenAPI 3.0 YAML file from existing Spring REST API?

We have used lately springdoc-openapi java library. It helps automating the generation of API documentation using spring boot projects. It automatically deploys swagger-ui to a spring-boot application Documentation will be available in HTML format, using the official [swagger-ui jars]: The Swagger UI page should then be available at http://server:port/context-path/swagger-ui.html and the OpenAPI description will be … Read more

tech