Friday, 24 December 2010

Eclipse shortcuts

Action Shortcut
1. Go to the next highlighted word Ctrl + K
2. Go to the next/previous error Ctrl + , / Ctrl + .
3. Expand / collapse Ctrl + Numpad + / Ctrl + Numpad -

Tuesday, 7 December 2010

ActiveRecord

Associations
Auto-generated methods

Singular associations (one-to-one)
                  |            |  belongs_to  |
generated methods | belongs_to | :polymorphic | has_one
------------------+------------+--------------+---------
#other            |     X      |      X       |    X
#other=(other)    |     X      |      X       |    X
#build_other()    |     X      |              |    X
#create_other()   |     X      |              |    X
#other.create!()  |            |              |    X
#other.nil?       |     X      |      X       |

Collection associations (one-to-many / many-to-many)

                              |       |          | has_many
generated methods             | habtm | has_many | :through
------------------------------+-------+----------+----------
#others                       |   X   |    X     |    X
#others=(other,other,...)     |   X   |    X     |    X
#other_ids                    |   X   |    X     |    X
#other_ids=(id,id,...)        |   X   |    X     |    X
#others<<                     |   X   |    X     |    X
#others.push                  |   X   |    X     |    X
#others.concat                |   X   |    X     |    X
#others.build(attributes={})  |   X   |    X     |    X
#others.create(attributes={}) |   X   |    X     |    X
#others.create!(attributes={})|   X   |    X     |    X
#others.size                  |   X   |    X     |    X
#others.length                |   X   |    X     |    X
#others.count                 |   X   |    X     |    X
#others.sum(args*,&block)     |   X   |    X     |    X
#others.empty?                |   X   |    X     |    X
#others.clear                 |   X   |    X     |    X
#others.delete(other,other,..)|   X   |    X     |    X
#others.delete_all            |   X   |    X     |
#others.destroy_all           |   X   |    X     |    X
#others.find(*args)           |   X   |    X     |    X
#others.find_first            |   X   |          |
#others.uniq                  |   X   |    X     |    X
#others.reset                 |   X   |    X     |    X

Explanations of some methods:

others.build(attributes = {}, …): Returns one or more new objects of the collection type that have been instantiated with attributes and linked to this object through a foreign key, but have not yet been saved.


attr_protected
Attributes named in this macro are protected from mass-assignment, such as new(attributes)update_attributes(attributes), or attributes=(attributes).
Mass-assignment to these attributes will simply be ignored, to assign to them you can use direct writer methods. This is meant to protect sensitive attributes from being overwritten by malicious users tampering with URLs or forms.

Monday, 29 November 2010

Trash folder in ubuntu

It might be a good idea to set the default download folder of the browser to be the trash folder, so that you can quickly empty the folder. In ubuntu, the trash folder is in [user home dir]/.local/share/Trash/files.

Sunday, 28 November 2010

Chinese to English Translation

1. 蜂胶: Propolis ([ˈprɔpəlis]), resinous mixture that honey bees collect from tree buds, sap flows, or other botanical sources. It is used as a sealant for unwanted open spaces in the hive.
2. 皮蛋: Preserved egg
3. 分散圈Circle of confusion [photography]

Tuesday, 23 November 2010

git usages

1. Fixing un-committed mistakes
$ git reset --hard HEAD
This will throw away any changes you may have added to the git index and as well as any outstanding changes you have in your working tree.

