2. 網絡請求與 HTTP 客戶端

示例(Requests):

import requests
response = requests.get('https://api.github.com')
print(response.status_code)
print(response.json())

image

3. 數據庫交互

示例(SQLAlchemy):

from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
Base = declarative_base()
class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    name = Column(String)
engine = create_engine('sqlite:///:memory:')
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
new_user = User(name='Alice')
session.add(new_user)
session.commit()
users = session.query(User).all()
for user in users:
    print(user.name)

image

4. 數據處理與分析

示例(Pandas):

import pandas as pd
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': ['a', 'b', 'c']
})
print(df)

image

5. 機器學習與深度學習

示例(Scikit-learn):

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2)
clf = KNeighborsClassifier()
clf.fit(X_train, y_train)
print(clf.score(X_test, y_test))

image

6. 圖像處理

示例(Pillow):

from PIL import Image
img = Image.open('example.jpg')
gray_img = img.convert('L')  # 轉換為灰度圖像
gray_img.show()

image

7. 自然語言處理

示例(spaCy):

import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("Apple is looking at buying U.K. startup for $1 billion")
for ent in doc.ents:
    print(ent.text, ent.label_)

image

8. 命令行接口

示例(Click):

import click
@click.command()
@click.option('--count', default=1, help='Number of greetings.')
@click.argument('name')
def hello(count, name):
    for _ in range(count):
        click.echo(f"Hello {name}!")
if __name__ == '__main__':
    hello()

image

9. 并發與并行

示例(Asyncio):

import asyncio
async def main():
    print('Hello')
    await asyncio.sleep(1)
    print('World')
asyncio.run(main())

image

10. 測試

示例(pytest):

# test_example.py
def add(a, b):
    return a + b
def test_add():
    assert add(1, 2) == 3

image

11. 日志記錄

示例(logging):

import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.info('This is an info message')

image

image

image

每一次互動,皆是鼓勵, 每一份支持,共促成長。

原文轉載自:https://mp.weixin.qq.com/s/h5hZqGIgwt3IDXXY3ei0cw

上一篇:

如何結合LangChain與MCP使用的案例

下一篇:

Nacos 發布 MCP Registry,實現存量應用接口“0改動”升級到 MCP 協議
#你可能也喜歡這些API文章!

我們有何不同?

API服務商零注冊

多API并行試用

數據驅動選型,提升決策效率

查看全部API→
??

熱門場景實測,選對API

#AI文本生成大模型API

對比大模型API的內容創意新穎性、情感共鳴力、商業轉化潛力

25個渠道
一鍵對比試用API 限時免費

#AI深度推理大模型API

對比大模型API的邏輯推理準確性、分析深度、可視化建議合理性

10個渠道
一鍵對比試用API 限時免費