Slide 1

Slide 1 text

python for Beginners - Part 2 Pythons Package Ecosystem Marcel Neidinger Associate Solutions Engineer, Collaboration [email protected] 14. November 2019

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

The standard library

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

The package ecosystem

Slide 12

Slide 12 text

What if they did not think about including it? 6

Slide 13

Slide 13 text

Community 7

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Stop talking, give me the code! 9

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

$ pip install webexteamssdk 10

Slide 18

Slide 18 text

$ pip install --upgrade webexteamssdk 10

Slide 19

Slide 19 text

$ pip uninstall webexteamssdk 10

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Meet Virtualenv 12

Slide 22

Slide 22 text

$ virtualenv -p python3 test venv 12

Slide 23

Slide 23 text

$ source test venv/bin/activate 12

Slide 24

Slide 24 text

$ pip freeze > requirements.txt 12

Slide 25

Slide 25 text

Web Requests

Slide 26

Slide 26 text

Meet requests requests 13

Slide 27

Slide 27 text

Meet requests requests HTTP(S) for Humans 13

Slide 28

Slide 28 text

Meet requests requests HTTP(S) for HumansTM 13

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

Permanent Storage - Redis

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

Questions? Nothing working? [email protected] 19

Slide 40

Slide 40 text

Useful resources

Slide 41

Slide 41 text

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