On CentOS 7.2 :

[root@centos-7 ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)

We already have Python 2.7 :

[root@centos-7 ~]# python --version
Python 2.7.5

Python Package Index

Install EPEL repository :

[root@centos-7 ~]# yum -y install epel-release

Which RPM contains “pip” ?

[root@centos-7 ~]# yum provides pip
---
---
---
python-pip-7.1.0-1.el7.noarch : A tool for installing and managing Python packages
Repo        : epel
Matched from:
Filename    : /usr/bin/pip

Install “pip” :

[root@centos-7 ~]# yum -y install python-pip

We already have following Python packages :

[root@centos-7 ~]# pip list
You are using pip version 7.1.0, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
backports.ssl-match-hostname (3.4.0.2)
configobj (4.7.2)
decorator (3.4.0)
iniparse (0.4)
perf (0.1)
pip (7.1.0)
pycurl (7.19.0)
pygobject (3.14.0)
pygpgme (0.3)
pyliblzma (0.5.3)
pyudev (0.15)
pyxattr (0.5.1)
setuptools (0.9.8)
slip (0.4.0)
slip.dbus (0.4.0)
urlgrabber (3.10)
yum-metadata-parser (1.1.4)

Upgrading “pip” ? why not ?

[root@centos-7 ~]# pip install --upgrade pip

Another complaint :

[root@centos-7 ~]# pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
backports.ssl-match-hostname (3.4.0.2)
configobj (4.7.2)
decorator (3.4.0)
iniparse (0.4)
perf (0.1)
pip (9.0.1)
pycurl (7.19.0)
pygobject (3.14.0)
pygpgme (0.3)
pyliblzma (0.5.3)
pyudev (0.15)
pyxattr (0.5.1)
setuptools (0.9.8)
slip (0.4.0)
slip.dbus (0.4.0)
urlgrabber (3.10)
yum-metadata-parser (1.1.4)

Edit “pip.conf” configuration file (here for all users) :

[root@centos-7 ~]# cat /etc/pip.conf
# "pip" configuration for all users :$

[list]
format=columns

No complaint anymore :

[root@centos-7 ~]# pip list
Package                      Version
---------------------------- -------
backports.ssl-match-hostname 3.4.0.2
configobj                    4.7.2
decorator                    3.4.0
iniparse                     0.4
perf                         0.1
pip                          9.0.1
pycurl                       7.19.0
pygobject                    3.14.0
pygpgme                      0.3
pyliblzma                    0.5.3
pyudev                       0.15
pyxattr                      0.5.1
setuptools                   0.9.8
slip                         0.4.0
slip.dbus                    0.4.0
urlgrabber                   3.10
yum-metadata-parser          1.1.4

Ok, now install “PyYAML” package :

[root@centos-7 ~]# pip install PyYAML

Python script example

We create a directory :

[root@centos-7 ~]# mkdir /examples && cd /examples

Then, create this YAML example file :

[root@centos-7 examples]# cat example.yaml
---

identity:
  name: wou
  first_name: frederic
  gender: male
  nationality: french

address:
  city: paris

Here a Python script example to parse this YAML file :

[root@centos-7 examples]# cat read.py
#!/usr/bin/env python

import sys
import yaml

# Which release of Python ?
print("running Python version "+sys.version+"\n")

# Open YAML file :
with open("example.yaml", 'r') as yaml_file:
  yaml_data = yaml.load(yaml_file)

# Display YAML file content in 2 ways :
print ("here are YAML data (exposed in default flow style):\n"+yaml.dump(yaml_data))
print ("here are YAML data (exposed in \"YAML\" style):\n"+yaml.dump(yaml_data,default_flow_style=False))

# define functions :
def identity_action():
  print(yaml_data[section]['first_name']+" "+yaml_data[section]['name']+" is "+yaml_data[section]['nationality']),
def address_action():
  print("and is living in \""+yaml_data[section]['city']+"\"")

# dictionary of functions :
apply_function_to_section = {'identity' : identity_action,
                             'address'  : address_action,
}

for section in yaml_data:
  apply_function_to_section[section]()

Finaly, we execute this Python script :

[root@centos-7 examples]# ./read.py
running Python version 2.7.5 (default, Sep 15 2016, 22:37:39)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)]

here are YAML data (exposed in default flow style):
address: {city: paris}
identity: {first_name: frederic, gender: male, name: wou, nationality: french}

here are YAML data (exposed in "YAML" style):
address:
  city: paris
identity:
  first_name: frederic
  gender: male
  name: wou
  nationality: french

frederic wou is french and is living in "paris"

 

PDF24    Send article as PDF   

Leave a Reply

Your email address will not be published. Required fields are marked *


*