Skip to content

Instantly share code, notes, and snippets.

@physacco
physacco / zwc.py
Last active December 14, 2015 05:29
Chinese Word Counter
#!/usr/bin/env python
# encoding: utf-8
# Name: Chinese Word Counter
# Description: This is a word counter for Chinese. It reads text from stdin,
# filters white spaces (including the full-width Chinese blank-space character),
# and then counts the remaining characters. In my tests, it usually produces
# the same result as WPS. It may be useful for some writers.
# Example: zwc.py < foo.txt
# Author: physacco
@physacco
physacco / hash2array.lua
Created February 28, 2013 03:10
This gist presents 2 Lua functions for converting hash to array and vice versa. This is a common task in Redis Lua scripting.
-- $ lua hash2array.lua
-- print a table that contains numbers and strings only
local f_log_hash = function(hash, name)
if name ~= nil then
print(name .. ':')
end
for k, v in pairs(hash) do
print(k, ' => ', v)
end
@physacco
physacco / goagent
Last active December 14, 2015 09:08
GoAgent init script for Redhat/Fedora.
#!/bin/sh
# goagent: GoAgent init script for Redhat/Fedora.
# Written by physacco. 2013/03/01
# To start at boot time:
# 1. copy this file to /etc/init.d/goagent
# 2. chkconfig goagent on
# ---------------------
@physacco
physacco / http-hello.go
Last active December 14, 2015 16:18
A simple HTTP server in Go. From A Tour of Go.
package main
import (
"fmt"
"runtime"
"net/http"
)
type Hello struct{}
@physacco
physacco / tcp-echo-server-with-bufio.go
Created March 8, 2013 03:00
A TCP echo server demo.
package main
import (
"os"
"fmt"
"net"
"bufio"
)
func handleConnection(conn net.Conn) {
@physacco
physacco / tcp-echo-client-with-bufio.go
Created March 8, 2013 03:01
A TCP echo client demo.
package main
import (
"os"
"fmt"
"net"
"time"
"bufio"
)
@physacco
physacco / install-vimfiles-for-go.sh
Created March 8, 2013 08:39
Install vim plugin for the go programming language. Tested on Fedora.
#!/bin/sh
GOROOT="/home/i/src/go"
VIMFILES="/usr/share/vim/vimfiles"
mkdir -p $VIMFILES/autoload/go
cp -i $GOROOT/misc/vim/ftdetect/gofiletype.vim $VIMFILES/ftdetect/
cp -i $GOROOT/misc/vim/syntax/go.vim $VIMFILES/syntax/
cp -i $GOROOT/misc/vim/syntax/godoc.vim $VIMFILES/syntax/
cp -i $GOROOT/misc/vim/autoload/go/complete.vim $VIMFILES/autoload/go/
@physacco
physacco / config.ru
Created March 9, 2013 09:58
Send, receive and process binary data in web browser. Using XMLHTTPRequest, ArrayBuffer and ArrayBufferViews. Tested on Firefox 20.0.
# encoding: utf-8
# Tested on ruby 1.9.2
class HTTPNotFound < Exception
end
class MimeTypes
HTML = "text/html; charset=utf-8"
JAVASCRIPT = "application/javascript: charset=utf-8"
BINARY = "application/octet-stream; charset=binary"
@physacco
physacco / test_flag.go
Created March 10, 2013 05:04
Test the usage of the flag package in go's stdlib.
package main
import (
"os"
"fmt"
"flag"
)
func main() {
help := flag.Bool("help", false, "Print help message")
@physacco
physacco / test_file_io.go
Last active December 14, 2015 18:09
Test basic file operations in go. Copy a file using the package os & io.
package main
import (
"io"
"os"
)
func main() {
// Open a file for reading
// The associated file descriptor has mode O_RDONLY.