If git reset takes a commit name as its parameter, it sets the current head to the specified commit and optionally resets the index and working tree to match. It defaults to HEAD (a keyword that refers to the most recent commit to the branch you're in).

Use --soft option when you want to stage all the previous commits but not commit them. This gives you a chance to modify the previous commit by adding to or taking away from it.

With --hard, it removes the commit from your repository and from your working tree. It's the equivalent of a delete button on your repository with no "undo". It's like the commit never happened, so it should be used with care.

2. Fixing commits
$ git commit --amend -C HEAD
This amends the previous commit, and keeps the same log message. "-C commit" takes an existing commit object, and reuses the log message and the authorship information when creating the commit.

3. Reset and checkout
$ git reset [commit] [--] path 
With a path parameter, git reset resets the index entries for all paths to their state at [commit]. (It does not affect the working tree, nor the current branch.)

This means that git reset paths is the opposite of git add paths.

After running git reset paths to update the index entry, you can use git-checkout(1) to check the contents out of the index to the working tree. Alternatively, using git-checkout(1) and specifying a commit, you can copy the contents of a path out of a commit to the index and to the working tree in one go.

So generally, use git checkout paths to revert changes in the working tree and use git reset paths to unstage changes.

A good explanation of reset and, its difference with checkout is here.

4. Tracking remote branch
Tracking when creating a new branch:
The full syntax is:
$ git checkout -track -b [local branch] [remote]/[tracked branch]
Simpler version is:
$ git checkout -t origin/branch
Setting up tracking for an existing branch:
& git branch --set-upstream branch upstream/branch

Thursday, 14 October 2010

Model View Controller

MVC provides a good abstraction and framework for combining GUI and the underlying application logic. However, different frameworks provides slightly different infrastructure for implementing MVC. Here's a summary of them.
 
Ruby on Rails:
You can implement a similar framework in Java:

  • Model - The model represents data and the rules that govern access to and updates of this data. In enterprise software, a model often serves as a software approximation of a real-world process.

  • View - The view renders the contents of a model. It specifies exactly how the model data should be presented. If the model data changes, the view must update its presentation as needed. This can be achieved by using a push model, in which the view registers itself with the model for change notifications, or a pull model, in which the view is responsible for calling the model when it needs to retrieve the most current data.

  • Controller - The controller translates the user's interactions with the view into actions that the model will perform. In a stand-alone GUI client, user interactions could be button clicks or menu selections, whereas in an enterprise web application, they appear as GET and POST HTTP requests. Depending on the context, a controller may also select a new view -- for example, a web page of results -- to present back to the user.
Interaction Between MVC Components

  1. The view registers as a listener on the model. Any changes to the underlying data of the model immediately result in a broadcast change notification, which the view receives. This is an example of the push model described earlier. Note that the model is not aware of the view or the controller -- it simply broadcasts change notifications to all interested listeners.
  2. The controller is bound to the view. This typically means that any user actions that are performed on the view will invoke a registered listener method in the controller class.
  3. The controller is given a reference to the underlying model.

Modifying the MVC Design
A more recent implementation of the MVC design places the controller between the model and the view. This design is common in the Apple Cocoa framework. 
The primary difference between this design and the more traditional version of MVC is that the notifications of state changes in model objects are communicated to the view through the controller. Hence, the controller mediates the flow of data between model and view objects in both directions. View objects, as always, use the controller to translate user actions into property updates on the model. In addition, changes in model state are communicated to view objects through an application's controller objects.

    Friday, 6 August 2010

    Rusell Peters' Quote

    "Chinese people and Indian people cannot do business together because Indian people can't get along without a bargain, and Chinese people cannot give you a bargain. Their objective is to get every penny from you. And ours is to keep every penny. There's a really bad power struggle there. I went to this Chinese mall. Some of you may know it - Pacific Mall. That's the wrong place for an Indian guy to go. I saw this bag; I want to buy this bag, I go to the Chinese guy behind the counter, 'How much?' He goes, [(In a Chinese accent)] 'Thirty-five dollars.' 'Um, How 'bout thirty?' And Chinese people will never tell you no. They'll tell you "no," the longest "no" you heard in your life, like you just said the most ridiculous thing they ever heard in their life. 'I'll give you thirty.' [(In a Chinese accent)] 'No-oooo. No-oooo. I can't do thirty dollars. I sell you thirty dollars today tomorrow you come I close down.'...[(In a Chinese accent)] 'Ok. You seem like nice guy. I give you best price. Thirty-four fifty.' I'm like, 'That's fifty cents, man.' [(In a Chinese accent)] 'Fifty cents is a lot of money. You save fifty cents here then maybe you somewhere else and you save another fifty cents, then you have one dollar. Then you take your dollar, you go to the dollar store, you buy something else.'"

    Sunday, 1 August 2010

    Hiking

    Went hiking on Friday with the census team. We went to two trails: one is the San Andreas Fault Trail in Los Trancos Open Space Preserve; the other is the Stevens Creek Nature Trail in Monte Bello Open Space Preserve.

    Monday, 26 July 2010

    Girl with a Pearl Earring

    By Dutch painter Johannes Vermeer. Watched the movie with the same title the other day, and was intrigued by the story that might be behind the painting. The stare in the girl's eyes is so intriguing and vivid. Before I could feel that there was something interesting in the painting because the head decoration and the clothes seem plain in contrast to the pearl earring. Didn't give much thought about the possible reason. The story in the movie, although fictional, seems pretty plausible.

    Friday, 23 July 2010

    Fairy Tale

    I forget for how long I haven't heard you telling me your favorite story.
    I've been thinking over and over again, and start to panic and wonder if I've done something wrong.
    You tell me in tears that fairy tales are lies.  I can't possibly be your prince.
    Maybe you will never understand that since the day you said you love me, the stars in my sky have brightened up.
    I want to be the angel in the fairy tale, extending my arms and turning them to wings to protect you.
    You have to believe, believe that we will live happily ever after just like in the fairy tale. And we will write our happy ending together.

    Sunday, 20 June 2010

    当爱已消失时

    没想到蔡康永写的一些话还很有哲理。想想也是,他口才确实很好,也很幽默,想必读的书应该不少。他说当发现爱已消失时,往往无比惊愕,不知发生了什么。可是如果回想当初爱降临时,其实也是何等的不明白没道理。

    to love or be loved

    to love or be loved, if can only choose one, i'll choose the former as it makes me feel more alive while the later makes me feel burdened

    Sunday, 23 May 2010

    diet and exericise rules

    diet:
    no flour & no sugar diet: no bread, no cookies, no dessert, no cakes
    no food after 7pm

    exercises:
    average 1 hour exercise per day (karate, squash, swim, bicycle, walking)

    往事随风 - 小娟与山谷里的居民

    你的影子无所不在
    人的心事像一颗尘埃
    落在过去飘向未来
    掉进眼里就流出泪来
    曾经沧海无限感慨
    有时孤独比拥抱实在
    让心春去让梦秋来
    让你离开
    舍不得忘
    一切都是为爱
    没有遗憾还有我
    就让往事随风都随风都随风心随你动
    昨天花谢花开不是梦不是梦不是梦
    就让往事随风都随风都随风心随你痛
    明天潮起潮落都是我都是我都是我
    就让往事随风都随风都随风心随你动
    昨天花谢花开不是梦不是梦不是梦
    就让往事随风都随风都随风心随你痛
    明天潮起潮落都是我都是我都是我
    (music)
    你的影子无所不在
    人的心事像一颗尘埃
    落在过去飘向未来
    掉进眼里就流出泪来
    曾经沧海无限感慨
    有时孤独比拥抱实在
    让心春去让梦秋来
    让你离开
    舍不得忘
    一切都是为爱
    没有遗憾还有我
    就让往事随风都随风都随风心随你动
    昨天花谢花开不是梦不是梦不是梦
    就让往事随风都随风都随风心随你痛
    明天潮起潮落都是我都是我都是我
    就让往事随风都随风都随风心随你动
    昨天花谢花开不是梦不是梦不是梦
    就让往事随风都随风都随风心随你痛
    明天潮起潮落都是我都是我都是我

    Saturday, 22 May 2010

     on privacy and facebook

    there's much debate about the privacy issues with facebook. i think as long as people have a choice about how exposed they want to be, everything should be fine. the popularity of facebook shows that it does provide value to the users, and it's legitimate for them to ask for something back. nothing comes for free, otherwise how are they going to survive and how the facebook employees going to earn their lives? so we need to trade something for the service facebook provides. this is how the world works: trading governed by the law of comparative advantage. we have our own strength in doing certain things, but not everything.  in this case, facebook has the social utility service that has value, and the users have their personal information that also have value, and they trade these two things.

    edward benson in his blog post mentioned two alternatives for facebook users.
    1) facebook asks for a fee for using its service, but doesn't sell the user information to third parties. this could work. with such a large user base (more than 400 million active users), even a small fee can make the company very profitable and the fee can  be insignificant to the users too. maybe we are too used to the free service paradigm for internet services that we forgot about this option. if people think it's worth the price, they would pay, unless they think their personal information doesn't worth the fee. if this is the case, then there shouldn't be such privacy issues. people may have different opinions about their privacy. maybe there can be 2 options. for those who don't want to have their information shared to third parties, pay a subscription fee.

    2) if our personal information and web browsing information  is valuable, why don't we directly sell it to the third party? i think this goes back to the point of law of comparative advantage. not everyone has the technical expert to build web tools to do so. if we try to do everything ourselves, we go back to the primitive society with self-sustainability. the law of comparative advantage says if parties produce good or service that they can do with higher efficiency and trade for other good or service, there is a net benefit for all the parties.

    Wednesday, 19 May 2010

    the requested lookup key was not found in any activation context

    i was trying to figure out why my usb flash drive was not detected when plugged in. so i followed instructions in this link http://support.microsoft.com/kb/925196 to change the registry. bad bad idea. after deleting the UpperFilters registry value and restarting, a lot of things stopped working, for example, couldn't access the H drive. so i repaired the windows os using the reinstallation disk.

    after this, things mostly went back to normal, but ie stopped working. basically couldn't navigate to the website by typing the address in the address bar. it gave the error in the title. could still click through links in the home page displayed. i searched the error online, and most pointed to the problem with ie 7, i checked the version i had and it was ie 6. tried a lot of things, installing ie, uninstalling it, repairing os. in the end, what works is upgrade ie 6 to ie 7 by doing all the necessary updates. after this, ie still doesn't work. even worse, it would launch. but then the upgrade came with a unstallation program in %windir%\ie7\spuninst\spuninst.exe to uninstall ie 7. after that, everything works.

    usinstall ie 7: http://support.microsoft.com/kb/927177

    Friday, 23 April 2010

    beijing olympic games sport pictograms

    really like this set of sport icons used in the beijing olympic games. this is one thing i like about the beijing games, the logo and these icons, because of the creativity and the cultural reference to chinese calligraphy. i think chinese calligraphy is an epitome of simplicity and elegance. these two elements constitute my sense of aestheticism. With strokes of seal scripts as their basic form, these icons integrate pictographic charm of inscriptions on bones and bronze objects in ancient China with simplified embodiment of modern graphics. i think this is really brilliant, and once again shows the wisdom in chinese characters which can represent ideas and concepts and also have phonetic significance too.


    seal script

    seal
    image sources:
    1. http://www.beijing2008.cn/20/28/column212032820.shtml 
    2. http://www.qhsl.gov.cn/Html/xuexiyuandi/2009-1/2/040729842.html
    3. http://mypaper.pchome.com.tw/tomy120/post/1312477236
    4. http://www.sinoarts.net/product.php?productid=16267

    Sunday, 14 March 2010

    stucked dvd

    trying to watched the dvds from the exercise ball package, but the dvd got stucked in the dvd drive making noise without responding. the computer froze too. so i turned off the computer, but on restart, it went into a gray screen and stopped there. turned off and on several times without success, only hearing the noise of the spinning dvd. a little bit panicked, i searched the internet using my ipod touch. turns out people having this problem too with their macbook pro. there are some suggested solutions. the one worked for me is after turning on the computer, press the eject button down until the dvd comes out. phew!

    Thursday, 11 March 2010

    shortcuts as i stumbled upon

    mac, firefox:
      1. command + shift + f: full screen
    mac, mail:
      1. opt + command + f: search

    Sunday, 28 February 2010

    sake

    went to blue fin yesterday for dinner and tasted sake for the first time. there is a sampler special that you can choose 3 kinds of sake. always thought sake is very strong, like the chinese rice wine (baijiu), turns out it's usually 18%-20% alcohol. comparing to the baijiu which is 40-60% alcohol, sake is like nothing. turns out i'm confused between sake and shōchū. shōchū is also a japanese alcohol drink which is 25% alcohol. shōchū may be similar to the korean soju.

    can't remember the names of the 3 types of sake we ordered. one was nigori which appears cloudy because it's unfiltered. contrary to its cloudy appearance, it tastes clear and refreshing. no strong taste. not very strong in alcohol either. can taste the the sweetness of the fermented rice as well. The second one is a dry one. don't quite like it. only tasted alcohol. the third one is koshu (古酒), "aged sake." it has an orange coloring and an interesting and distinctive taste.

    Friday, 5 February 2010

    一年

     Those of us who fell for the professor cast ourselves as Cinderella intellectuals, waiting for the phrase - rather than the slipper - that fit us perfectly. We waited, at fourteen, at nineteen, at twenty-five or even thirty-five, for the figure who would see what was hidden and special and glorious in us, who would love us for our smart selves alone and not our yellow hair - or so we thought - who would play out every fantasy of sibling rivalry we ever had and choose us from among all our peers for attention. -- regina barreca "contraband appetites"
    一年前,当我走进6.824的教室,一切没有什么不寻常。这么多年的求学生涯,上过的课无数,而这门课也只是其中之一。虽然很多人都说这是系统课里最难、作业量最大的课,我还是很坚定的选择了它,因为我觉得分布式系统是现在和今后很重要的领域,而且我也很喜欢写程序,特别是用c++。

    教室很小,而第一堂课来听的学生却不少,都坐到门外了。走进来一位高高瘦瘦的教授,做了自我介绍,然后就开始讲分布式系统的一些大的概念和课程安排。一切还是那么寻常,波澜不惊。

    课业确实很重,每堂课前都要读一篇paper,还要回答一个问题,并且要交的。每周都一个lab要完成,要看要写不少程序。学生也渐渐少了, 三十来个吧。我不算是个刻苦的学生,况且还有研究项目要做。 我喜欢做lab, 每次新的lab一出,我就花上5,6个小时做完,bonus的部分也做。但是读paper就没什么耐心,不是整篇读完,只是读到能回答问题就行了。我自以为这样很有效率,反正看了重点就行了,等以后考试前再仔细看一边,那时更清楚什么是重点。可是这样在上课时,教授提的关于paper上的问题,我并不都能回答上来。现在想来有点后悔,如果我更认真一点,上课更积极发言,也许能给他留下更深刻的印象。可那时我还没有要留下好印象的冲动。就像以前任何一门课一样我只是安静的听课,有时即使知道答案,也懒得回答。不过我还是回答了几次问题的,有些一反常态,可能是因为人比较少,所以我也有胆量了。回答问题的感觉真的很好,我似乎能感到教授投来赞许的目光。可是有时我就是提不起劲来回答,或是怕回答错了。但后来发现其实我知道答案后,就会非常懊悔。特别是临近学期末的时候,那时我真的很想给教授留下好印象。可是越是这样想,越是心有顾忌,越是不敢发言。每次这样,下课后我就情绪低落,甚至厌恶自己的怯懦和内向。

    学期过半时,有一天课后,我问助教一个关于lab的问题, - 我一般总是先问助教, 因为对教授有种敬畏感, - 我说觉得一个squence number没必要。教授碰巧也在旁边,听后又在黑板上画了可能出现的communication sequence and failure mode, 觉得不用sequence number是可以的,"if you are being  smart and careful in other places",(但其实sequence number对于以后的部 分还是有用的, 如server and client failure),但他还是对于我的提问给予了肯定。至今我还能在脑海中回放他笑着对我说"You stunned us." 那一瞬间,我觉得有些惊愕,似乎"stunned us"有些夸大了,可还是满心欢喜,看着那满眼的笑意,他那炯炯的眼神直透我心底。老师,你太强大了,一个不经意的赞许把我彻底击败。

    期中考试考得还不错,lab也都是满分,我觉得我对系统的兴趣确实很大,就像以前在ubc上的computer architecture, 我也花了很多精力去做branch prediction competition, 最后得了第一,那时的兴奋也是难以言表,老师也对我特别欣赏,那也是个很帅的教授。正如regina barreca在contraband appetites中提到的:
    the confusion between loving the profession and loving the professor
    我有时真的不知道我是因为喜欢系统这个领域才喜欢教授呢,还是反之。或许两者都有吧。

    快到学期末时,我发觉上课的时候我越来越不敢看教授了,只是盯着黑板或看paper或写笔记。因为我害怕,害怕与那智慧的眼神交汇,害怕电流从视神经直击心脏,使之飞速跳动,害怕我会脸红,因为我无法掩饰我的爱慕。

    最后一堂课是project presentation, 由于电脑与投影仪不兼容,准备好的demo也不能演示。虽然其他和我用一样mac book pro的同学都不能演示,我还是很郁闷,辛辛苦苦准备的演示,就白忙活了,而且也没有机会impress教授了。整整一天心情都很沮丧,直到教授发来email问大家能不能把report放在网上,在回复的email中,我说我对没有做第二手准备感到抱歉,没想到教授马上回复说是他没有测试好投影仪,还说我口头的解释也很好。看到这,我心花怒放,低落的心情烟消云散。

    教授还给每个同学发email, 感谢我们上课的参与。我想他是给每个同学都发的,虽然email里写着我的名字,当然自动的生成不一样的名字也不是什么难事,我还是感到很窝心。教授真是细心啊。

    Wednesday, 3 February 2010

    ai and safety

    there are more and more electronics and intelligent devices installed in the car. however as the safety issue is really crucial for the car, we need really robust artificial intelligent systems in the car if we'll ever have one. i'm very pro-technology. i think the world will become more and more digitalized in very fabric of our life. intelligent car should be the trend, however there is still a long way to go from the current state of ai development. as a first step, we can have intelligent system as an aid that augments human's ability, but it cannot replace human as the main controller and there should be easy ways to over ride the automatic systems.
    http://tinyurl.com/ye6qvsn

    Sunday, 24 January 2010

    dancing with my shadow

    the name of my blog comes from a line in song ci (宋词) "shui diao ge tou (水调歌头 )" by su shi (苏轼). song ci is a from of classical chinese poetry from the song dynasty. they are actually lyrics, and with tunes, they can be sung. here's the english translation (found here) and the original chinese version of "shui diao ge tou". the translation is not bad, in my opinion.

    Shui tiao ko tou
    The moon -- how old is it?
    I hold the cup and ask the clear blue sky
    But I don‘t know, in palaces up there
    When is tonight?
    If only I could ride the wind and see --
    But no, jade towers
    So high up, might be too cold
    For dancing with my shadow --
    How could there, be like here?

    Turning in the red chamber
    Beneath the carved window
    The brightness baffles sleep
    But why complain?
    The moon is always full at parting
    A man knows grief and joy, separation and reunion
    The moon, clouds and fair skies, waxing and waning --
    And old story, this struggle for perfection!
    Here‘s to long life
    This loveliness we share even a thousand miles apart!
     
     水 调 歌 头
    明 月几时有
    把酒问青天
    不知天上宫阙
    今夕是何年
    我欲乘风归去
    又恐琼楼玉宇
    高处不胜寒
    起舞弄轻影
    何似在人间

    转朱阁
    低绮户
    照无眠
    不应有恨
    何事长向别时圆
    人有悲欢离合
    月有阴晴圆缺
    此 事古难全
    但愿人长久
    千里共婵娟

    how to add a cloud label to blogger

    here's the tutorial on how to add a cloud label to blogger as the one i have on the right: http://phy3blog.googlepages.com/Beta-Blogger-Label-Cloud.html

    avatar and fault tolerance

    watched the movie with lei, ermin and yehua on the second day of its worldwide release. we had an interesting conversation right after the movie. in the movie, dr. grace augustine said that the roots of the trees on pandora form an enormous and intricate network, and there is communication and information flow. the last part of the movie is about humans trying to destroy this network of life, and they found that there is a mother tree destroying which can destroy everything. so this leads us to find the analogy in the network on our planet, the internet. the fact that they have a mother tree is not very fault tolerant, like a client-server architecture where the server will become the central point of attack. this again shows the importance of distributed system. a purely distributed system means every node is equal. even some nodes fail, the system can still function, and it's hard to take down every node. purely distributed system is hard to implement and only exists in research projects. many p2p systems (skype, bittorrent) are still a hybrid of client-server and p2p architectures. makes me want to change research group even more because i think this area is very important.