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