pyyaml

Parse YAML with dots delimiter in keys

Parse YAML with dots delimiter in keys Question: We use YAML configuration for services scaling. Usually it goes like this: service: scalingPolicy: capacity: min: 1 max: 1 So it’s easy to open with basic PyYAML and parse as an dict to get config[‘service’][‘scalingPolicy’][‘capacity’][‘min’] result as 1. Problem is that some configs are built with dots …

Total answers: 2

TypeError: load() missing 1 required positional argument: 'Loader' in Google Colab

TypeError: load() missing 1 required positional argument: 'Loader' in Google Colab Question: I am trying to do a regular import in Google Colab. This import worked up until now. If I try: import plotly.express as px or import pingouin as pg I get an error: ————————————————————————— TypeError Traceback (most recent call last) <ipython-input-19-86e89bd44552> in <module>() …

Total answers: 4

ruamel.yaml: pin comment to next data item instead of previous one

ruamel.yaml: pin comment to next data item instead of previous one Question: hI observed a somewhat confusing behavior when using ruamel.yaml with roundtrip loader. It’s probably because it’s not trivial for ruamel.yaml to automatically determine to which data item a comment should be connected. In the below example, I would like to always keep the …

Total answers: 1

Indent items of YAML lists when dumping in Python

Indent items of YAML lists when dumping Question: The context Consider this minimal working example: I have this JSON file $ cat main.json [ { "name": "a", "numbers": [1, 2] }, { "name": "b", "numbers": [10, 20] } ] I need to convert that JSON file to YAML. The following script accomplishes that $ cat …

Total answers: 1

Array does not have indent or space in PyYAML

Array does not have indent or space in PyYAML Question: In the code below I created the net_plan_dict variable dictionary and converted it into a YAML format file. Inside the dictionary I have a field called addresses which is an array of three elements. After creating the YAML file, the three array elements were not …

Total answers: 2

Loading custom objects with PyYAML

Loading custom objects with PyYAML Question: I have the following YAML document: !!com.example.hero.YAMLAnimals animals: Land: [Cow, Lion] Sea: [Salmon, Cod] I expected to be able to load the animals object by subclassing YAMLObject. class YAMLAnimals(yaml.YAMLObject): yaml_tag = u’!com.example.hero.YAMLAnimals’ def __init__(self, animals): self.animals = animals However, when I load the document, I get a ConstructorError. ConstructorError: …

Total answers: 2

Is 'yes' really an alias for 'true' according to the YAML 1.1 spec? The 1.2 spec?

Is 'yes' really an alias for 'true' according to the YAML 1.1 spec? The 1.2 spec? Question: I’m trying to debug an issue, and it boils down to… >>> import yaml as pyyaml >>> import ruamel.yaml as ruamel >>> ruamel.load(“x: yes”) == ruamel.load(“x: true”) False >>> pyyaml.safe_load(“x: yes”) == pyyaml.safe_load(“x: true”) True There are rumors …

Total answers: 3

How to parse a YAML file with multiple documents?

How to parse a YAML file with multiple documents? Question: Here is my parsing code: import yaml def yaml_as_python(val): “””Convert YAML to dict””” try: return yaml.load_all(val) except yaml.YAMLError as exc: return exc with open(‘circuits-small.yaml’,’r’) as input_file: results = yaml_as_python(input_file) print results for value in results: print value Here is a sample of the file: ingests: …

Total answers: 2

How to read a python tuple using PyYAML?

How to read a python tuple using PyYAML? Question: I have the following YAML file named input.yaml: cities: 1: [0,0] 2: [4,0] 3: [0,4] 4: [4,4] 5: [2,2] 6: [6,2] highways: – [1,2] – [1,3] – [1,5] – [2,4] – [3,4] – [5,4] start: 1 end: 4 I’m loading it using PyYAML and printing the …

Total answers: 3

yaml anchors definitions loading in PyYAML

yaml anchors definitions loading in PyYAML Question: I’m using PyYAML. Is there a way to define a YAML anchor in a way it won’t be a part of the data structure loaded by yaml.load (I can remove “wifi_parm” from the dictionary but looking for a smarter way)? example.yaml: wifi_parm: &wifi_params ssid: 1 key: 2 test1: …

Total answers: 3