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

    View Slide

  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

    View Slide

  3. About these Slides
    These slides are deliberately filled with content and can serve as a reference for you.
    2

    View Slide

  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
    2

    View Slide

  5. 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

    View Slide

  6. The standard library

    View Slide

  7. Batteries included?
    • pythones comes with a lot of functionality included
    3

    View Slide

  8. 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

    View Slide

  9. 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

    View Slide

  10. 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 ’ : ” [email protected] .com” }
    json representation = json . dumps( d )
    3 p r i n t ( json representation )
    5

    View Slide

  11. The package ecosystem

    View Slide

  12. What if they did not think about including it?
    6

    View Slide

  13. Community
    7

    View Slide

  14. 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

    View Slide

  15. Stop talking, give me the code!
    9

    View Slide

  16. Meet Pip
    • (De-facto) standard package manager for python
    • Command-line based
    10

    View Slide

  17. $ pip install webexteamssdk
    10

    View Slide

  18. $ pip install --upgrade webexteamssdk
    10

    View Slide

  19. $ pip uninstall webexteamssdk
    10

    View Slide

  20. What if I need different versions of the same
    package?
    11

    View Slide

  21. Meet Virtualenv
    12

    View Slide

  22. $ virtualenv -p python3 test venv
    12

    View Slide

  23. $ source test venv/bin/activate
    12

    View Slide

  24. $ pip freeze > requirements.txt
    12

    View Slide

  25. Web Requests

    View Slide

  26. Meet requests
    requests
    13

    View Slide

  27. Meet requests
    requests HTTP(S) for Humans
    13

    View Slide

  28. Meet requests
    requests HTTP(S) for HumansTM
    13

    View Slide

  29. 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

    View Slide

  30. 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

    View Slide

  31. 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

    View Slide

  32. 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

    View Slide

  33. Meet requests (contd.)
    Adding Authentification
    1 import requests
    3 room id = ””
    token = ””
    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

    View Slide

  34. The WebexTeamsSDK
    • Often API wrapper exist
    • Let’s rewrite the code above
    17

    View Slide

  35. The WebexTeamsSDK
    • Often API wrapper exist
    • Let’s rewrite the code above
    1 from webexteamssdk import WebexTeamsAPI
    3 room id = ””
    token = ””
    5
    api = WebexTeamsAPI( access token=token )
    7 api . messages . create ( roomId=room id , markdown=” Test12345 ” )
    17

    View Slide

  36. Permanent Storage - Redis

    View Slide

  37. 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

    View Slide

  38. 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

    View Slide

  39. Questions? Nothing working?
    [email protected]
    19

    View Slide

  40. Useful resources

    View Slide

  41. 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

    View Slide