Tuesday, December 31, 2013

What is RabbitMQ and Celery?


key points from Wiki

  • Open source message broker software
  • AMQP (Advanced Message Queuing Protocol)
  • Written in the Erlang Programming Language
  • Built on the Open Telecom Platform framework
words from their official website
Celery is an asynchronous task queue/job queue based on distributed message passing. It is focused on real-time operation, but supports scheduling as well.
The execution units, called tasks, are executed concurrently on a single or more worker servers using multiprocessing, Eventlet, or gavent. Tasks can execute asynchronously (in the background) or synchronously (wait until ready).

Wednesday, November 20, 2013

Install nox-classic on Ubuntu 12.04

It's seems that there are problems if just following the instruction in their wiki page. Try the instructions below if you have problems while installing nox.

Update apt-get download list

  • cd /etc/apt/sources.list.d/
  • sudo wget http://openflowswitch.org/downloads/debian/nox.list
  • sudo apt-get update
  • sudo apt-get install nox-dependencies

Download from Github

  • cd ~
  • apt-get install nox-dependencies libtbb-dev libboost-serialization-dev libboost-all-dev
  • git clone git://github.com/noxrepo/nox
  • cd nox
  • ./boot.sh
  • mkdir build
  • cd build


  • ../configure
  • make
  • sudo make install


  • cd src
  • ./nox_core -i ptcp:6633
  • ./nox_core -v -i ptcp:6633 switch

Monday, November 18, 2013

Setup eCos Development Environment on Ubuntu 12.04 for STM32

Before You Begin

Install Required Tools & Dependencies

  • sudo apt-get install cvs libstdc++5 ia32-libs libglib2.0-dev

Download eCos then Setup

  • wget --passive-ftp ftp://ecos.sourceware.org/pub/ecos/ecos-install.tcl
  • sh ecos-install.tcl
    • choose a faster distribution site, like "[16] ftp://ftp.u-aizu.ac.jp/pub/gnu/cygnus/ecos", which may locate in Japan
    • choose a location to store the downloads, take default /home/<username>/ecos as example in this article
    • select "[1] arm-eabi", which is said in my course assignment instruction; however, I found we don't need this if we have arm-none-eabi (which we've installed already)
  • cd /home/<username>/ecos
  • cvs -z3 -d :pserver:anoncvs@ecos.sourceware.org:/cvs/ecos co -P ecos
  • in file "ecosenv.sh", modify "ECOS_REPOSITORY=/home/<username>/ecos/ecos-3.0/packages ; export ECOS_REPOSITORY" to "ECOS_REPOSITORY=/home/<username>/ecos/ecos/packages ; export ECOS_REPOSITORY"
  • . ecosenv.sh
    • this exports the system PATHs for ecos tools

Create HelloWorld Project

  • cd <somewhere you like to place you project>
  • mkdir ecos_project && cd ecos_project
  • ecosconfig new stm32f4discovery
  • configtool
    • change "eCos HAL->Cortex-M Architecture->Cortex-M3/-M4 STM32 Variant->STMicoelectronics STM32F4-Discovery board HAL->Startup Type" to ROM instead of JTAG
    • this starts an GUI interface, open the "ecos.ecc" file which locates in the folder "stm32f4discovery" just created
    • press "Build" -> "Generate Build Tree"
  • create an hello.c file, like:
