Python
此内容尚不支持你的语言。
Information
Python is a high-level, interpreted programming language known for its clear syntax and readability, making it excellent for beginners and experienced developers alike. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming. Python’s vast ecosystem of libraries and frameworks—such as Django for web development, Pandas for data analysis, and TensorFlow for machine learning—makes it incredibly versatile for a wide range of applications.
Python is widely used in scientific computing, data mining, and artificial intelligence, thanks to its simplicity and the powerful libraries that extend its capabilities. It’s also a favorite for automation, scripting, and rapid application development across diverse fields. The language’s design and module architecture encourage code reusability and maintainability.
Setup
These are various tutorials for setting up python on different operating systems!
Windows
-
Download Python Installer:
- Go to the official Python website.
- Click on the “Downloads” section and select the latest version for Windows.
-
Run the Installer:
- Double-click the downloaded file to run the installer.
- Check the box that says “Add Python to PATH.”
- Click on “Customize installation” if you want to choose specific features, otherwise, click on “Install Now.”
-
Verify the Installation:
- Open Command Prompt (Win + R, type
cmd
, and press Enter). - Type
python --version
and press Enter. You should see the Python version you installed. - Also, check
pip
(Python package installer) by typingpip --version
.
- Open Command Prompt (Win + R, type
macOS
-
Download Python Installer:
- Go to the official Python website.
- Click on the “Downloads” section and select the latest version for macOS.
-
Run the Installer:
- Open the downloaded
.pkg
file and follow the installation instructions. - The installer will automatically add Python to your PATH.
- Open the downloaded
-
Verify the Installation:
- Open Terminal (Cmd + Space, type
Terminal
, and press Enter). - Type
python3 --version
and press Enter. You should see the Python version you installed. - Also, check
pip3
by typingpip3 --version
.
- Open Terminal (Cmd + Space, type
Linux (Ubuntu)
-
Update and Upgrade System:
- Open Terminal (Ctrl + Alt + T).
- Run the following commands:
sudo apt update sudo apt upgrade
-
Install Python:
- Run the command to install Python (replace
3.x
with the version you want, e.g.,3.10
):sudo apt install python3.x
- Install
pip
:sudo apt install python3-pip
- Run the command to install Python (replace
-
Verify the Installation:
- Type
python3 --version
and press Enter. You should see the Python version you installed. - Also, check
pip3
by typingpip3 --version
.
- Type
Virtual Environments
-
Install
virtualenv
:pip3 install virtualenv
-
Create a virtual environment:
virtualenv myenv
-
Activate the virtual environment:
- Windows:
myenv\Scripts\activate
- macOS/Linux:
source myenv/bin/activate
- Deactivate the virtual environment by typing
deactivate
.
- Windows:
pip
pip
is the package manager for Python. It is used to install and manage software packages written in Python.
pip
is included with most Python distributions, and it can be installed separately for other distributions.
To use pip
, you need to know the name of the package you want to install.
You can find the name of the package by searching the Python Package Index (PyPI).
Once you know the name of the package, you can install it with the following command:
pip install <$package_name>
Replacing the syntax of <$package_name>
with the package you would like to install from the PyPI libraries.
In this example, we will look into the install of the requests
package via pip
.
pip install requests
-
Install Packages Using
pip
:- To install a package:
pip install package_name
- To list installed packages:
pip list
- To install a package:
-
Upgrade
pip
:- Run the command:
python -m pip install --upgrade pip
- Run the command:
FastAPI
FastAPI is a modern and fast web framework designed for building APIs with Python 3.7+ using standard Python type hints. Offering high performance, FastAPI leverages asynchronous programming to achieve speeds comparable to those of Node.js and Go, making it ideal for developing scalable and efficient web applications. The framework is built on top of Starlette for handling web components and Pydantic for data validation and serialization, ensuring smooth data management. FastAPI stands out for its automatic interactive API documentation, featuring Swagger UI and ReDoc, which simplifies the process of exploring and testing API endpoints. The framework also supports OAuth2 authentication with JWT tokens, making it a robust choice for secure API development. Additionally, FastAPI’s design focuses on developer productivity, reducing code duplication and enhancing maintainability. By providing built-in support for data validation, serialization, and interactive documentation, FastAPI accelerates the development process, making it a popular choice for both small projects and large-scale enterprise applications.
Tutorial on Setting Up FastAPI
-
Prepare Dev Environment:
Ensure that Python 3.7 or later is installed on your system. You can download the latest version from the official Python website. Follow the installation instructions for your operating system. Verify the installation by opening a terminal or command prompt and running:
python --version
This should display the installed Python version.
Creating a virtual environment is a good practice to manage project dependencies. In your terminal or command prompt, navigate to your project directory and run:
python -m venv env
This command creates a virtual environment named
env
. Activate the virtual environment by running:- On Windows:
.\env\Scripts\activate
- On macOS/Linux:
source env/bin/activate
-
Install FastAPI and Uvicorn:
FastAPI requires an ASGI server to run. Uvicorn is a recommended ASGI server for FastAPI. Install FastAPI and Uvicorn within your virtual environment by running:
pip install fastapi uvicorn
This command installs both FastAPI and Uvicorn, enabling you to build and serve your API.
-
Create a Basic FastAPI Application:
Organize your project directory with a simple file tree structure to keep everything neat and manageable.
文件夹my_fastapi_project/
- main.py
- requirements.txt
- .gitignore
- main.py: The main application file where the FastAPI app is defined.
- requirements.txt: A file to list project dependencies, including
fastapi
anduvicorn
. - .gitignore: A file to specify which files and directories to ignore in version control (optional, but recommended).
Create a new Python file in your project directory, for example,
main.py
. Open this file in a text editor and add the following code:from fastapi import FastAPI app = FastAPI() @app.get("/") async def read_root(): return {"Hello": "World"} @app.get("/items/{item_id}") async def read_item(item_id: int, q: str = None): return {"item_id": item_id, "q": q}
This code defines a basic FastAPI application with two endpoints: a root endpoint (
/
) that returns a simple greeting and an endpoint (/items/{item_id}
) that returns an item ID and an optional query parameter. -
Run the FastAPI Application:
Use Uvicorn to run your FastAPI application. In your terminal or command prompt, navigate to your project directory and run:
uvicorn main:app --reload
The
--reload
option enables automatic reloads when code changes are detected, which is useful for development. Open a web browser and navigate tohttp://127.0.0.1:8000
to see the greeting message. Visithttp://127.0.0.1:8000/items/1?q=test
to see the item endpoint in action. -
Explore Automatic Interactive API Documentation:
FastAPI provides interactive API documentation automatically. With your application running, navigate to
http://127.0.0.1:8000/docs
to see the Swagger UI. This interface allows you to interact with your API endpoints directly from the browser. Another documentation interface is available athttp://127.0.0.1:8000/redoc
. -
Add More Functionality:
Expand your FastAPI application by adding more routes, handling different HTTP methods, and integrating data validation. FastAPI’s documentation provides detailed guidance on various features, including path parameters, query parameters, request bodies, and response models.
Let us begin by installing
Pydantic
, here is a quick command to do so:pip install pydantic
Next, to add a POST endpoint for creating items, modify
main.py
:from fastapi import FastAPI, HTTPException from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str price: float is_offer: bool = None @app.post("/items/") async def create_item(item: Item): return item
This code defines an
Item
model using Pydantic and a POST endpoint that accepts anItem
and returns it.By following these steps, you will have a fully functional FastAPI application ready for further development and customization.
Pydantic
Pydantic is a powerful data validation and settings management library for Python, leveraging Python type annotations to ensure robust and reliable data handling. By defining data models through type hints, Pydantic automates the validation and parsing process, converting input data into the appropriate types and structures. This functionality is particularly beneficial in scenarios requiring strict data integrity, such as API development and configuration management. Pydantic supports various standard library types, custom data types, and even complex nested data structures.
Built with performance in mind, Pydantic’s core validation logic is implemented in Rust, making it one of the fastest data validation libraries available. The library also includes features like JSON Schema generation, custom validators, and the ability to run in strict or lax modes, allowing for flexible data coercion and validation. With broad adoption, including use by major companies and integration with popular frameworks like FastAPI, Pydantic has become a cornerstone in the Python ecosystem for data validation.
Pydantic Tutorial
-
Setting up your project:
First, create a new directory for your project.
mkdir pydantic_project cd pydantic_project
Set Up a Virtual Environment:
Create and activate a virtual environment.
python3 -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
Install Pydantic Install Pydantic via pip.
pip install pydantic
-
Project Structure:
Create the following project structure:
文件夹pydantic_project/
文件夹venv/
- …
- main.py
文件夹models/
- user.py
文件夹utils/
- helpers.py
- .gitignore
- README.md
-
Create Pydantic Models:
Create a Pydantic model in
models/user.py
.# models/user.py from pydantic import BaseModel, EmailStr, Field from typing import List, Optional from datetime import date class Address(BaseModel): street: str city: str state: str zip_code: str class User(BaseModel): id: int name: str = Field(..., max_length=100) email: EmailStr is_active: bool = True addresses: List[Address] = [] date_of_birth: Optional[date] = None class Config: schema_extra = { "example": { "id": 1, "name": "John Doe", "email": "[email protected]", "is_active": True, "addresses": [ { "street": "123 Main St", "city": "Anytown", "state": "CA", "zip_code": "12345" } ], "date_of_birth": "1990-01-01" } }
-
Create Helper Functions:
Create helper functions in
utils/helpers.py
.# utils/helpers.py from models.user import User, Address from typing import List def create_user(data: dict) -> User: return User(**data) def filter_active_users(users: List[User]) -> List[User]: return [user for user in users if user.is_active] def add_address_to_user(user: User, address_data: dict) -> User: address = Address(**address_data) user.addresses.append(address) return user
-
Main Application Logic:
Create the main application logic in
main.py
.# main.py from models.user import User from utils.helpers import create_user, filter_active_users, add_address_to_user # Sample user data user_data = { "id": 1, "name": "John Doe", "email": "[email protected]", "is_active": True, "addresses": [ { "street": "123 Main St", "city": "Anytown", "state": "CA", "zip_code": "12345" } ], "date_of_birth": "1990-01-01" } # Create a user user = create_user(user_data) print(user) # Add a new address to the user new_address = { "street": "456 Elm St", "city": "Othertown", "state": "NY", "zip_code": "67890" } user = add_address_to_user(user, new_address) print(user) # Filter active users users = [user, create_user({**user_data, "id": 2, "is_active": False})] active_users = filter_active_users(users) print(active_users)
-
Run Your Application:
Run your application using the following command:
python main.py
-
Extending Valdiation:
Pydantic automatically validates data. For instance, if you try to create a user with an invalid email, it will raise a
ValidationError
.Custom Validators You can add custom validation methods within your model.
from pydantic import validator class User(BaseModel): # ... (other fields) @validator('name') def name_must_be_capitalized(cls, v): if not v.istitle(): raise ValueError('Name must be capitalized') return v
Serialization and Deserialization Pydantic models can easily be serialized to and deserialized from JSON.
user_json = user.json() new_user = User.parse_raw(user_json)
Schema Generation Pydantic can generate JSON Schemas for your models.
print(User.schema_json(indent=2))
You now have a detailed setup for a Pydantic project with models, helper functions, and application logic. Do not forget, you can extend this project further by adding more models, complex validation, and integrating with other parts of your application.
Haystack
Haystack is an advanced Python library designed to simplify the creation of search systems and question-answering applications. It leverages natural language processing (NLP) to allow developers to build powerful search interfaces that can understand and process user queries in a human-like manner. With Haystack, users can integrate various NLP models, connect to document stores, and implement sophisticated retrieval mechanisms. It supports multiple backends, including Elasticsearch and FAISS, enabling flexible and scalable search solutions. Additionally, Haystack offers features like end-to-end pipelines, real-time updates, and integration with external data sources, making it a versatile tool for modern information retrieval tasks.