2.5 Python Libraries

Key Points

  • Understand pip
  • Understand Python modules and learn how to introduce modules, standard libraries, and third-party libraries
  • Learn about JSON library and requests library in Python

Preparation Before Class

Installing Requests Library

Install it via pip in the command prompt:

pip install requests

If you see "Successfully installed requests-xxxx", it means the installation is done.

Course Content

Pip and Python Libraries

1. Python Pip

Pip is a package-management system written in Python and is used to search, download, install, and uninstall Python packages.

2. Python Modules

If you define variables or functions in Python and then exit and re-enter the Python interpreter, those settings will not be stored.

To solve this problem, Python can store these definitions in files called modules, which are used in scripts or the interactive interpreter.

Creating a Module:

#!/usr/bin/python3
# Filename: support.py
 
def print_func( par ):
    print ("Hello : ", par)
    return

Using a Module:

#!/usr/bin/python3
# Filename: test.py
 
# import the module
import support
 
# you can call the function now
support.print_func("Runoob")

3. Python Packages

Packages allow for a hierarchical structuring of the module namespace using dot notation. For example, if a module's name is A.B, then it represents a submodule B in package A.

4. Python Libraries

In addition to built-in modules, Python also has many third-party modules.

In general, all third-party modules are registered on PyPI - the Python Package Index. After finding the name of the module on the website, you can install it in your terminal or command prompt.

Library Name

Description

Math

Provide access to the mathematical functions for floating-point numbers

NumPy

Provide multi-dimensional arrays and matrices, and tools for working with these arrays and matrices

Matplotlib

Create static, animated, and interactive visualizations

Pandas

Provide access to data analysis and manipulation tools

Scrapy

Provides fast web crawling and high-level screen scraping

5. Importing a Library

By executing an import statement, you can import a library and use its functions.

import math
import numpy as np
from numpy import *

JSON Library

JavaScript Object Notation (JSON) is a standard text-based format that uses human-readable text to store and transmit data objects. Although JSON is originally designed for JavaScript to write data structures, you can handle JSON data without any knowledge of JavaScript.

This is an example of JSON data:

{"name":"Zophie","isCat":true,"miceCaught":0,"napsTaken":37.5,"felineIQ":null}

1. Importing JSON Library

After importing the JSON module, you can call its relevant functions. Note that in JSON keys and string values must be enclosed in double quotes.

import json

Here is a comparison table that shows how Python types are converted to JSON types:

Python Type

JSON Type

dict

object

list, tuple

array

str, unicode

string

int, long, float

number

True

true

False

false

None

null

2. Format Conversion

1) json.loads

The json.loads() function converts a JSON-formatted string to a Python object.

import json
a='{"name":"Zophie","isCat":true,"miceCaught":0,"napsTaken":37.5,"felineIQ":null}'
b=json.loads(a)
print(b)

2) json.dumps()

The json.dumps() function converts a Python object to a JSON-formatted string. The Python object can be a dictionary, a list, an integer, a float, a string, a boolean, or None.

import json
a={'name': 'Zophie', 'isCat': True, 'miceCaught': 0, 'napsTaken': 37.5, 'felineIQ': None}
b=json.dumps(a)
print(b)

3. Differences Between JSON and a Dictionary

The two are similar because they both represent a collection of key-value pairs.

However, a dictionary is a built-in data structure in Python, while JSON is a data exchange format. A dictionary has many built-in functions and can be called in multiple ways, while JSON is a format for data packaging and does not have such functions. Moreover, the JSON format has some strict rules and limitations. For example, keys and strings must be enclosed in double quotes, but there is no such rule for dictionaries.

Requests Library

1. Importing Requests Library

import requests

2. Components of Requests

requests.request(method, url, ``*``*``kwargs)

3. Using Requests

To access a page via GET, input:

>>> import requests
>>> r = requests.get('https://www.google.com/') # Google
>>> r.status_code
200
>>> r.text
r.text

4. Request Format

The most common request formats for APIs are json/application and x-www-form-urlencoded. When calling APIs, you need to process the parameters according to the request format of the API.

  • If the request format of the API is application/json:
import requests
import urllib.parse
import json

url = "https://api.example.com/path/to/api"

payload = {"image":"http://www.xxxx.com/xxxx.png"}

headers = {
  'Authorization': 'APPCODE xxxxxxxxxxxxxxx',
  'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data = json.dumps(payload))

json.loads(response.text)
  • If the request format of the API is x-www-form-urlencoded:

URL encoding is a method to encode data into valid URL format.

Before URL encoding:

{"image":"http://www.xxxx.com/xxxx.png"}

After URL encoding:

'image=http%3A%2F%2Fwww.xxxx.com%2Fxxxx.png'
import requests
import urllib.parse
import json

url = "https://api.example.com/path/to/api"

payload = {"image":"http://www.xxxx.com/xxxx.png"}

headers = {
  'Authorization': 'APPCODE xxxxxxxxxxxxxxx',
  'Content-Type': 'application/x-www-form-urlencoded'
}

response = requests.request("POST", url, headers=headers, data = urllib.parse.urlencode(payload))

json.loads(response.text)

Was this information helpful?
Yes
NoNo
Need more help? Contact support