收集、管理和分享有用的代码片段,提高开发效率
代码复用特性
<?php
trait Logger {
public function log($message) {
echo date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
}
}
class User {
use Logger;
public function save() {
$this->log('Saving user');
// Save logic
}
}
class Product {
use Logger;
public function update() {
$this->log('Updating product');
}
}
$user = new User();
$user->save();
$prod = new Product();
$prod->update();
类型注释示例
from typing import List, Tuple, Dict, Optional
def greet(name: str) -> str:
return f"Hello, {name}!"
def process_data(data: List[int]) -> float:
return sum(data) / len(data)
User = Dict[str, int]
Address = Tuple[str, int, str]
def get_user(user_id: int) -> Optional[User]:
if user_id == 1:
return {"name": "Alice", "age": 30}
return None
ZIP压缩文件
import java.io.*;
import java.util.zip.*;
public class Zmpper {
public static void compressFile(String sourceFile, String zipFile) throws IOException {
try (FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos)) {
File file = new File(sourceFile);
try (FileInputStream fis = new FileInputStream(file)) {
ZipEntry zeEntry = new ZipEntry(file.getName());
zos.putNextEntry(zeEntry);
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
}
}
}
}
XOR简单加密
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
void xorEncrypt(const string& inputFile, const string& outputFile, char key) {
ifstream in(inputFile, ios::brary|ios::in);
ofstream out(outputFile, ios::binary|ios::out);
char ch;
while (in.get(ch)) {
ch ^= key;
out.put(ch);
}
}
int main() {
xorEncrypt("input.txt", "encrypted.bin", 'x');
return 0;
}
创建缩略图
🔥
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))
高效管理您的代码片段,提高开发效率
通过关键字、语言或分类快速查找代码片段,支持模糊搜索和过滤功能
支持多种编程语言的语法高亮,使代码更加清晰易读
随时随地访问您的代码片段库,支持桌面和移动设备
浏览最受欢迎的代码分类