登录

常用代码下载

精选高频实用代码片段,覆盖 Python、JavaScript、Shell、SQL 等主流语言,提升开发效率,一键复制即用。

读取 JSON 文件 Python

安全加载本地 JSON 配置文件
import json

def load_config(path):
    with open(path, 'r', encoding='utf-8') as f:
        return json.load(f)

config = load_config('config.json')

防抖函数 JavaScript

用于搜索框、窗口 resize 等高频事件
function debounce(func, delay) {
  let timer;
  return function (...args) {
    clearTimeout(timer);
    timer = setTimeout(() => func.apply(this, args), delay);
  };
}

// 使用示例
const handleInput = debounce(search, 300);

批量重命名文件 Shell

将当前目录下 .jpg 文件加前缀
#!/bin/bash
for file in *.jpg; do
  mv "" "prefix_$file"
done

分页查询 SQL

MySQL 分页标准写法(第2页,每页10条)
SELECT * FROM users
ORDER BY id
LIMIT 10 OFFSET 10;

响应式卡片布局 CSS

使用 Flex 实现自适应卡片
.card-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}
.card {
  flex: 1 1 300px;
  background: #2a2a4a;
  padding: 16px;
  border-radius: 12px;
}

发送 HTTP 请求 Python

使用 requests 库调用 API
import requests

response = requests.post(
    'https://api.example.com/data',
    json={'key': 'value'},
    headers={'Authorization': 'Bearer token'}
)
print(response.json())