Skip to content

Instantly share code, notes, and snippets.

View wcang's full-sized avatar
🌴
On vacation

Ang Way Chuang wcang

🌴
On vacation
View GitHub Profile
@wcang
wcang / tee.py
Created April 6, 2013 06:10
Dirty and ugly hack to duplicate stdout stream to a file. This thing sort of acts like unix's tee program. Be careful as this only overwrites several file functions
'''
Dirty and ugly hack to duplicate stdout stream to a file
Be careful as this only overwrites several file functions
This thing sort of acts like unix's tee program
Ang Way Chuang <wcang@sfc.wide.ad.jp>
Feel free to use, modify and distribute. No warranty and no restriction
'''
import sys
@wcang
wcang / cd.py
Created April 17, 2013 16:23
Sample code to try out python's context manager.
import os
class cd:
def __init__(self, path):
print "constructor"
self.dest = path
self.origin = os.getcwd()
def __enter__(self):
os.chdir(self.dest)
@wcang
wcang / chroot.py
Created April 17, 2013 17:00
Demo code to try out chroot using python context manager
import os
import sys
import shutil
class chroot:
def __init__(self, root_dir):
self.root_dir = root_dir
def __enter__(self):
self.real_root = os.open("/",os.O_RDONLY)
@wcang
wcang / mldstd.py
Created May 8, 2013 16:23
Quick tryout of state design pattern for MLD router state transition
#implementation of state design pattern for MLD router state transition
#(RFC2710)
import time
class NonQuerier:
def __init__(self, ctx):
print "In non-querier"
print "Start other querier timeout"
self.ctx = ctx
self.timeout = time.time()
@wcang
wcang / mldstd.c
Created May 8, 2013 17:28
Quick tryout of state design pattern for MLD router state transition written in C
/* Implementation of state design pattern for MLD router state transition
* (RFC 2710) using C
*/
#include <stdio.h>
#include <stdbool.h>
struct mld_state {
struct mld_ctx * ctx;
void (*init)(struct mld_state *, struct mld_ctx *);
void (*recv_query)(struct mld_state *, bool lower);
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h> /* this is needed for struct sockaddr_in (IPv4 address) */
#include <unistd.h> /* close syscall */
#include <arpa/inet.h> /* for inet_ntop */
@wcang
wcang / fork.c
Created February 19, 2014 14:23
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
static void child_process(int fd)
@wcang
wcang / deepEqual.js
Created December 20, 2014 06:49
Deep comparison in Javascript
function deepEqual(obj1, obj2)
{
if (obj1 === obj2) {
return true;
}
if (typeof(obj1) == typeof(obj2)) {
if (typeof(obj1) == 'object') {
var obj1Prop = 0;
var obj2Prop = 0;
@wcang
wcang / vector.js
Last active August 29, 2015 14:12
Javascript Object Orientation with getter function
// Your code here.
function Vector(x, y) {
this.x = x;
this.y = y;
this.plus = function(v) {
var res = new Vector(this.x, this.y);
res.x += v.x;
res.y += v.y;
return res;
@wcang
wcang / elojs_13.1.js
Created January 19, 2015 17:41
Exercise 13.1 for Eloquent Javascript
<style>
/* Defines a cleaner look for tables */
table { border-collapse: collapse; }
td, th { border: 1px solid black; padding: 3px 8px; }
th { text-align: left; }
</style>
<script>
var MOUNTAINS = [
{name: "Kilimanjaro", height: 5895, country: "Tanzania"},