Skip to content

Instantly share code, notes, and snippets.

View alextanhongpin's full-sized avatar

Alex Tan Hong Pin alextanhongpin

View GitHub Profile
@alextanhongpin
alextanhongpin / mysql-parser.go
Created April 13, 2020 18:17
Sample usage of mysql parser - could be useful to create ERD diagrams from database schema
package main
import (
"fmt"
"log"
"github.com/pingcap/parser"
"github.com/pingcap/parser/ast"
_ "github.com/pingcap/tidb/types/parser_driver"
)
@alextanhongpin
alextanhongpin / golang-pipe-to-graphviz.go
Last active April 13, 2020 18:17
Sample golang code that pipes the dynamic .dot file data to external graphviz process to write the .png file
// -- IMPORTSS NOT SHOWN
path, _ := exec.LookPath("dot")
cmd := exec.Command(path, fmt.Sprintf("-T%s", "png"))
stdin, err := cmd.StdinPipe()
if err != nil {
log.Fatal(err)
}
go func() {
defer stdin.Close()
// Writes the data to stdin.
@alextanhongpin
alextanhongpin / nginxoptimization.config
Created February 25, 2018 12:17 — forked from wearhere/nginxoptimization.config
Optimized nginx configuration for an AWS Elastic Beanstalk environment using an Application Load Balancer.
files:
"/opt/elasticbeanstalk/#etc#nginx#optimized-nginx.conf":
mode: "000644"
owner: root
group: root
encoding: plain
content: |
# Elastic Beanstalk Managed
# Elastic Beanstalk managed configuration file
@alextanhongpin
alextanhongpin / package.json
Created January 11, 2018 08:08
How to "host" your npm modules on Github and refer it.
{
"name": "test-github",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "alextanhongpin <alextan@seekasia.com>",
"description": "",
@alextanhongpin
alextanhongpin / Dockerrun.aws.json
Created January 2, 2018 09:43
Deploying Metabase to AWS Elasticbeanstalk
{
"AWSEBDockerrunVersion": "1",
"Ports": [
{
"ContainerPort": "3000"
}
],
"Image": {
"Name": "metabase/metabase:v0.27.2",
"Update": "true"
@alextanhongpin
alextanhongpin / instructions.md
Last active June 13, 2023 16:48
Setting up locale on alpine:3.6 docker image

Locale

When using the alpine docker image, the command locale -a or locale-gen does not exist. You might need them to install different locales in your docker image to allow your applications to support multilingual. Most common issue is when dealing with database inputs (MySQL), whereby the text will be displayed as garbage characters if you do not have the right locale installed.

FROM alpine:3.6

# ---not shown here---
@alextanhongpin
alextanhongpin / gradient_descent.py
Last active December 26, 2017 07:40
Gradient descent in python
import numpy as np
X = [1, 2, 4, 3, 5]
y = [1, 3, 3, 2, 5]
alpha = 0.01
B_0 = 0
B_1 = 0
EPOCHS = 10
def predict(X_i, B_0, B_1):
@alextanhongpin
alextanhongpin / index.js
Created November 25, 2017 04:16
Remove trailing zeros, and increment value
// Map location to a range, based on the number of trailing zeroes.
// e.g. 50000 will become { from: 50000, to: 60000 }
// 50100 will become { from: 50100, to: 50200 }
// 10600 will become { from: 10600, to: 10700 }
export function mapLocation (code) {
const reverseString = s => s.split('').reverse().join('')
const str = code.toString()
const startLen = str.length
const trimmedZeros = reverseString(parseInt(reverseString(str), 10).toString())
@alextanhongpin
alextanhongpin / decrypt.js
Created November 24, 2017 08:54
The question is as follow, for each word in the line, print the character based on the index if the length is greater than the index [4:52] so for the first line, `[h]ey g[o]od la[w]yer` (edited) [4:53] hey is the first word, so take the first index, which is `h` good is the second word, so take the second index, which is `o`... [4:53] if the se…
function decrypt (str) {
let counter = 0
return str.split(' ').reduce((a, b) => {
if (b.length >= counter + 1) {
const part = b[counter]
counter += 1
return a + part
}
return a
@alextanhongpin
alextanhongpin / combination.js
Created November 15, 2017 06:55
Create different combinations from array
function combination (x) {
return Array(Math.pow(2, x.length)).fill(0).map((_, i) => {
return Array(x.length).fill(0).map((_, j) => {
if ((i & Math.pow(2, j))) {
return x[j]
}
return null
}).filter((nonNull => nonNull !== null))
})
}