tto / docs / python / python notes

virtual environment and pwsh

python3 -m venv ./myenv
& ./myenv/bin/Activate.ps1
which python3
which python

requirements

put packages you import into a file requirements.txt so they can be installed when setting up an environment.

install with: pip install -r requirements.txt

use the name of the package with or without required version in the file:

matplotlib
pandas==1.5.2

web requests

use the requests module to make web requests. the response has a method json() that will return a json dictionary. if the api returns an array of json objects, r.json() will be a list so we need to access an item in the list before we can access its properties.

def get_auth_me():
    r = requests.get("/.auth/me")
    return r.json()[0]

def get_tenant_id():
    claims = get_auth_me()['user_claims']
    # iterate through claims and return the value of the claim with typ tenantid 
    for item in claims:
        if item['typ'] == 'http://schemas.microsoft.com/identity/claims/tenantid':
            return item['val']

print(get_tenant_id())

python and homebrew

check this [python-homebrew]