收集、管理和分享有用的代码片段,提高开发效率
创建数据库备份
SELECT * INTO OUTFILE '/path/to/backup.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
FROM users;
绘制柱状图
const ctx = document.getElementById('myChart').getContext('2d');
const data = {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
datasets: [{
label: 'Sales',
data: [65, 59, 80, 81, 56],
backgroundColor: 'rgba(255, 99, 132, 0.2)',
borderColor: 'rgba(255, 99, 132, 1)',
borderWidth: 1
}]
};
const config = {
type: 'bar',
data: data,
options: {
responsive: true,
scales: {
y: {
beginAtZero: true
}
}
}
};
new Chart(ctx, config);
创建缩略图
🔥
BeautifulSoup解析
import requests
from bs4 import BeautifulSoup
response = requests.get('https://example.com')
soup = BeautifulSoup(response.text, 'html.parser')
title = soup.title.text
print(f'Title: {title}')
for link in soup.find_all('a'):
print(link.get('href', None))
发送SMTP邮件
import javax.mail.*;
import javax.mail.inet.*;
public class EmailSender {
public static void sendEmail(String to, String from, String subject, String body) throws MessagingException {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.example.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthenticator getPasswordAuthenticator() {
return new PasswordAuthenticator("username", "password");
}
});
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipient(RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setText(body);
Transport.send(message);
}
}
使用OpenMP并行排序
#include <omp.h>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> data(10000000);
// Fill data with random values
for (auto& n : data) n = rand() % 1000;
#prag omp parallel
#prag omp for
sort(data.begin(), data.end());
cout << "Sorted " << data.size() << " elements\n";
return 0;
}
高效管理您的代码片段,提高开发效率
通过关键字、语言或分类快速查找代码片段,支持模糊搜索和过滤功能
支持多种编程语言的语法高亮,使代码更加清晰易读
随时随地访问您的代码片段库,支持桌面和移动设备
浏览最受欢迎的代码分类