Skip to content

Instantly share code, notes, and snippets.

View tenfyzhong's full-sized avatar
:octocat:

tenfy tenfyzhong

:octocat:
View GitHub Profile
@tenfyzhong
tenfyzhong / trim.sh
Created January 29, 2024 08:04
Trim leading and trailing space in bash
trim() {
local var="$*"
# remove leading whitespace characters
var="${var#"${var%%[![:space:]]*}"}"
# remove trailing whitespace characters
var="${var%"${var##*[![:space:]]}"}"
printf '%s' "$var"
}
@tenfyzhong
tenfyzhong / flushall.go
Last active November 14, 2019 02:54
flushall of redis cluster
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"time"
@tenfyzhong
tenfyzhong / main.go
Last active May 10, 2019 01:57
用环境变量渲染template
package main
import (
"flag"
"fmt"
"os"
"strings"
"text/template"
)
@tenfyzhong
tenfyzhong / ssh-copy-id.exp
Last active February 1, 2019 06:02
copy ssh public key to machine
#!/usr/bin/expect -f
set timeout 5
set user [lindex $argv 0]
set host [lindex $argv 1]
set password [lindex $argv 2]
spawn ssh-copy-id $user@$host
expect {
"*yes/no" { send "yes\r";exp_continue }
@tenfyzhong
tenfyzhong / post-commit
Last active December 15, 2018 07:28
log git commit
toplevel=$(git rev-parse --show-toplevel)
log_file=$HOME/.commit_log/$(date +%Y)_week_$(date +%U).log
message=$(git log -1 --pretty=format:'%H%n%cd%n%s%n%b')
mkdir -p "$HOME/.commit_log"
touch "$log_file"
printf "%s\n$message\n--------------------------------------------------------------------------------\n" "$toplevel" >> "$log_file"
@tenfyzhong
tenfyzhong / time33.go
Created November 27, 2018 03:04
time33 hash algorithm
package time33
func Time33(s []byte) uint64 {
hash := uint64(0)
for _, b := range s {
hash += hash<<5 + uint64(b)
}
return hash
}
@tenfyzhong
tenfyzhong / frange.py
Created August 14, 2018 00:50
float range
def frange(start, stop, increment):
x = start
while x < stop:
yield x
x += increment
@tenfyzhong
tenfyzhong / dedupe.py
Created August 12, 2018 09:38
remove duplications
def dedupe(items, key=None):
seen = set()
for item in items:
val = item if key is None else key(item)
if val not in seen:
yield item
seen.add(val)
@tenfyzhong
tenfyzhong / client.py
Created December 2, 2017 11:55
example
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import time
HOST = 'localhost'
PORT = 1680
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.setsockopt(socket.SOL_TCP, socket.TCP_QUICKACK, 1)
package trylock
import "sync/atomic"
// Mutex trylock的mutex
type Mutex struct {
v int32
}
// TryLock 尝试获取锁,取不到则返回false