Coding Getting Started with Python

Similar documents
NXOS in the Real World Using NX-API REST

NetDevOps Style Configuration Management for the Network

DEVNET Introduction to Git. Ashley Roach Principal Engineer Evangelist

Managing Cisco UCS with the Python SDK

Hands On Exploration of NETCONF and YANG

Git, Atom, virtualenv, oh my! Learn about dev tools to live by!

Your API Toolbelt Tools and techniques for testing, monitoring, and troubleshooting REST API requests

Coding Intro to APIs and REST

Cisco Spark Messaging APIs - Integration Platforms as a Service Real World Use-Cases

Cisco UCS Agentless Configuration Management Ansible or Microsoft DSC

Hands-On with IoT Standards & Protocols

Automation and Programmability using Cisco Open NXOS and DevOps Tools

CloudCenter for Developers

Empower your testing with Cisco Test Automation Solution Featuring pyats & Genie

BGP in the Enterprise for Fun and (fake) Profit: A Hands-On Lab

Automation with Meraki Provisioning API

Get Hands On With DNA Center APIs for Managing Intent

2018 Cisco and/or its affiliates. All rights reserved. Cisco Public

Catalyst 9K High Availability Lab

Who wants to be a millionaire? A class in creating your own cryptocurrency

Introduction to OpenConfig

Finesse APIs: Getting started with the REST APIs and XMPP events

DevNet Workshop-Hands-on with CloudCenter and Jenkins

Magical Chatbots with Cisco Spark and IBM Watson

Hybrid Cloud Automation using Cisco CloudCenter API

Kuber-what?! Learn about Kubernetes

Insights into your WLC with Wireless Streaming Telemetry

Cisco Container Platform

An Introduction to Monitoring Encrypted Network Traffic with "Joy"

Deploying Cloud-Agnostic Applications with Cisco CloudCenter

An Introduction to Developing for Cisco Kinetic

Introducing Cisco Network Assurance Engine

Cisco Spark Widgets Technical drill down

WORKSHOP: from Zero to a Network Application with #golang

Getting Started With Containers

European DevNet Pros Panel

TRex Realistic Traffic Generator

Getting Started with OpenStack

Introduction to Python Network Programming for Network Architects and Engineers

Privacy Requirements Scoping

Chris Calloway for Triangle Python Users Group at Caktus Group December 14, 2017

Python Development Workflow

PnP Deep Dive Hands-on with APIC-EM and Prime Infrastructure

Tetration Hands-on Lab from Deployment to Operations Support

Cisco WAN Automation Engine (WAE) Network Programmability with Segment Routing

Creating a custom gadget using the Finesse JavaScript Library API

Cisco SD-Access Hands-on Lab

DevOps CICD for VNF a NetOps Approach

Consuming Model-Driven Telemetry

NSO in Brownfield: Fully Automated One-Click Reconciliation

Spark SDK Video - Overview and Coding Demo

PSOACI Tetration Overview. Mike Herbert

Cloud Mobility: Meraki Wireless & EMM

Contiv installation and integration with ACI

Cisco UCS Director and ACI Advanced Deployment Lab

Cisco Enterprise Agreement

NetDevOps for the Network Dude How to get started with API's, Ansible and Python

Scripting the Catalyst: Beyond the Basics

Cisco Virtualized Infrastructure Manager

Managing The Digital Network Workforce Transformation

VXLAN EVPN Fabric and automation using Ansible

PSOACI Why ACI: An overview and a customer (BBVA) perspective. Technology Officer DC EMEAR Cisco

Internet of Things Field Network Director

Introduction to Cisco IoT Tools for Developers IoT 101

Science One CS : Getting Started

Customer s journey into the private cloud with Cisco Enterprise Cloud Suite

Automating Cisco Spark with Cloud Integration Cisco DevNet Webinar Series

Cisco Spark. Questions? Use Cisco Spark to communicate with the speaker after the session. How

Python Project Documentation

Contiv installation and integration with ACI. LTRCLD-2003

Ipswitch: The New way of Network Monitoring and how to provide managed services to its customers

Real time Location Services Overview and Use cases

Serviceability of SD-WAN

Building Applications with IOx

CS 1110, LAB 1: EXPRESSIONS AND ASSIGNMENTS First Name: Last Name: NetID:

