2. 網(wǎng)絡(luò)請(qǐng)求與 HTTP 客戶端

示例(Requests):

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

image

3. 數(shù)據(jù)庫(kù)交互

示例(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. 數(shù)據(jù)處理與分析

示例(Pandas):

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

image

5. 機(jī)器學(xué)習(xí)與深度學(xué)習(xí)

示例(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')  # 轉(zhuǎn)換為灰度圖像
gray_img.show()

image

7. 自然語(yǔ)言處理

示例(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. 并發(fā)與并行

示例(Asyncio):

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

image

10. 測(cè)試

示例(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

每一次互動(dòng),皆是鼓勵(lì), 每一份支持,共促成長(zhǎng)。

原文轉(zhuǎn)載自:https://mp.weixin.qq.com/s/h5hZqGIgwt3IDXXY3ei0cw

上一篇:

如何結(jié)合LangChain與MCP使用的案例

下一篇:

Nacos 發(fā)布 MCP Registry,實(shí)現(xiàn)存量應(yīng)用接口“0改動(dòng)”升級(jí)到 MCP 協(xié)議
#你可能也喜歡這些API文章!

我們有何不同?

API服務(wù)商零注冊(cè)

多API并行試用

數(shù)據(jù)驅(qū)動(dòng)選型,提升決策效率

查看全部API→
??

熱門(mén)場(chǎng)景實(shí)測(cè),選對(duì)API

#AI文本生成大模型API

對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力

25個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)

#AI深度推理大模型API

對(duì)比大模型API的邏輯推理準(zhǔn)確性、分析深度、可視化建議合理性

10個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)