python3 advanced programming (3)

Python3 built-in functions

Python3 has a wealth of built-in functions, which can facilitate us to perform various operations. Here are some commonly used built-in functions:

  • print(): Print out information.

  • len(): Returns the length (number of elements) of a sequence object.

  • range(): Used to generate a sequence of numbers within a specified range.

  • input(): Used to read data entered by the user from the console.

  • str(): Convert an object to a string.

  • int(): Converts an object to an integer.

  • float(): Converts an object to a floating point number.

  • list(): Convert an object to a list.

Use these built-in functions to easily complete various tasks, such as printing out information, processing strings, performing numerical calculations, and so on.

python3 built-in functions

Python3 MongoDB

MongoDB is a NoSQL database that stores data using documents. In Python3, we can use the PyMongo module to connect to the MongoDB database and perform various operations.

First, we need to install the PyMongo module. You can use the following command to install: pip3 install pymongo

Then, we can use the following code to connect to the MongoDB database:

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]

This will connect to the “customers” collection in the MongoDB database named “mydatabase”. We can insert data into a collection using the insert_one() method, find data using the find_one() method, update data using the update_one() method, and delete data using the delete_one() method.

Using MongoDB can easily store and manage large amounts of data, especially unstructured data.

Python3 urllib

The urllib module in Python3 is one of the standard libraries for manipulating URLs. It provides a range of functions to handle URLs, including:

  • send HTTP request

  • Parse the HTTP response

  • Handling Cookies

  • handle exception

The following are some commonly used functions and classes in the urllib module:

  • urllib.request.urlopen(): Used to open URLs.

  • urllib.request.Request(): Used to construct HTTP requests.

  • urllib.request.build_opener(): Used to create a custom URL opener.

  • urllib.request.ProxyHandler(): used to set the proxy server.

  • urllib.request.HTTPCookieProcessor(): Used to process Cookies.

  • urllib.request.HTTPError(): Used to handle HTTP errors.

  • urllib.request.urlretrieve(): Used to download files.

Here is an example of sending a GET request using the urllib module:

import urllib.request

response = urllib.request.urlopen('<http://www.example.com/>')
html = response. read()
print(html)

Use the urllib module to easily send HTTP requests, handle responses, manage cookies, and more.

uWSGI is a web server that can be used to deploy Python applications to production environments. It supports multiple protocols and web server interfaces, and provides various options and configurations, making it very flexible and customizable.

To use uWSGI, you need to install it first. uWSGI can be installed on Ubuntu with the following command:

sudo apt-get install uwsgi

Once installed, you can start uWSGI with the following command:

uwsgi --http :8080 --wsgi-file myapp.py --callable app

This starts a web server listening on port 8080 and forwards requests to a callable called “app” in a Python application called “myapp.py”. You can change the port and application name as needed.

To run uWSGI as a service, automatically started at system boot, you can create a systemd unit file. Here is an example:

[Unit]
Description=uWSGI Service

[Service]
ExecStart=/usr/bin/uwsgi --ini /etc/uwsgi/sites/myapp.ini
Restart=always
User=myuser
Group=mygroup
Environment=PATH=/home/myuser/myenv/bin
Environment=PYTHONPATH=/home/myuser/myapp

[Install]
WantedBy=multi-user.target

This file will start the application specified in the uWSGI configuration file named “myapp.ini”. It also defines the user and group for the uWSGI process, as well as the path to the application and the path to the Python environment.

Deploying Python applications into production is easy with uWSGI and offers a high degree of customizability and flexibility.

Python3 pip

pip is a package manager in Python3, which can be used to install, upgrade and uninstall Python packages. It can download and install Python packages from the Python Package Index (PyPI).

Every Python3 installation comes with pip, so you can use it directly from the command line. The following are some commonly used pip commands:

  • pip3 install package_name: Install the specified Python package.

  • pip3 uninstall package_name: Uninstall the specified Python package.

  • pip3 freeze: List all installed Python packages and their version numbers.

  • pip3 freeze > requirements.txt: Save all installed Python packages and their version numbers to a file.

  • pip3 install -r requirements.txt: Install all Python packages and their version numbers from the file.

Python packages can be easily managed using pip, making development easier and more efficient.

Alternatively, you can use pip to install third-party Python libraries. For example, if you want to install the pandas library, you can use the following command:

pip3 install pandas

This will download and install the latest version of the pandas library from PyPI. If you want to install a specific version of the pandas library, you can use the following command:

pip3 install pandas==1.1.5

This will install pandas library version 1.1.5.

In addition, pip also supports some other options and parameters, such as:

  • -upgrade: Upgrade installed Python packages.

  • -user: Install the Python package into the current user’s home directory.

  • i: Specifies the PyPI mirror address to use.

  • r: Installs a Python package and its version number from the specified file.

With these options and parameters, you can use pip more flexibly to manage Python packages.

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledgePython entry skill treePreliminary knowledgeCommon development tools 258933 people are studying systematically