Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python for Systems Engineers - Part 2

Python for Systems Engineers - Part 2

Python is one of the most popular programming languages of our time and the powerhouse behind most of Cisco’s API efforts. If there is a Cisco API chances are high that there will be a software development kit for that API in python. In this 3-part webinar series we will get our hands dirty with python.

Part 2 covers the python package ecosystem, virtual environments and some packages that might be useful when developing proof-of-concepts.

Marcel Neidinger

November 14, 2019
Tweet

More Decks by Marcel Neidinger

Other Decks in Technology

Transcript

  1. python for Beginners - Part 2 Pythons Package Ecosystem Marcel

    Neidinger Associate Solutions Engineer, Collaboration [email protected] 14. November 2019
  2. What is the plan? • Part 1: Introduction to python3

    • Part 2: Introduction to some useful python packages • Part 3: Hands-On - Let’s code a small MVP in one hour 1
  3. About these Slides These slides are deliberately filled with content

    and can serve as a reference for you. Code sample 1 p r i n t ( ” This i s a code snippet ” ) Exercise 2
  4. About these Slides These slides are deliberately filled with content

    and can serve as a reference for you. Code sample 1 p r i n t ( ” This i s a code snippet ” ) Exercise Exercise: Some trivial proofs Proof, for any decission problem P1 ∈ NP and P2 ∈ P that P1 >p P2 holds. 2
  5. Build-in functions • Don’t reinvent the wheel! • → Your

    programming skills are (very probably) worse then those who implemented the standard libary • Batteries included programming 4
  6. json - Parsing and Creating json 1 import json •

    Load a json string into a dict 1 j s o n s t r = ’ { ” best−api ” : ”Webex Teams API ” } ’ res = json . loads ( j s o n s t r ) 3 p r i n t ( ” The best API i s ” + s t r ( res [ ’ best−api ’ ] ) ) 5
  7. json - Parsing and Creating json 1 import json •

    Load a json string into a dict 1 j s o n s t r = ’ { ” best−api ” : ”Webex Teams API ” } ’ res = json . loads ( j s o n s t r ) 3 p r i n t ( ” The best API i s ” + s t r ( res [ ’ best−api ’ ] ) ) • Create json from a dict 1 d = { ’message ’ : ” Hi , I am a message ” , ’ toPersonEmail ’ : ” jgomez2@cisco .com” } json representation = json . dumps( d ) 3 p r i n t ( json representation ) 5
  8. Meet PyPi • Central repository for python code • 204

    720 projects (2 of them from me) • 1 540 109 releases • 2 294 855 files • 385 989 user 8
  9. Meet requests (Contd.) Making a GET request 1 import requests

    response = requests . get ( ” https : / / deckofcardsapi .com/ api / deck / new / s h u f f l e / ” ) 3 p r i n t ( response . status code ) 14
  10. Meet requests (Contd.) Making a GET request 1 import requests

    response = requests . get ( ” https : / / deckofcardsapi .com/ api / deck / new / s h u f f l e / ” ) 3 p r i n t ( response . status code ) Accessing the json 1 import requests response = requests . get ( ” https : / / deckofcardsapi .com/ api / deck / new / s h u f f l e / ” ) 3 payload = response . json ( ) deck = payload [ ’ deck id ’ ] 5 p r i n t ( ” The id of our deck of cards i s : ” + s t r ( deck ) ) 14
  11. Meet requests (contd.) Making a POST request (with data) 1

    import requests payload = { ” deck count ” : 4} 3 response = requests . post ( ” https : / / deckofcardsapi .com/ api / deck / new / s h u f f l e / ” , data= payload ) deck = response . json ( ) [ ’ deck id ’ ] 15
  12. Meet requests (contd.) Making a POST request (with data) 1

    import requests payload = { ” deck count ” : 4} 3 response = requests . post ( ” https : / / deckofcardsapi .com/ api / deck / new / s h u f f l e / ” , data= payload ) deck = response . json ( ) [ ’ deck id ’ ] Draw a card import requests 2 deck = response . json ( ) [ ’ deck id ’ ] 4 u r l = ” https : / / deckofcardsapi .com/ api / deck / ” + s t r ( deck ) + ” / draw /? count=2 ” 6 resp = requests . get ( u r l ) r e s u l t = resp . json ( ) 8 f o r card in r e s u l t [ ’ cards ’ ] : p r i n t ( ”We got a ” + card [ ’ s u i t ’ ] + ” ” + card [ ’ value ’ ] ) 15
  13. Meet requests (contd.) Adding Authentification 1 import requests 3 room

    id = ”<INSERT ROOM ID HERE>” token = ”<INSERT TOKEN HERE>” 5 headers = { 7 ” Authorization ” : ” Bearer ” + s t r ( token ) } 9 payload = { 11 ” markdown ” : ” Hello from ∗∗python ∗∗ ” , ” roomId ” : room id , 13 } a p i u r l = ” https : / / api . ciscospark .com/ v1 / messages ” 15 resp = requests . post ( a p i u r l , headers=headers , data=payload ) 16
  14. The WebexTeamsSDK • Often API wrapper exist • Let’s rewrite

    the code above 1 from webexteamssdk import WebexTeamsAPI 3 room id = ”<INSERT ROOM ID HERE>” token = ”<INSERT TOKEN HERE>” 5 api = WebexTeamsAPI( access token=token ) 7 api . messages . create ( roomId=room id , markdown=” Test12345 ” ) 17
  15. Meet Redis • Remote Dictionary Server • High performance key-value

    store • Side-note: Start a Redis instance with 1 $ docker run −−name=redis −devel −−publish =6379:6379 −−hostname=redis −−r e s t a r t =on− f a i l u r e −−detach redis : l a t e s t 18
  16. Meet redis (contd.) Connect to redis and set/get a key

    1 import redis 3 r = redis . Redis ( ) r . set ( ” test key ” , ” This i s a t e s t key ” ) 5 res = r . get ( ” test key ” ) i f res i s not None : 7 p r i n t ( ” The value i s ” + s t r ( res . decode ( ’ utf −8 ’ ) ) ) 19
  17. Links • The python3 standard library: https://docs.python.org/3/library/ • Installing python

    for development: Mac: https://docs.python-guide.org/starting/install3/osx/ Windows: https://docs.python-guide.org/starting/install3/win/ Linux: https://docs.python-guide.org/starting/install3/linux/ • The core concepts of python: https://www.python-course.eu/python3_history_and_philosophy.php • Setup the atom editor for python development: https://hackernoon.com/ setting-up-a-python-development-environment-in-atom-466d7f48e297