FreeBSD YAML files in Python using the YAML YML module

This article explains how to learn using python to work with yaml files by reading and writing it using popular Pyyaml libraries. In writing this article we use FreeBSD 13.2 as a server to run Yaml files with Python39.

Python39 has several modules to read YAML files. Through articles we will learn about how to read the yaml file on python, or rather, we will show you how to do this using the pyyaml module.


1. What is YAML File

YAML is a recursive acronym for "YAML Ain't MarkupLanguage". YAML stands for Yet Another Mark-up Language, which means "Another markup language". This file format is now very popular for storing serialized data that is fairly human-readable data format that is widely used for configuration files and data exchange between languages with different data structures.

YAML is an easy-to-read data serialization standard for all programming languages. This format is mainly used for configuration files, but it can also be used for other purposes. The contents of such a file can be different types of scalar data, such as a number, string, and so on. It can also be composite data, such as a list or dictionary. YAML file extension "*.yaml".

The full form of YAML is Yet Another Mark-up Language. This file format is very popular now to store serialized data that is human-readable. It is mainly used for configuration files, but it can be used for other purposes also. Different types of scalar data such as number, string, etc., and compound data such as list, the dictionary can be the content of this file. The extension of this fie is '.yaml'. Multiple modules exist in Python to read the YAML file.


2. FreeBSD Package PKG PyYAML

PyYAML is a YAML parser and emitter for the Python programming language.

PyYAML features:
  1. A complete YAML 1.1 parser. In particular, PyYAML can parse all examples from the specification. The parsing algorithm is simple enough to be a reference for YAML parser implementors.
  2. Unicode support including UTF-8/UTF-16 input/output and escape sequences.
  3. Low-level event-based parser and emitter API (like SAX).
  4. High-level API for serializing and deserializing native Python objects (like DOM or pickle). support for all types from the YAML types repository. A simple extension API is provided.
  5. Relatively sensible error messages.
To install PyYAML on FreeBSD, run the following command.
root@ns6:~ # pkg install py39-aspy.yaml py39-pyaml py39-pyaml-env py39-pydantic-yaml py39-pyyaml-include py39-pyyaml5 py39-pyyaml_env_tag py39-ruamel.yaml py39-ruamel.yaml.clib py39-saneyaml py39-strictyaml py39-types-PyYAML py39-yaml py39-yamllint
Updating FreeBSD repository catalogue...
FreeBSD repository is up to date.
All repositories are up to date.
The following 17 package(s) will be affected (of 0 checked):

New packages to be INSTALLED:
        py39-annotated-types: 0.5.0
        py39-aspy.yaml: 1.3.0
        py39-dateutil: 2.8.2
        py39-pyaml: 23.5.9
        py39-pyaml-env: 1.2.1
        py39-pydantic-core: 2.10.1
        py39-pydantic-yaml: 1.2.0
        py39-pydantic2: 2.4.2
        py39-pyyaml-include: 1.3_2
        py39-pyyaml5: 5.4.1
        py39-pyyaml_env_tag: 0.1
        py39-ruamel.yaml: 0.17.33
        py39-ruamel.yaml.clib: 0.2.7_1
        py39-saneyaml: 0.6.0
        py39-strictyaml: 1.7.3
        py39-types-PyYAML: 6.0.12.12
        py39-yamllint: 1.32.0

Number of packages to be installed: 17

The process will require 14 MiB more space.
3 MiB to be downloaded.
If you use the FreeBSD Ports system, run the following command.
root@ns6:~ # cd /usr/ports/devel/py-yaml
root@ns6:/usr/ports/devel/py-yaml # make config
root@ns6:/usr/ports/devel/py-yaml # make install clean


3. Python YAML Configuration Guide

After the package installation, you can proceed to import the PyYAML package in Python. In this example, we will open the config.yaml file in read mode and pass it to the yaml.load function. Additionally we will also define the Loader parameter as yaml.FullLoader, which is a secure loader that can load any YAML document.

a. Example 1

As a first step, we will create a folder for practice and create a main.py and config.yaml file, run the following command.
root@ns6:~ # mkdir -p /tmp/PyYAML
root@ns6:~ # cd /tmp/PyYAML
root@ns6:/tmp/PyYAML #
The following is an example script for the "/tmp/PyYAML/config.yaml" file.

maintainer: Jack Wilson
pca_params:
  n_components: 224
umap_params:
  min_dist:
  - 0.01
  - 0.1
  - 0.5
  spread: 1.0
  metrics:
  - euclidean
  - l1
  - manhattan
  - chebyshev
  - chebyshev
visualize_params:
  median_threshold: 6
n_components: 0.2

Here's an example script of "/tmp/PyYAML/main.py" file.

import yaml
from pprint import pprint

with open("config.yaml") as f:
    cfg = yaml.load(f, Loader=yaml.FullLoader)


pprint(cfg["pca_params"])

cfg["n_components"] = 0.2
cfg["umap_params"]["metrics"].append("chebyshev")

with open("config.yaml", "w") as f:
    cfg = yaml.dump(cfg, stream=f, default_flow_style=False, sort_keys=False)

Run the main.py file.
root@ns6:/tmp/PyYAML # python main.py
{'n_components': 224}

b. Example 2

In this example, We will write data to a YAML file using the yaml.dump() method which takes a python object and a file pointer as parameters. In the example below, we write the article_info dictionary into a YAML file named "tutswiki.yaml".

The following is an example of a code snippet from the "/tmp/PyYAML/dump.py" file.

import yaml

article_info = [
    {
        'Details': {
        'domain' : 'https://www.unixwinbsd.site',
        'language': 'python',
        'date': '3/12/2023'
        }
    }
]

with open("tutswiki.yaml", 'w') as yamlfile:
    data = yaml.dump(article_info, yamlfile)
    print("Write successful")


Run the "/tmp/PyYAML/dump.py" file.
root@ns6:/tmp/PyYAML # python dump.py
Write successful
Please open the tutswiki.yaml file with the cat command.
root@ns6:/tmp/PyYAML # cat /tmp/PyYAML/tutswiki.yaml
- Details:
    date: 3/12/2023
    domain: https://www.unixwinbsd.site
    language: python

In this tutorial, we discussed using Python to work with various types of YAML files. In this tutorial, we use Python as a YAML parser. I hope this tutorial is useful for you.
Iwan Setiawan

I Like Adventure: Mahameru Mount, Rinjani Mount I Like Writer FreeBSD

Post a Comment

Previous Post Next Post