#include <stdio.h>
int main(){
  printf("hello ecos!\r\n");

  • arm-none-eabi-gcc -o Hello.elf hello.c -Lecos_install/lib -I ecos_install/include -mcpu=cortex-m4 -mthumb -g -O2 -ffunction-sections -fdata-sections -Ttarget.ld -nostdlib
  • arm-none-eabi-objcopy -O binary -R .sram Hello.elf Hello.bin
    • finally, you got Hello.bin here, and burn it into stm32 by following the instruction in last article using QSTLink2 

Sunday, November 17, 2013

Fast way to Start VM and SSH into it on Mac

Though Mac OS is based on FreeBSD, but still lots of unix-based tools are not as good as those on Linux. And, it turns out people start to use "port", "brew", etc to get the tools they want. However, in my point of view, "port", "brew", etc are still not good as "apt-get" or "yum" since they are not well integrated.

So, I am using VirtualBox with Ubuntu guest. And, I write a simple script to:

  • Start the Ubuntu VM
  • SSH into the machine
  • Save the VM state when finishes SSH
I don't even need the GUI of Ubuntu. Also, the machine saves its state instead of turning off, so I don't have to wait long.

Tuesday, November 12, 2013

Operating System Study - Questions & Answers for Shumin

While I was discussing with Shumin, I found there are few things I don't really sure, so I wrote them down and try to find out the answers here. Since these are part of the discussion, they may be not that formal and precise.

Difference between Emulation & Virtualization

Virtualiztion: 模擬出整套系統真實的環境(包含模擬硬體),ex. VirtualBox。
  • Pros: 被模擬的環境比較真實,容易安裝或測試想跑的程式。
  • Cons: 消耗原本系統較大的資源(就像是你Mac會卡住)。

Emulation: 在原本系統中複製類似的環境,ex. WINE(可以讓你在linux/mac上面裝Windows程式的,很有名)。
  • Pros: 比較便宜、不耗成本的做法。
  • Cons: 不好用,被模擬的程式看到的環境比較差,容易壞、不易當測試環境。

What is "System Call"

  1. 先知道有個東西叫作API,是系統提供使用者的控制系統的方法,printf, read, write, open..都是。
  2. “The system-call interface intercepts function calls in the API and invokes the necessary system calls within the operating system.”
  3. “The intended system returns the status of the system call and any return values.”
Heron: 簡單來說就是system提供一些函式讓user可以call,之後怎麼handle就交給system(OS)去處理。

What is "static"

課本裡說”Thread-Local Storage”(TLS)類似於static,而不同處在於TLS在不同thread中是unique的。
static: "a static variable is a variable that has been allocated statically—whose lifetime or "extent" extends across the entire run of the program” (from Wikipedia) 表示static變數不是一般會動態分配的變數(heap memory)也不是函式呼叫時候的變數(call stack)。
而在C/C++語言中(http://en.wikipedia.org/wiki/Static_(keyword)) ,static變數表示:Lifetime為整份program執行期間、只能被內部(如同一class裡面)看見的變數。

Monday, November 11, 2013

Does the number of Cores = the number of kernel threads?


"Does the number of Cores = the number of kernel threads?"

While we were studying for the midterm of Operating System, few people started to ask this question. It seems that the textbook, "Operating System Concepts (by Abraham Silberschatz, Peter B. Galvin, Greg Gagne)", doesn't explain this clearly enough. Therefore, I started to find out the answer on the internet. Here comes my study.


NO! The maximum number of the kernel threads can up to around 15,000 on a general machine. And, the number of the cores mostly are round 2~4. It means that "one core can run more than 2 kernel threads in the same time", which is called "Hyper-Threading".


Friday, November 8, 2013

"Pitch It" is Released

App Site: http://pitchit.nctucs.net/
Apple Store: https://itunes.apple.com/us/app/pitch-it-perfect-pitch-challenge/id736721019?ls=1&mt=8

I've been working on this app, "Pitch It", for around one month. This interesting work is cooperated with Shumin, who has brought out lots of beautiful design and being a great partner.

"Pitch It" is a note guessing game. It plays a music note and ask to the user to tell if he/she can tell which note it is. People define a person who have "absolute pitch" or "perfect pitch" if he/she can answer more than 70% of the questions. With this app, the app can tell if you have "absolute pitch" or not.

On the other hand, this app is fast-implemented. I wanted to see how the Apple Store distribution works. That was a little complicated, and take a long for app reviewing by Apple. There, I write done time when the state is changed of this app on iTunes Connect.
  • Sat, Nov 2, 2013 at 12:38 AM: turned to Waiting For Upload
  • Sat, Nov 2, 2013 at 01:18 AM: turned to Waiting For Review
  • Fri, Nov 8, 2013 at 06:32 AM: turned to In Review
  • Fri, Nov 8, 2013 at 07:58 AM: turned to Processing for App Store
  • Fri, Nov 8, 2013 at 08:45 AM: turned to Ready for Sale
It means that takes 6 days for "Waiting For Review" state, and less than two hours for them to review and release.

Friday, November 1, 2013



若是這一台空的Linux機器安裝Wordpress,沒設定情況下會發現不能上傳相片,出現類似這樣的Error Message:"Unable to create directory wp-content/uploads"。
此問題來自上傳檔案這目標位置權限設定上並不能寫入,而網路上有許多做法是將 wp-content/uploads 改成777之類的,但這樣會變成任何人都可以讀寫,出現不合理的資訊安全問題。



(以環境為Ubuntu 12.04 with Apache為例)
mkdir <Web-root Directory>/wp-contents/uploads 
chgrp www-data <Web-root Directory>/wp-contents/uploads 
chmod 775 <Web-root Directory>/wp-contents/uploads


Thursday, October 17, 2013

Install OpenCV 2.6.4 on Ubuntu 12.04

I can still remember the time I was installing OpenCV library three years ago. It's not easy for the first time to install such a large library onto my own IDE. Knowledges about "Makefiles", system PATH, pkg-config, etc. are required to setup OpenCV. Also, you may also be familiar with your IDF, so that you can find where to fill in the right settings.

Here, take a common environment as example, is the way to install OpenCV 2.6.4 (or any other latest version) on Ubuntu 12.04. This is simple thanks for the script written by jayrambhia.

  1. git clone https://github.com/jayrambhia/Install-OpenCV
  2. cd Install-OpenCV/Ubuntu
  3. chmod +x opencv_latest.sh
  4. ./opencv_latest.sh

These steps ask the machine to download the script from jayrambhia's github, then run the script in it. Remember you may be asked to type in your password in order to run the "sudo" commands. After running the four commands, you'll completely installed OpenCV on your Ubuntu machine.

Sunday, October 13, 2013


There's some notes about "Naming" which I learned from "The Practice of Programming" by Brian W. Kernighan and Rob Pike. It's base on the book "The C Programming Language".
  • use descriptive names for globals, short names for locals
  • local variable
    • i and j for loop indices
    • p and q for pointers
    • s and t for strings
  • Programmers are often encourage to use long variable names regardless of context. This is a mistake: clarity is often achieved through brevity. (清楚與否需要透果簡潔以實現)
  • initial capital letters for Globals, all capitals for CONSTANTS
  • be consistent
  • use active names for functions
    • ex. getTime(), putChat()
    • bool functions: isoctal()
  • be accurate

Wednesday, October 9, 2013

STM32 Development Environment Setup on Ubuntu


During the class in "I/O and Drivers", I learned how to setup the environment on Windows including ARM GCC Tool Chain, GNUmake, and ST-Link Utility so that I can install our own code into the board, STM32.

Back to my home, I don't have any Windows machine. Then, I tried to build the same environment on Ubuntu or Mac OS X, which turned out that it’s much easier to setup the environment on Ubuntu. This report will cover the things I've done in order to install our own program on STM32 using Ubuntu.

Note: I'm using Ubuntu 12.04.

My Environment

  1. install the libraries or other dependencies we need
    • bash> sudo apt-get install build-essential git flex bison libgmp3-dev libmpfr-dev libncurses5-dev libmpc-dev autoconf texinfo libtool libftdi-dev libusb-1.0-0-dev && sudo apt-get build-dep gcc-4.5
  2. install ARM toolchain
  3. add the bin files to system PATH
    • echo 'export PATH=/home/YOUR_USER/sat/bin:$PATH' > ~/.bashrc
  4. install QSTLink2 (this program burns the bin files into the board)
    • install its dependencies: bash> sudo apt-get install qt4-qmake libqt4-dev libqt4-gui libqt4-xml qt4-designer qtcreator libusb-0.1-4 libusb-1.0-0-dev
    • download QSTLink2 source code: http://vedder.se/wp-content/uploads/2012/07/qstlink2.zip
    • unzip QSTLink2 source code
    • install QSTLink2: bash> qmake-qt4 && make && sudo make install && sudo reload udev

Run & Burn the Code

  1. make
  2. open QSTLink2
  3. connect
  4. select bin file
  5. send to the board

Tuesday, October 8, 2013


This post is my class note for 1.2: The Power of Prototyping

Why Prototype

  1. 在短時間內得到feedback
  2. 解決難以預測的情況

Rights for Prototype

  1. 不被要求是具有完整功能的
  2. 容易被修改的
  3. 一定會被換掉

Main Point

"Prototypes are questions, ask lots of them."

Saturday, October 5, 2013

Finite Automata

Few notes from my reading of Introduction to the Theory of Computation chapter 1.1:

Definition of Finite Automaton

A finite automaton is a 5-tuple (Q, Σ, δ, q0, F), where
  1. Q is a finite set called the states,
  2. Σ is a finite set called the alphabet,
  3. δ: Q × Σ → Q is the transition function,
  4. q0 ∈ Q is the start state, and
  5. F ⊆ Q is the set of accept states (final states).

Definition of Regular Language

A language is called a regular language if some finite automaton recognizes it.

Definition of Regular Operatioins

Let A and B be languages. We define the regular operations union, concatenation, and start as follows.
  • Union: A ∪ B = { x | x∈A or x∈B }
  • Concatenation: A ○ B = { xy | x∈A and y∈B }
  • Star: A* = {x1 x2 ... xk | k ≧ 0 and each xi ∈ A}
  • the concatenation operation attaches a string from A in front of a string from B in all possible ways to get the strings in the new language
  • the star operation is a unary operation instead of a binary operation
  • because "any number" includes 0 as a possibility, the empty string ε is always a member of A*, no matter what A is

Friday, October 4, 2013

constraintsWithVisualFormat, the most interesting coding style I've ever seen

WWDC 2012的演講中,介紹了auto layout,相對於其他文件,這關於排版的程式設計透過影片能比較快的學起來,其中最酷的是一個constraintsWithVisualFormat的函式,讓我們感覺到除了在裝置、界面設計上別出心裁的Apple,在開發者這端也有很有趣的設計。

Apple在iOS 5之後的開發平台上提供auto layout的功能,可以在點選storyboard點選某個view後,於右側的選單中開啓這功能(多已經預設),而constraints就是其中最重要的機制,不會複雜,說穿了,constraint就是一個一維線性代數(y = ax + b),裡頭的y和x分別是某個物件中的某個位置或長度,a和b就是個常數,於是乎,auto layout就是透過這些constraints對於物件擺放的敘述而排版,好的敘述會讓版面不論縮放、旋轉後都是好看的。

constraints可以用一般的函式一一輸入 y = ax + b,但是Apple在設計時候可能是發現這樣太麻煩了,一個constraint的函式表示要六、七行以上呢!所以提出Visual Format這樣的東西,是透過視覺化程式碼,對於喜歡ASCII Art的人的一大福音!

以下是WWDC 2012演講簡報中部分的內容,很酷!




Saturday, September 28, 2013


In the course "Formal Language", we learned about "DFA and NFA", and here's my study notes.


Finite Automaton

A finite automaton is a processor that has states and reads input, each input character potentially setting it into another state.
 In my opinion, we can see this as a finite state machine.

Deterministic Finite Automaton (1943)

A finite state machine that accepts/rejects finite strings of symbols and only produces a unique computation (or run) of the automaton for each input string. 'Deterministic' refers to the uniqueness of the computation.
In one state at a time.

Nondeterministic Finite Automaton (1959)

A finite state machine that (1)does not require input symbols for state transitions and (2)is capable of transitioning to zero or two or more states for a given start state and input symbol.
In more than one state at a time.

Difference between DFA and NFA

  • all transitions are uniquely determined
  • an input symbol is required for all state transitions
  • DFAs are easier to understand, but NFAs are generally smaller. 

Sunday, September 22, 2013

Upload File using jQuery-File-Upload

Original Code


My Work

This is cool jQuery library which allows the user to upload their files with cool interface. It also support "drag & drop", so it's much faster for people to select files. However, security may be a big issue in this case.

Problems I Met & How I Solved Them

Error, unable to upload the file

Solving this problem by giving right permission to the folder which the website is going to save files (in the example code, it's ./server/php/files). Make sure that "www-data" is able to "write" the folder.

The thumbnails are not showing up

I directly type in the URL of the thumbnails, and the browser showed a message asked me to read the error log file. In the log file, it says:
Invalid command 'Header', perhaps misspelled or defined by a module not included in the server configuration, referer: http://heron.nctucs.net/upload/index.html
So, here my solution from this website:
ln -sf /etc/apache2/mods-available/headers.load /etc/apache2/mods-enabled/headers.load

Saturday, September 21, 2013


Original Document

Facts (sentences from the paper)

  • In its early days, Facebook ran on a single server that cost $85 a month and had to handle traffic for every Harvard student who used the service.
  • Facebook.com was first coded in PHP, which was perfect for the quick iterations that have since defined our style of product rollouts and release engineering.
  • Facebook has grown to 5,299 employees and 1.15 billion users as of June 2013.
  • Every day, there are more than 4.75 billion content items shared on Facebook, more than 4.5 billion "Likes," and more than 10 billion messages sent. More than 250 billion photos have been uploaded to Facebook, and more than 350 million photos are uploaded every day on average.
  • To build more efficient apps, Facebook is working to solve:
    • Diversity of devices and operating system
    • Geographic diversity
      • 1.15 billion people
      • 70 different languages
    • Network connectivity
      • Testing to ensure consistent network connectivity (Air Traffic Control to simulate different network conditions right inside Facebook' offices)
    • Consuming less data


What I learned

Facebook.com was first complemented using PHP, what we all think it's easy and fast for developing. Then, they started to work very deep into PHP when more people using Facebook. This is new to me, what I thought is that this kind of websites doesn't go really deep into the technology but learning human psychology/business/etc insteads.

Wednesday, August 21, 2013



當航空公司頁面有更新的時候這隻程式便會發出聲音,而我設定的聲音則是「I am a bad feeling about this」,是個從免費網站下載的聲音檔。

import sched, time
import urllib2
import pygame

s = sched.scheduler(time.time, time.sleep)
url = "http://www.china-airlines.com/ch/schedule/cancel.htm"

original_html = ""

def do_something(sc):

    global url, original_html

    # do your stuff
    response = urllib2.urlopen(url)
    html = response.read()

    if cmp(original_html, html):
        print "New!!!"

    sc.enter(2, 1, do_something, (sc,))

def original():
    global original_html
    response = urllib2.urlopen(url)
    original_html = response.read()

def main():


    s.enter(2, 1, do_something, (s,))

if __name__ == "__main__":

Monday, July 1, 2013

Delegate in iOS

My question is "what is delegation?" after writing several iOS programs.

"Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object - the delegate - and at the appropriate time sends a message to it. The message informs the delegate of an event that the delegating object is about to handle or has just handled. The delegate may respond to the message by updating the appearance or state of itself or other objects in the application, and in some cases it can return a value that affects how an impending event is handled. The main value of delegation is that it allows you easily customize the behavior of several objects in one central object."

Basically, "delegation" is way that one object communicate with the other object.

這網頁中的Example 1是一個很好的範例,講述有兩個object,為brain和beer bottle,brain可以不斷下達指令給beer bottle要喝,但是brain因為要看電視所以太忙沒有辦法知道什麼時候beer被喝完,於是乎,beer bottle此時就要用delegation,當沒有beer時告知brain。

這網頁講述了delegation中物件的關係,若此時物件A call 物件B:

  • A是B的delegate object
  • B會有A的reference
  • A會implement B的delegate method
  • B可以與A communicate,用delegate methods


Sunday, June 30, 2013

Learning VIM (if using Windows)

  1. 安裝gVim http://www.vim.org/download.php#pc
  2. 透過ssh等方式連到一台linux/unix的機器
  3. 使用Cygwin創立linux/unix模擬環境






vim指令設計上其實是非常符合手指位置的(人體工學?)。一般而言,看到一份文件第一個用的指令是向下卷,即是指令 j ,可以使游標往下,而 j 恰好是右手食指位置,不需要移動手指就可以按到,向上的指令 k 則是位居第二名。

而有些則是從名稱的縮寫而來,例如 i 是insertion的縮寫 d 是delete等。


vim非常適合處理反覆的情況,例如,往上的指令是 k ,若希望往上八行,打 8k 即可。
更深入的部分有巨集,使用指令 q ,可以記憶一連串的指令,然後再重複!(寫verilog十分需要)






Real Tutorial這裡:
  1. 基本指令(其實大概80%在用的都出現在此網頁):http://yannesposito.com/Scratch/en/blog/Learn-Vim-Progressively/
  2. Vim Adventure,透過玩遊戲的方式學習vim: http://vim-adventures.com/
  3. Vim Cheat Sheet,圖片方式的指令集: http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html
  4. Complete Tutorial: ftp://ftp.vim.org/pub/vim/doc/book/vimbook-OPL.pdf
// end here

Drupal Installation

Follow the instruction in Drupal tutorial:

However, for the permission setting part, it's not working if I follow the instruction.
During the installation, Drupal may have to do some modifications on the files in sites/default/, they may be modified. So, 'w' is required.

The easiest way to solve the permission issue here is to grant all the permissions and modify them back as soon as the installation is done.

  • chmod 777 settings.php && chmod 777 ../default/
  • process the installation
  • chmod 644 settings.php ^&& chmod 755 ../default/

Sunday, June 23, 2013



#include <cstdio>
#include <vector>

using namespace std;

int main(){

 vector<int> myVector;

 // setter: push new
 myVector.push_back(2); // index = 0
 myVector.push_back(5); // index = 1
 myVector.push_back(1); // index = 2

 // getter: by index
 printf("%d\n", myVector[1]); // 5

 // get size
 int size = myVector.size();
 printf("size = %d\n", size);

 return 0;


std::map是C++中的函式,include <map>之後可以使用。



  1. setter:設定新的項目
  2. getter:取得已存的項目,若不存在會回傳0,並被加入map中
  3. is not found:檢查該key有無存在在map中
  4. wall through:走過所有map中的項目

#include <cstdio>
#include <map>
#include <iostream>

using namespace std;

int main(){

 map<char, int> myMap;
 map<char, int>::const_iterator iter;

 // setter

 // getter
 printf("%d\n", myMap['h']); //2
 printf("%d\n", myMap['e']); //3
 printf("%d\n", myMap['r']); //0, not exist

 // check if exsit
 int isNotFound;
 isNotFound = ( myMap.find('z') == myMap.end() );
 printf("%c\n", (isNotFound)?'y':'n'); // y

 isNotFound = ( myMap.find('e') == myMap.end() );
 printf("%c\n", (isNotFound)?'y':'n'); // n

 // walk through all elements in map
 for(iter = myMap.begin() ; iter != myMap.end() ; iter++)
  printf("- %c:%d\n", iter->first, iter->second);

 return 0;

Thursday, May 16, 2013

Matchismo on iOS

This is the first assignment for the course CS193P at Stanford 2013 winter session. It shows how the "MVC" structure works, and also includes really good coding style in objective-oriented programming.


Some interesting stuffs I would like to share is as below:

  1. Connecting the "View" related events or properties by dragging the object from GUI programming interface to text code. Cool.
  2. NIL pointer design, I've written things about this in last post.
  3. General initialisation design. Objects in objective-C mostly have the some usage in initialisations, setters/getters, function callings, etc. It's easy for the developers to learn some basic examples and apply on different occasions.
  4. Auto-complete editing design in Xcode. Although this feature, auto-complete while typing the code, is not new stuff, it's still the best auto-complete mechanism I've ever seen. It's fast, and smart. It allows you to jump to next blank in a very short time. While writing the program, you spent very little time on typing. Around 80%~90% of the code can be done by the auto-complete function, only the new names for object/function/etc are needed to be typed completely. At last, Xcode also generates those an annoying special characters in the code, like "", [], {}, etc.
Finally, the developing environment for iOS is not only including great designs but also so much fun.

Saturday, May 11, 2013

NIL Pointer in Objective C

In many programming languages, we do the checking if the pointer is null (equally, 0) or not before we start to grab the content. However, in Objective C, there's no need to do this. The design makes you no need to do so.

When a pointer has no object points to, it's automatically storing the value 0, which we call a NIL pointer. And, we can complete our other designs without checking the pointer is NIL or not because that function calls just do nothing on NIL pointers, and return 0.

Take one example I learned in the class,
if ([card.contents isEqualToString:self.contents] ){ score = 1; }
No need to card.contents is NIL or not. If it's NIL, isEqualToString just simply skip it and return 0, which may cause the if-statement failed, which is also the right thing we want.

Thursday, April 18, 2013

Why VIM?

" Vim不是「很難用的編輯器」,只是你不夠瞭解他 "


  1. 命令模式:每個按鍵變成指令,可在短時間內編輯你想要的東西,例如:「在行尾加句點(2 keys)」、「大小寫互換(1 keys)」、「複製目前這行九遍(4 keys)」、...。而做反覆的事情只需要加上數字便可以表示做幾次,另外甚至可以寫一整串的指令。
  2. 客制化:透過撰寫.vimrc這份檔案,可以把vim改成自己喜好的樣式,例如:「設定compile and run的快捷鍵」、「設定android開發時需要的指令成快捷鍵」、...
  3. 結合shell:Vim可以簡易的在編輯器中輸入外部系統指令,也可以讓外部容易的連上Vim編輯文件,相輔之下,讓人在寫程式快速穿梭在系統和文字編輯之間。



  1. 連續按左箭頭到句首(或是用滑鼠小心點擊)
  2. 按delete刪掉本來的小寫字元
  3. 輸入大寫字元
  4. 再連續按右箭頭到句尾(或是移動手改用滑鼠)
  5. 按'.'輸入句點
  1. 輸入:「^~A.」即可


Saturday, April 6, 2013

Web Image Grabbing Robot Using Google Image API

This time, I am facing a problem to grab corresponding images from a keyword list. Most of the time, we do Google Image Search, and find out the image we want. However, this will be inefficient when it comes to few hundreds of keywords.

Therefore, the program I wrote here automatic read keywords from the list, and call the Google Image API to find out the first three images relating to this keyword. The images are showed in URL. The reason for showing the first three images is that not all the time Google give out the one we want in the first element of the results. I've also written a website in PHP which allow people to select the image from these three images, and automatically grab the one which is selected to the server. As a result, you only have to check through which image you want to store in the end, you don't have to do any search or any downloading.

Python Program:
import urllib2
import simplejson

keyword = raw_input("Image Search >> ")
keyword_encoded = urllib2.quote(keyword, '')

url = ('https://ajax.googleapis.com/ajax/services/search/images?' +
       'v=1.0&q=' + keyword_encoded + '&userip=IP-INSERT-HERE-HERE')

request  = urllib2.Request(url, None, {'Referer': 'plate.nctucs.net'})
response = urllib2.urlopen(request)

results = simplejson.load(response)

for i in range(3):
 print results['responseData']['results'][i]['url']

Wednesday, April 3, 2013

Applying K-mean on CSV files using Python

What is K-mean?

K-mean is an easy to clustering the data, which knowing their features already. We call the input data entities as "observation", and the output groups as "cluster". Today, k-means is working for labeling n the observations into k clusters.

What is CSV?

CSV is a data storage format in plain text. This can be generate from excel, google form, etc. It's also easy to be applied in different languages, since it's simple syntax.

What Am I Writing Today?

I am writing a program can read the table from a CSV file which may be generated by excel or google drive form, and apply the k-mean algorithm. At last, it output the clusters as result showing the items in different clusters, also draw the points on the screen.


# This program attend to read data from a csv file,
# and apply kmean, then output the result.

from pylab            import plot,show
from numpy            import vstack,array
from numpy.random     import rand
from scipy.cluster.vq import kmeans, vq, whiten

import csv

if __name__ == "__main__":

    # clusters
    K = 3

    data_arr = []
    meal_name_arr = []

    with open('meals2.csv', 'rb') as f:
        reader = csv.reader(f)
        for row in reader:
            data_arr.append([float(x) for x in row[1:]])

    data = vstack( data_arr )
    meal_name = vstack(meal_name_arr)

    # normalization
    data = whiten(data)

    # computing K-Means with K (clusters)
    centroids, distortion = kmeans(data,3)
    print "distortion = " + str(distortion)

    # assign each sample to a cluster
    idx,_ = vq(data,centroids)

    # some plotting using numpy's logical indexing
    plot(data[idx==0,0], data[idx==0,1],'ob',
         data[idx==1,0], data[idx==1,1],'or',
         data[idx==2,0], data[idx==2,1],'og')

    print meal_name
    print data

    for i in range(K):
        result_names = meal_name[idx==i, 0]
        print "================================="
        print "Cluster " + str(i+1)
        for name in result_names:
            print name



Wednesday, February 20, 2013

Basic Things About namespace in C++


namespace顧名思義是指命名空間,底下可以有變數、函示的定義,使用時候可以在程式碼裡面加using namespace <name_of_the_namespace>即可,若是不加,則在要使用該namespace下的東西前面加來自的地方,例如:寫using namespace std; 之後,可以直接用cout;而若是沒寫,則可以用std::cout。

老師提供的空白檔內有一個稱為HOMEWORK的namespace,底下有三個函式,分別是NumberOfInversion(), Init(), Cleanup(),Init和Cleanup即是初始和結束要做的事情,NumberOfInversion是依照題目要求要完成的部分。


#include <iostream> 
#include "code_empty.cpp"

using namespace std;

int main(){ 

    using namespace HOMEWORK;


    int arr[] = {0,1,4,3,2}; 
    cout << NumberOfInversion(arr, 5) << endl;;


    return 0; 

*記得在編譯main.cpp時候要注意include code_empty.cpp的情形(視編譯環境)

Friday, February 1, 2013

Controlling Virtualbox From Command Line & SSH From Host to Client

Here, I got some virtual machines on my laptop. And I would like to learn how to control them via command line. The controls I am interested in includes, "start", "power off", "save current state", etc.

Take the "Fedora" virtual machine as example.

  • VBoxManage startvm "Fedora"
    • Turn on the virtual machine
  • VBoxManage startvm "Fedora" --type headless
    • Start the virtual machine without really start the window
  • VBoxManage controlvm "Fedora" savestate
    • Save the current state of the virtual machine and turn off the window
Setting up connection from host to client using ssh is also easy.
  1. on the setting page of the virtual machine
  2. click on network tab, and add a new adapter (usually, it's adapter 2)
  3. attached to "host-only adapter", with name "vboxnet0"
    • if virtualbox shows "invalid setting" in the lower part, go to the "preference" page of virtualbox, then find "network" tab, and add a new setting by pressing the button on the right hand side
  4. start the virtual machine, and click IP by typing "ifconfig"
  5. enable ssh server on the virtual machine, you may check it work or not by trying ssh to itself
  6. ssh to the IP you saw on the client with the right username