This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fabric import Connection | |
def deploy(host_string, user, password, project_path, remote_dir): | |
c = Connection(host=host_string, user=user, connect_kwargs={"password": password}) | |
with c.cd(remote_dir): | |
c.run('git pull origin master') # 更新代码 | |
c.run('pip install -r requirements.txt') # 安装依赖 | |
c.run('python manage.py migrate') # 执行数据库迁移 | |
c.run('python manage.py collectstatic --noinput') # 静态文件收集 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from locust import HttpUser, task, between | |
class WebsiteUser(HttpUser): | |
wait_time = between(5, 15) # 定义用户操作之间的等待时间 | |
@task | |
def load_test_api(self): | |
response = self.client.get("/api/data") | |
assert response.status_code == 200 # 验证返回状态码为200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import smtplib | |
from email.mime.text import MIMEText | |
from email.mime.multipart import MIMEMultipart | |
def generate_report(source, to_addr, subject): | |
# 假设这里是从数据库或文件中获取数据并生成报告内容 | |
report_content = pd.DataFrame({"Data": [1, 2, 3], "Info": ["A", "B", "C"]}).to_html() | |
msg = MIMEMultipart() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import imaplib | |
import email | |
from email.header import decode_header | |
import os | |
def download_attachments(email_addr, password, imap_server, folder='INBOX'): | |
mail = imaplib.IMAP4_SSL(imap_server) | |
mail.login(email_addr, password) | |
mail.select(folder) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
import pandas as pd | |
def visualize_data(data_file): | |
df = pd.read_csv(data_file) | |
# 示例:绘制柱状图 | |
df.plot(kind='bar', x='category', y='value') | |
plt.title('数据分布') | |
plt.xlabel('类别') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def analyze_log(log_file): | |
with open(log_file, 'r') as f: | |
lines = f.readlines() | |
error_count = 0 | |
for line in lines: | |
if "ERROR" in line: | |
error_count += 1 | |
print(f"日志文件中包含 {error_count} 条错误记录。") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fileinput | |
def search_replace_in_files(dir_path, search_text, replace_text): | |
for line in fileinput.input([f"{dir_path}/*"], inplace=True): | |
print(line.replace(search_text, replace_text), end='') | |
# 使用示例: | |
search_replace_in_files('/path/to/files', 'old_text', 'new_text') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image | |
import os | |
def compress_images(dir_path, quality=90): | |
for filename in os.listdir(dir_path): | |
if filename.endswith(".jpg") or filename.endswith(".png"): | |
img = Image.open(os.path.join(dir_path, filename)) | |
img.save(os.path.join(dir_path, f'compressed_{filename}'), optimize=True, quality=quality) | |
# 使用示例: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
def clean_data(file_path): | |
df = pd.read_csv(file_path) | |
# 示例:处理缺失值 | |
df.fillna('N/A', inplace=True) | |
# 示例:去除重复行 | |
df.drop_duplicates(inplace=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from bs4 import BeautifulSoup | |
def fetch_web_content(url): | |
response = requests.get(url) | |
if response.status_code == 200: | |
soup = BeautifulSoup(response.text, 'html.parser') | |
# 示例提取页面标题 | |
title = soup.find('title').text | |
return title |
NewerOlder