Python Programming Exercises 1

Routing Underlay and NFV Automation with DNA Center

Cloud-Ready WAN For IAAS & SaaS With Cisco s Next- Gen SD-WAN

DevNet Workshop-Learning Cisco platform Exchange Grid (pxgrid) Dynamic Topics

Designing and Implementing Cisco Network Programmability (NPDESI) v1.0

Tour the latest Cisco Spark API features

Chris Simpkins (Georgia Tech) CS 2316 Data Manipulation for Engineers Python Overview 1 / 9

Cisco SD-Access Building the Routed Underlay

Cisco DNA Center and Italtel Netwrapper Evolution: Network and Applications come together

Using Workload Automation to Optimize Hybrid Cloud Estates

Cisco VIRL. The Swiss-Army Knife of Network Simulators. Simon Knight, Software Engineer Brian Daugherty, Technical Leader.

Stateless Multicast with Bit Indexed Explicit Replication

NetBrain Technologies: Achieving Agile Network Operations: How Automation Can Improve Visibility Across Hybrid Infrastructures

contribution-guide.org Release

VRF, MPLS and MP-BGP Fundamentals

Programming in Python Advanced

Gerd Doeben-Henisch February 12, Steps To Prepare Python Environment 2. 3 Use Python Interactive Console 3

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

Cisco IOS XR Programmability for Cloud-Scale Networking

withenv Documentation

AlgoSec: How to Secure and Automate Your Heterogeneous Cisco Environment

The Transformation of Media & Broadcast Video Production to a Professional Media Network

Model-Driven Telemetry and Analytics

CS Programming Languages: Python

dh-virtualenv Documentation

Transcription:

DEVNET-3602 Coding 1002 - Getting Started with Python Matthew DeNapoli, DevNet Developer Evangelist

Cisco Spark How Questions? Use Cisco Spark to communicate with the speaker after the session 1. Find this session in the Cisco Live Mobile App 2. Click Join the Discussion 3. Install Spark or go directly to the space 4. Enter messages/questions in the space cs.co/ciscolivebot#devnet-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public

Agenda Why Python and How to get it? Breaking Down Python Code Interacting with Python Live What are Libraries and How to Use Them Using pip to Install Libraries Virtual Environments Foundational Libraries

Why Python and How to get it?

Python is powerful... and fast; plays well with others; runs everywhere; is friendly & easy to learn; is Open. http://www.python.org/about

Why Python for Network Engineers Readable and easy to learn Widely available and Open Source Windows, Mac, Linux Routers & Switches Many relevant code samples Lots of training resources DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 7

How to get Python? You might already have it http://python.org/downloads Package Management Tools Example: Homebrew for Mac brew install python2 brew install python3 DevNet$ python version Python 2.7.12 DevNet$ python Python 2.7.12 (default, Oct 11 2016, 05:20:59) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 8

Breaking Down Python Code

example1.py Script Structure and Format Importing and using packages Variable declaration and usage Function creations and usage Basic Error Handling DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 10

example1.py Script Structure and Format Importing and using packages Variable declaration and usage Function creations and usage Basic Error Handling DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 11

example1.py Script Structure and Format Importing and using packages Variable declaration and usage Function creations and usage Basic Error Handling DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 12

example1.py Testing the Code DevNet$ python example1.py You must provide a number as a parameter to this script Example: python example1.py 12 DevNet$ python example1.py 5 10.0 DevNet$ python example1.py 45.4 90.8 netprog_basics/programming_fundamentals/python_part_1/example1.py 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public

example2.py Reading from and writing to files The with statement Requesting interactive user input Writing to the command line 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public

example2.py Reading from and writing to files The with statement Requesting interactive user input Writing to the command line DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 15

example2.py Reading from and writing to files The with statement Requesting interactive user input Writing to the command line DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 16

example2.py Testing the Code DevNet$ python example2.py What is your name? Hank Adding new log entry Log File Contents ----------------- Entry logged at: 2017-07-25 23:11:16.168003 by Hank Entry logged at: 2017-07-26 00:17:03.199335 by Hank netprog_basics/programming_fundamentals/python_part_1/example2.py netprog_basics/programming_fundamentals/python_part_1/example2.log 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public

example3.py Creating and using dictionaries Creating and using lists Working with for loops Conditional statements DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 18

