Skip to content

Instantly share code, notes, and snippets.

@Martin91
Martin91 / jsonbench.go
Created November 25, 2022 07:59
Benchmark mysql with simple read, update and insert json by JSON type and text type
package jsonbench
import (
"database/sql"
"fmt"
"math/rand"
)
var (
jsonPattern = "{\"value\": \"%s\"}"
@Martin91
Martin91 / containsAny.go
Last active May 20, 2021 01:54
containsAny in Golang
package main
import (
"fmt"
"reflect"
)
func containsAny(data interface{}, element ...interface{}) bool {
rVal := reflect.ValueOf(data)
switch rVal.Kind() {
@Martin91
Martin91 / sshtunnel.go
Last active April 18, 2023 06:49
Connect to a database via SSH tunnel
// Package internal ...
package internal
import (
"fmt"
"github.com/go-sql-driver/mysql"
"golang.org/x/crypto/ssh"
"net"
)
@Martin91
Martin91 / .git_scripts_check_branchs.sh
Created December 22, 2020 03:02
git scan released branches
#!/bin/zsh
specialBranches=("master" "uat" "test" "release")
echo "\u001b[31mScanning released local branches...\n--------------------------------------------------------\u001b[0m"
releasedLocalBranches=()
for branch in $(git branch)
do
git show-ref -s --heads $branch | xargs git branch --contains | grep master > /dev/null
if [[ $? -eq 0 && ! " ${specialBranches[@]} " =~ " ${branch} " ]]; then
@Martin91
Martin91 / mvcc.py
Last active February 24, 2023 21:07 — forked from elliotchance/transaction_example.py
https://elliot.land/implementing-your-own-transactions-with-mvcc, fixed a few runtime errors in the original gist and simulate a case that updating a deleting record
next_xid = 1
active_xids = set()
records = []
class TransactionConflictError(RuntimeError):
pass
def new_transaction():
@Martin91
Martin91 / postman-pre-request-script-for-shopee-openapi.js
Last active April 16, 2021 17:06
Postman Pre-request script for Shopee OpenAPI's authentication
var CryptoJS = require("crypto-js");
var Property = require('postman-collection').Property;
var now = new Date();
var timestampNow = parseInt(now.getTime() / 1000);
pm.collectionVariables.set("timestamp", timestampNow);
var host = pm.collectionVariables.get("host");
var path = pm.request.url.getPath();
var requestURL = host + path;
@Martin91
Martin91 / output.txt
Created June 12, 2019 07:07
supervisord XML/RPC API demo
==================== Supported Methods ======================
['supervisor.addProcessGroup', 'supervisor.clearAllProcessLogs', 'supervisor.clearLog', 'supervisor.clearProcessLog', 'supervisor.clearProcessLogs', 'supervisor.getAPIVersion', 'supervisor.getAllConfigInfo', 'supervisor.getAllProcessInfo', 'supervisor.getIdentification', 'supervisor.getPID', 'supervisor.getProcessInfo', 'supervisor.getState', 'supervisor.getSupervisorVersion', 'supervisor.getVersion', 'supervisor.readLog', 'supervisor.readMainLog', 'supervisor.readProcessLog', 'supervisor.readProcessStderrLog', 'supervisor.readProcessStdoutLog', 'supervisor.reloadConfig', 'supervisor.removeProcessGroup', 'supervisor.restart', 'supervisor.sendProcessStdin', 'supervisor.sendRemoteCommEvent', 'supervisor.shutdown', 'supervisor.signalAllProcesses', 'supervisor.signalProcess', 'supervisor.signalProcessGroup', 'supervisor.startAllProcesses', 'supervisor.startProcess', 'supervisor.startProcessGroup', 'supervisor.stopAllProcesses', 'supervisor.stopProcess'
@Martin91
Martin91 / groupby.py
Last active September 28, 2018 03:19
Python itertools.groupby trap
data = [{'id': 1, 'value': 1}, {'id': 2, 'value': 2}, {'id': 2, 'value': 3}, {'id': 1, 'value': 4}]
for id, ele in groupby(data, key=lambda e: e['id']):
print "id: %d" % id
print "values: %s" % list(ele)
# => id: 1
# => values: [{'id': 1, 'value': 1}]
# => id: 2
# => values: [{'id': 2, 'value': 2}, {'id': 2, 'value': 3}]
# => id: 1
@Martin91
Martin91 / django_aggregation.py
Last active August 10, 2018 07:36
django work with aggregation functions
from django.db.models import Count
Model.values('pickup_plan_id').annotate(picked_count=Count('pickup_plan_id')).filter(picked_count__lt=2)
# SELECT
# `model`.`pickup_plan_id`,
# COUNT(`model`.`pickup_plan_id`) AS `picked_count`
# FROM
# `model`
# WHERE
@Martin91
Martin91 / iptables_allow_only_localhost.sh
Created July 22, 2018 07:46
iptables to allow only localhost to access specified port
# Allow access only localhost by port 1991
sudo iptables -A INPUT -i lo -p tcp --dport 1991 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 1991 -j DROP