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

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

Docker-compose no longer building image (AttributeError: cython_sources)

Important security disclaimer: I would strongly discourage anyone downgrading PyYAML to 5.3.1, as versions before 5.4 all have a known CVE — CVE-2020-14343. This issue was caused by an incompatibility of PyYAML<6.0.1 compiled under the version of Cython>=3.0, released on July 17, 2023. This has now been fixed by a new release of PyYAML, tagged … Read more

How to read a python tuple using PyYAML?

I wouldn’t call what you’ve done hacky for what you are trying to do. Your alternative approach from my understanding is to make use of python-specific tags in your YAML file so it is represented appropriately when loading the yaml file. However, this requires you modifying your yaml file which, if huge, is probably going … Read more

YAML loads 5e-6 as string and not a number

The problem lies in the fact that the YAML Resolver is set up to match floats as follows: Resolver.add_implicit_resolver( u’tag:yaml.org,2002:float’, re.compile(u”’^(?:[-+]?(?:[0-9][0-9_]*)\\.[0-9_]*(?:[eE][-+][0-9]+)? |\\.[0-9_]+(?:[eE][-+][0-9]+)? |[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\\.[0-9_]* |[-+]?\\.(?:inf|Inf|INF) |\\.(?:nan|NaN|NAN))$”’, re.X), list(u’-+0123456789.’)) whereas the YAML spec specifies the regex for scientific notation as: -? [1-9] ( \. [0-9]* [1-9] )? ( e [-+] [1-9] [0-9]* )? the latter makes the … Read more

tech