Skip to content

Commit b74d896

Browse files
committed
Fix Issue CiscoDevNet#6 - add commands from slides
1 parent 231e8a1 commit b74d896

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#! /usr/bin/env python
2+
"""
3+
Learning Series: Network Programmability Basics
4+
Module: Programming Fundamentals
5+
Lesson: Python Part 3
6+
Author: Hank Preston <hapresto@cisco.com>
7+
8+
data_library_exercises.py
9+
Illustrate the following concepts:
10+
- Commands from slides exploring data interactions
11+
"""
12+
13+
# XML
14+
print("Treat XML like Python Dictionaries with xmltodict\n\n")
15+
import xmltodict
16+
from pprint import pprint
17+
18+
xml_example = open("xml_example.xml").read()
19+
pprint(xml_example)
20+
21+
xml_dict = xmltodict.parse(xml_example)
22+
int_name = xml_dict["interface"]["name"]
23+
print(int_name)
24+
25+
print(xmltodict.unparse(xml_dict))
26+
27+
print("\n\n")
28+
29+
# JSON
30+
print("To JSON and back again with json\n\n")
31+
import json
32+
from pprint import pprint
33+
34+
json_example = open("json_example.json").read()
35+
pprint(json_example)
36+
37+
json_python = json.loads(json_example)
38+
int_name = json_python["ietf-interfaces:interface"]["name"]
39+
print(int_name)
40+
41+
print(json.dumps(json_python))
42+
43+
print("\n\n")
44+
45+
# YAML
46+
print("YAML? Yep, Python Can Do That Too!\n\n")
47+
import yaml
48+
from pprint import pprint
49+
50+
yml_example = open("yaml_example.yaml").read()
51+
pprint(yml_example)
52+
53+
# Note: The video used the yaml.load() method. This method
54+
# has security vulnerabilities and has been deprecated. The
55+
# yaml.safe_load() method is the suggested way to load YAML
56+
# data now.
57+
# You can read about this at https://msg.pyyaml.org/load
58+
yaml_python = yaml.safe_load(yml_example)
59+
int_name = yaml_python["ietf-interfaces:interface"]["name"]
60+
print(int_name)
61+
62+
print(yaml.dump(yaml_python))
63+
64+
print("\n\n")
65+
66+
# CSV
67+
print("Import Spreadsheets and Data with csv\n\n")
68+
import csv
69+
from pprint import pprint
70+
71+
csv_example = open("csv_example.csv").read()
72+
pprint(csv_example)
73+
74+
csv_example = open("csv_example.csv")
75+
csv_python = csv.reader(csv_example)
76+
77+
for row in csv_python:
78+
print("{} is in {} and has IP {}.".format(row[0], row[2], row[1]))
79+
80+
print("\n\n")

0 commit comments

Comments
 (0)