Skip to content

Instantly share code, notes, and snippets.

@winston-wen
winston-wen / argparse.go
Created August 24, 2023 09:27
Parse cmdargs in Golang.
import (
"github.com/akamensky/argparse"
)
func main() {
parser := argparse.NewParser("my2go", "Generate GORM models from MySQL database")
// register arguments
host := parser.String("n", "host", &argparse.Options{Required: false, Help: "MySQL host", Default: "localhost"})
port := parser.Int("p", "port", &argparse.Options{Required: false, Help: "MySQL port", Default: 3306})
@winston-wen
winston-wen / logging.go
Last active August 22, 2023 08:19
Typical logging in Golang.
package main
import (
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
lbj "gopkg.in/natefinch/lumberjack.v2"
)
@winston-wen
winston-wen / go.json
Last active August 16, 2023 03:10
vscode_go_conf
{
"fmt.Println": {
"prefix": ["pl", "cl", "sout"],
"body": [
"fmt.Println($1);",
],
}
}
@winston-wen
winston-wen / panic_in_thread.rs
Last active August 2, 2023 06:26
panic in child thread won't affect parent thread.
use std::thread;
use std::time::Duration;
fn main() {
let _ = thread::spawn(|| {
let errmsg = format!("Child thread id {:?} will soon panic.", thread::current().id());
thread::sleep(std::time::Duration::from_secs(3));
panic!("{}", errmsg);
});
@winston-wen
winston-wen / json_traverse.sql
Created August 2, 2023 02:48
Traverse a JSON object in MySQL 8.
-- MySQL 8
drop procedure if exists foofoo;
create procedure foofoo(
params JSON
)
begin
-- parameters at root level
declare layers JSON; declare total_min, n_layers int unsigned;
-- parameters per layer
@winston-wen
winston-wen / init.gradle.kts
Created July 30, 2023 16:04 — forked from wuseal/init.gradle.kts
国内全局加速Gradle依赖下载速度配置,把这个文件放到~/.gradle目录下既可
/**
* Created by Seal.Wu on 2020/7/11
* Description: Set up mirrors for Gradle Globally
*/
val MAVEN_REPOSITORY_URL = "https://maven.aliyun.com/repository/central"
val JCENTER_REPOSITORY_URL = "https://maven.aliyun.com/repository/jcenter"
val GOOGLE_REPOSITORY_URL = "https://maven.aliyun.com/repository/google"
val GRADLE_PLUGIN_REPOSITORY_URL = "https://maven.aliyun.com/repository/gradle-plugin"
gradle.settingsEvaluated {
@winston-wen
winston-wen / 1. 在图上绘制方框.py
Last active August 19, 2021 06:40
matplotlib用法
import matplotlib.pyplot as plt
import matplotlib.patches as pch
rec = pch.Rectangle((x1, y1), w, h, linewidth=1, edgecolor='r', facecolor='none')
plt.gca().add_patch(rec)
plt.show()
@winston-wen
winston-wen / 1. static_local.py
Last active August 15, 2021 09:05
python模拟C语言的静态局部变量
def fibonacci():
# 如果函数名太长, 为了编码的优雅方便, 建议起个别名.
ctx = fibonacci
# 在if not hasattr代码块里声明并初始化静态局部变量.
# 如果需要用到多个静态局部变量, 只需要检查一个用不到的变量(如ctx.init)是否存在.
# 完成初始化所需的静态局部变量之后, 记得初始化ctx.init. 如此这块代码才不会执行第二次.
if not hasattr(ctx, "init"):
ctx.a0, ctx.a1 = 1, 1
# ctx.init的赋值是随意的. 我的习惯是赋一个有意义的值.
ctx.init = "<<<Initialized>>>"
package main
import (
"net/smtp"
"log"
"os/exec"
"fmt"
)
func main() {
@winston-wen
winston-wen / sftp-logger.py
Created June 11, 2020 10:49
Send results to a file server so that one can easily view the results, where the results are generated by programs running on Linux servers.
import os
import paramiko
class SftpLogger(object):
def __init__(self, host, port, username, password, basedir='/home/dropbox/'):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(
hostname=host,