Skip to content

Instantly share code, notes, and snippets.

View tsonglew's full-sized avatar
:octocat:
I may be slow to respond.

Tsonglew tsonglew

:octocat:
I may be slow to respond.
View GitHub Profile
@tsonglew
tsonglew / httpServerShutdown.go
Created September 26, 2017 12:54
http server
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
)
@tsonglew
tsonglew / readFileContent.go
Created September 27, 2017 12:52
read from file
func Content(filename string) (string, error) {
f, err = os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
var result []byte
buf := make([]byte, 100)
for {
@tsonglew
tsonglew / leakyBuffer.go
Created September 29, 2017 12:43
A leaky buffer
package main
var freeList = make(chan *Buffer, 100)
var serverChan = make(chan *Buffer)
func client() {
for {
var b *Buffer
select {
case b = <-freeList:
package main
import "fmt"
func gen(nums ...int) chan int {
out := make(chan int)
go func() {
for _, n := range nums {
out <- n
}
@tsonglew
tsonglew / tableExists.cs
Created June 11, 2018 02:58
C# check if table exists in sqlite
private bool tableExists(string tableName, SQLiteConnection db)
{
bool exists;
db.Open();
try
{
var cmd = new SQLiteCommand(
"select case when exists((select * from information_schema.tables where table_name = '" + tableName + "')) then 1 else 0 end", db);
@tsonglew
tsonglew / singleton.py
Last active July 31, 2018 03:20
python3 singleton
# Method 1. with __new__ magic method
class Singleton:
def __new__(cls):
if not hasattr(cls, 'instance'):
cls.instance = super().__new__(cls)
return cls.instance
class TestSingletonNew(Singleton):
pass
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.UI;
public class testScript : MonoBehaviour {
public RawImage image;
private VideoPlayer videoPlayer;
private VideoSource videoSource;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ButtonCreator : MonoBehaviour {
public GameObject buttonPrefab;
public GameObject panelToAttachButtonsTo;
// Use this for initialization
void Start () {
@tsonglew
tsonglew / depends.py
Created July 30, 2018 02:21
remove pipenv packages dependencies `pipenv uninstall `pipenv graph --json |python3 depends.py requests``
import sys
import json
package = sys.argv[1]
other_dependencies = set()
removing_dependencies = set([package])
for i in json.load(sys.stdin):
for p in i['dependencies']:
key = p['key']
@tsonglew
tsonglew / max_and_min.py
Last active August 28, 2018 13:56
find max and min index of an array with 3/2N comparisons
def max_min(arr):
length = len(arr)
if length <= 1:
return 0
even_length = length
last = None
if length % 2 == 1:
last = arr[length-1]
even_length -= 1
i = 0