Skip to content

Instantly share code, notes, and snippets.

@leongkui
leongkui / convert2utf8.py
Last active May 28, 2025 03:28
Fix/convert GBK/GB2312 format to UTF8
import codecs
import sys
# https://stackoverflow.com/questions/53954604/python-encoding-chinese-to-special-character
infile = codecs.open(sys.argv[1], "r", "gb2312")
lines = infile.readline()
infile.close()
print(lines)
@leongkui
leongkui / restoreconf.sh
Last active January 1, 2022 02:46
Ubuntu restore missing configuration from package
# https://askubuntu.com/questions/66533/how-can-i-restore-configuration-files
dpkg -S /etc/cassandra
21432 sudo apt install --reinstall -o Dpkg::Options::="--force-confask,confnew,confmiss" $(dpkg -S /etc/cassandra | sed 's/,//g; s/:.*//')
21434 sudo apt install --reinstall -o Dpkg::Options::="--force-confask,confnew,confmiss" cassandra
@leongkui
leongkui / recaptcha.py
Created December 19, 2021 03:33
Selenium Python - reCaptcha
# from https://stackoverflow.com/questions/53917157/find-the-recaptcha-element-and-click-on-it-python-selenium/53917309#53917309
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
@leongkui
leongkui / stop_ds_store.sh
Created December 11, 2021 02:32
MacOS Stop Writing .DS_Store on network drive
defaults write com.apple.desktopservices DSDontWriteNetworkStores false
@leongkui
leongkui / git.sh
Last active December 7, 2021 08:46
Git 999
## Recover files from previous git commit
git checkout <revision-hash> -- path/to/file/or/directory
@leongkui
leongkui / github-action-build.yaml
Created December 7, 2021 06:15
Next.js CI/CD build cache
uses: actions/cache@v2
with:
path: ${{ github.workspace }}/.next/cache
# Generate a new cache whenever packages or source files change.
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }}
# If source files changed but packages didn't, rebuild from a prior cache.
restore-keys: |
${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-
@leongkui
leongkui / resintall-apt-config.sh
Created December 3, 2021 03:51
Ubuntu apt reinstall config file
dpkg -S /etc/<files>
apt install --reinstall -o Dpkg::Options::="--force-confask,confnew,confmiss" <package>
@leongkui
leongkui / generator.go
Last active December 2, 2021 09:48
[Golang implementation of Python Range Generator] #golang #generator
package main
import "fmt"
/*
Adopted from https://serge-hulne.medium.com/iterators-map-filter-reduce-and-list-processing-in-go-golang-implementing-python-functional-2d24d780051f
*/
type ReduceCallback func(chan int) int
@leongkui
leongkui / react-hook-state-update.js
Created September 28, 2021 02:59
[react hook state update] #react #reacthook
// Through Input
const [state, setState] = useState({ fName: "", lName: "" });
const handleChange = e => {
const { name, value } = e.target;
setState(prevState => ({
...prevState,
[name]: value
}));
};
@leongkui
leongkui / go-cyclo
Created September 10, 2021 00:01
[Golang Cyclomatic Complexities] #golang
go install github.com/fzipp/gocyclo/cmd/gocyclo@latest
gocyclo -ignore "_test.go" .