example3.py Creating and using dictionaries Creating and using lists Working with for loops Conditional statements DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 19

example3.py Creating and using dictionaries Creating and using lists Working with for loops Conditional statements DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 20

example3.py Testing the Code part 1 DevNet$ python example3.py The author's name is Hank. His favorite color is green. The current colors are: blue green red What is your favorite color? green You have the same favorite as Hank. DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 21

example3.py Testing the Code part 2 DevNet$ python example3.py The author's name is Hank. His favorite color is green. The current colors are: blue green red What is your favorite color? orange That's a new color, adding it to the list! There are now 4 colors in the list. The color you added was orange. DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 22

Interacting with Python Live

What is and Why use Interactive Python Real-time enter code and see results Experiment and learning Debug scripts that aren t working Build on the output of a script DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 24

Accessing Interactive Python python i Basic command line python i script.py Run a script and leave session active idle Basic interpreter client written in Python Virtual Environment python m idlelib.idle DevNet$ python -i Python 3.6.2 (default, Jul 17 2017, 16:44:45) [GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from pprint import pprint >>> pprint("hello World") 'Hello World' >>> exit() DevNet$ python -i example1.py 21 42.0 >>> doubler(3) 6 >>> exit() DevNet$ idle DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 25

Helpful Interactive Python Commands dir() Return all variable, classes, objects (collectively called names ) available dir(name) Return attributes for an object help(name) Built-in help system. Displays docs and info for object DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 26

Demo

What are Libraries and How to Use Them

Python Libraries (Modules, Applications, etc) Any Python code outside of your script you want to use Provide some capability or data you need Included with statements from library import name import library DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 29

Python Libraries (Modules, Applications, etc) Any Python code outside of your script you want to use Provide some capability or data you need Included with statements from library import name import library DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 30

Where to get Libraries Write them yourself Example: common_vars Included with Python itself Example: datetime, os, sys, json From Python Package Index (PyPI) Example: pip install requests Download and install manually Example: ACI Toolkit from GitHub netprog_basics/programming_fundamentals/python_part_2/common_vars.py 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public

Using pip to Install Libraries

Pip, The Python Package Installer Python 2.7.6 and Python 3.4 or greater includes by default Integrates with PyPI for packages Install, Upgrade, and Uninstall packages requirements.txt in projects provide input to pip DevNet$ pip --version pip 9.0.1 DevNet$ pip install pyang Collecting pyang Downloading pyang-1.7.3-py2.py3-none-any.whl (326kB) 100% 327kB 1.3MB/s Installing collected packages: pyang Successfully installed pyang-1.7.3 DevNet$ pip install -r requirements.txt Collecting requests (from -r requirements.txt (line 1)) Downloading requests-2.18.2-py2.py3-none-any.whl (88kB) 100% 92kB 662kB/s. {OUTPUT TRUNCATED}. Successfully installed requests-2.18.2 six-1.10.0 urllib3-1.22 netprog_basics/programming_fundamentals/python_part_2/requirements.txt 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public

pip Commands to Know Install a package pip install package Upgrade a package pip install --upgrade package Uninstall a package pip uninstall package View all packages installed pip freeze Install requirements.txt pip install r requirements.txt DevNet$ pip freeze asn1crypto==0.22.0 bcrypt==3.1.3 certifi==2017.4.17 cffi==1.10.0 chardet==3.0.4 cryptography==2.0 flake8==3.3.0 idna==2.5 lxml==3.8.0 mccabe==0.6.1 ncclient==0.5.3 paramiko==2.2.1 pyang==1.7.3 pyasn1==0.2.3 pycodestyle==2.3.1 pycparser==2.18 pyflakes==1.5.0 PyNaCl==1.1.2 requests==2.18.2 six==1.10.0 urllib3==1.22 DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 34

Virtual Environments

What is a Virtual Environment (venv) Build isolated, fully functional Python environments on a single workstation Virtual Environments can Run different versions of Python Have different libraries installed Have different versions of libraries installed DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 36

Setting Up a Virtual Environment Install the virtualenv library pip install virtualenv Create the Virtual Environment virtualenv name Specify Python Version virtualenv name --python=python3 Activate Virtual Environment source name/bin/activate* Deactivate Virtual Environment deactivate DevNet$ pip install virtualenv Successfully installed virtualenv DevNet$ virtualenv venv New python executable in /private/tmp/venv/bin/python2.7 Also creating executable in /private/tmp/venv/bin/python Installing setuptools, pip, wheel...done. DevNet$ virtualenv venv2 --python=python3 Running virtualenv with interpreter /usr/local/bin/python3 Using base prefix '/usr/local/cellar/python3/3.6.2/frameworks/python.fr amework/versions/3.6' New python executable in /private/tmp/venv2/bin/python3.6 Also creating executable in /private/tmp/venv2/bin/python Installing setuptools, pip, wheel...done. DevNet$ source venv/bin/activate (venv) DevNet$ * Commands on Windows Platforms slightly different DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 37

Installing Python Libraries in Virtual Environments Once activated, no different (venv) DevNet$ pip install pyang Collecting pyang Downloading pyang-1.7.3-py2.py3-none-any.whl (326kB) 100% 327kB 1.3MB/s Installing collected packages: pyang Successfully installed pyang-1.7.3 (venv) DevNet$ pip install -r requirements.txt Collecting requests (from -r requirements.txt (line 1)) Downloading requests-2.18.2-py2.py3-none-any.whl (88kB) 100% 92kB 662kB/s. {OUTPUT TRUNCATED}. Successfully installed requests-2.18.2 six-1.10.0 urllib3-1.22 DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 38

Foundational Libraries

Core Python Libraries to Know and Love Pretty Print from pprint import pprint Python Interpreter Utilities import sys Operating System Interfaces import os Date and Time Utilities import datetime * Many libraries included with core Python, see docs DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 40

Prettier Printing with pprint Better formatting than default print() function https://docs.python.org/3/library/pprint.html DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 41

Access Details about Python with sys Access to some details/variables concerning running state Access command line arguments with sys.argv[] Access to functions that interact with the interpreter Exit Python with specific error message sys.exit("message") DevNet$ python -i common_vars.py "CLI Arg 1" "CLI Arg 2" >>> >>> import sys >>> >>> sys.argv[1] 'CLI Arg 1' >>> sys.argv[2] 'CLI Arg 2' >>> >>> sys.exit("error Occurred") Error Occurred https://docs.python.org/3/library/sys.html DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 42

Interact with Files, Paths, and Environment with os Access and manipulate directories and files Note: Opening files can be done with open(filename) Access and Manipulate Environment Variables os.environ[var_name] https://docs.python.org/3/library/os.html DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 43

Get your Date and Time Correct with datetime Create, format, and manipulate dates and times Time arithmetic! Work with timestamps and other representations https://docs.python.org/3/library/datetime.html DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 44

Demo

Summing up

Review Why Python and How to get it? Breaking Down Python Code Script structure, imports, variables, functions & error handling Working with files and command line input Using lists and dictionaries, loops, and conditional statements Interacting with Python Live How to access the Python interpreter Using dir() and help() to explore and learn Understand what Python libraries are and how to use them Looked at Python Virtual Environments, why and how to use them Explored core Python libraries for displaying data, managing running scripts, and working with the operating system DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 47

Cisco Spark How Questions? Use Cisco Spark to communicate with the speaker after the session 1. Find this session in the Cisco Live Mobile App 2. Click Join the Discussion 3. Install Spark or go directly to the space 4. Enter messages/questions in the space cs.co/ciscolivebot#devnet-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public

Please complete your Online Session Evaluations after each session Complete 4 Session Evaluations & the Overall Conference Evaluation (available from Thursday) to receive your Cisco Live T-shirt All surveys can be completed via the Cisco Live Mobile App or the Communication Stations Complete Your Online Session Evaluation Don t forget: Cisco Live sessions will be available for viewing on-demand after the event at www.ciscolive.com/global/on-demand-library/. 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public

Continue Your Education Demos in the Cisco campus Walk-in Self-Paced Labs Tech Circle Meet the Engineer 1:1 meetings Related sessions DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 50

Continue Your Education DevNet: https://developer.cisco.com DevNet Learning Labs: https://learninglabs.cisco.com DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 51

Got more questions? Come find me! mdenaol@cisco.com @denapom11 http://github.com/denapom11 @CiscoDevNet facebook.com/ciscodevnet/ http://github.com/ciscodevnet DEVNET-3602 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 52

Thank you