Skip to content

Instantly share code, notes, and snippets.

View emmettna's full-sized avatar

Emmett Na emmettna

  • Ottawa
View GitHub Profile
public static int gcd(int p, int q)
{
if (q == 0) return p;
int r = p % q;
return gcd(q, r);
}
public static int rank(int key, int[] a){
// Array must be sorted.
int lo = 0;
int hi = a.length - 1;
while (lo <= hi){
// Key is in a[lo..hi] or not present.
int mid = lo + (hi - lo) / 2;
if (key < a[mid]) hi = mid - 1;
else if (key > a[mid]) lo = mid + 1;
else return mid;
@emmettna
emmettna / gist:3812b535efde6d4a39802977e5c83a1f
Created March 28, 2017 00:41
Making Swing transparent background
frame.setUndecorated(true);
frame.getContentPane().setBackground(new Color(1.0f,1.0f,1.0f,0.0f));
frame.setBackground(new Color(1.0f,1.0f,1.0f,0.0f));
//additionally panel needs to be set opaque (panel.setOpaque(false))
@emmettna
emmettna / JsonDecoder.py
Created October 2, 2017 19:41
Django Python3 UTF-8 Json decoder method
from django.http import JsonResponse
import json
def get_json_data(data):
try:
return json.loads(data.body.decode('utf-8'))
except TypeError:
return JsonResponse({'response': 'Type Incorrect'})
except Exception as e:
return JsonResponse({'response': 'Unexpected Error as follow : '+str(e)})
@emmettna
emmettna / AsciiFalseJsonResponse.py
Created October 2, 2017 19:47
Django Python3 AsciiFalseJsonResponse Decorator and fuction
from functools import wraps
from django.http import HttpResponse
import json
"""
Either use it as a decorator or as a function
But When using as a decorator, Make sure you return dictionary object.
Both HttpResponse and JsonResponse will cause an Error.
"""
class AsciiFalseJsonResponse:
@emmettna
emmettna / dbconn.py
Created October 2, 2017 20:06
Python3 Database Dictionary Cursor
import pymysql
class DBConn:
conn_obj = None
def __init__(self):
try:
db = pymysql.connect(host="",
user="",
passwd="",
@emmettna
emmettna / Docker-compose Nginx + Django + Gunicorn
Last active June 27, 2021 20:50
Docker-compose Nginx + Django + Gunicorn
1. Make folder tree like these
mi_project
├── src
│ ├── midjangoapp
│ ├── manage.py
├── config
│ ├── requirements.pip
│ ├── nginx
│ ├── midjangoapp.conf
├── Dockerfile
@emmettna
emmettna / multiple_apps_on_docker_compose_with_haproxy
Created February 26, 2018 15:25
Two different web app on docker-compose using haproxy
Hierarchy
src -firstApp(django)
- Dockerfile
- anyother necessaries
-secondApp(hello-world)
- Dockerfile
- anyother necessaries
-haprxy
- Dockerfile
- haproxy.cfg
function getColorSets(){
const numberRange = [...Array( 14).keys()].map( x => x * 20) // [0,20..260]
const addZero = function( val){ return ( val.length < 2) ? "0" + val : val; }
const defaultColors = ["f44336","E91E63","9C27B0","673AB7","3F51B5","2196F3","03A9F4","00BCD4","009688",
"4CAF50","8BC34A","CDDC39","FFEB3B","FFC107","FF9800","FF5722","795548","9E9E9E","607D8B"]
const flattenList = function( unFlattenedList){ [].concat.apply([], unFlattenedList) }
const generateSet = function( colors, n) {
return colors.map( x => [[x.slice(0,2)],[x.slice(2,4)],[x.slice(4,6)]]) // slice RGB
.map(x => x.map( d => addZero( ( ( parseInt( d, 16) + n) % 255).toString( 16))))
.map( x => x.join( ""))
@emmettna
emmettna / 2019-12-20-load-config-example.py
Created January 17, 2020 08:52
blog python sample code
def load_config(host, id, pass):
...