»

The riddle of experience vs. memory

Log — Tags: , — Posted by Starck on July 6, 2014

近十幾年來,科學家努力想解釋「快樂」該怎麼定義,如何產生與量化衡量。Daniel Kahneman 似乎在認知心理學的基礎下,提出人有兩種自我的概念:「記憶的自我」(Remembering self)和「經驗的我」(Experiencing self),並試圖用這兩種視角,解釋我們如何看待快樂。

這樣的二分法,源自他的新書 Thinking, Fast and Slow 所說的,系統一和系統二,可以聽聽看他在 google 的演講。

非暴力抗爭

Log — Tags: — Posted by Starck on April 7, 2014

問題

我們在不使用暴力的情況下,如何面對極端暴力?當你面對暴力,不管是一個小孩面對欺負他的小混混,還是家庭暴力或者是敘利亞今天的街道上,面對坦克和彈藥,最有效的方式是什麽?

反擊?屈服?使用更多暴力?

反思這個問題在目前的行動當中得不到答案

意識到我真正需要的,卻是我從目前訓練中得不到的。我真正想知道「暴力」「壓迫」有什麽作用。

我發現:惡棍使用三種暴力。

政治暴力來威懾,肉體暴力來恐嚇,以及精神暴力來摧毀。並且用更多的暴力幾乎不起作用。

綀習面對害怕

我需要的就是增長知識。 這意味著當我垮掉時 我要知道如何保持平衡,我的優點在哪,我的弱點在哪。我什麽時候屈服?我爲了什麽堅持?冥想或自我反思,這是一種方式,當然也不是唯一的方式,而我選擇透過這種方式,來獲得這種內在的力量。

引用例子:

我心目中一個理想型的英雄是在緬甸的 Aung San Suu Kyi (翁山蘇姬),她是一群在 Rangoon (緬甸仰光)的學生抗議團隊的領導者。他們面對一排機關槍,她立刻意識到在扳機上手抖動的士兵,比學生抗議者更害怕。 但她告訴學生坐下。然後她以出奇的冷靜,無畏,走向第一個槍,把她的手放上去,把槍放下來,所有人都安全了。

這就是控制恐懼能做的。不僅當面對機關槍時,更是當你在路上遇到刀戰。但我們必須練習。那麽我們害怕什麽呢?我有一箴言。「我越害怕,我的恐懼越大。如果恐懼太大了,不好的事情就會發生了。」

綀習將憤怒化為正面的力量

所以這就是恐懼。那憤怒呢?只要有不公正就有憤怒。但憤怒像汽油,如果你潑出汽油,然後有人點了一個火柴,那你就在地獄裏了。但作爲引擎,憤怒是很有力的。如果我們能把憤怒放入引擎裏,他能帶我們向前,他能讓我們走過悲慘的時刻,能給我們真正的內部力量。

溝通

我在和核武器政策制定者的工作中知道了這個。因爲一開始我對於他們給我們帶來的危險感到很憤怒,以至於我想辯論,責備,並且讓他們感到錯誤。但這根本不管用。爲了開始一個能夠帶來改變的對話,我們需要應付我們的憤怒。對於制定核武器的行為感到憤怒是很正常,但對人憤怒是沒有用的。他們和我們一樣是人。他們在做他們認爲對的。 而這就是我們和他們交流的基礎。

註譯: 如果我們認為我們正在做一件對的事情,那麼我們也需要先同意對方也在做一件他們認為對的事情。

是什麼帶來改變

所以這就是第三個,憤怒。現在我們來到了這次演講的核心,也就是現在在這個世界正在發生什麽?在上個世紀是由上而下的權利。過去政府仍然告訴人們該做什麽。這個世紀有了變化。現在是由下而上或者說草根權利。想從混凝土中長出的蘑菇。像 Bundy 所說,是人和人連結在一起,而帶來改變。

結論

重新認識自我,並運用我們的恐懼,把憤怒化作燃料,與他人合作,和他人結合在一起,勇氣,以及最重要的,積極的非暴力抗爭與溝通。

Geraldine Hamilton: Body parts on a chip

Log — Tags: , — Posted by Starck on February 17, 2014

Why it’s so dang hard to stick to a resolution

Log — Tags: , — Posted by Starck on January 9, 2014

“Ask yourself what you want for yourself and your life in the next year. What is it that you want to offer the world? Who do you want to be, what do you want more of in your life? And then ask: How might I get there?”

Psychologist Kelly McGonigal explains how to make resolutions that lead to real change:

SOLID principles

Coding, Design — Tags: , — Posted by Starck on October 1, 2013


The SOLID design principles, articulated by Robert “Uncle Bob” Martin, are five principles that provide a good foundation for sound application design. The five principles are:

  • S (Single Responsibility Principle)
  • O (Open/Closed Principle)
  • L (Liskov Substitution Principle)
  • I (Interface Segregation Principle)
  • D (Dependency Inversion Principle)


Single Responsibility Principle

A class (or unit of code) should have one responsibility.

設計一個類別,所有的實作應只為用來解決一個特定意圖(需求) 而如果為了解決這個需求,而延伸出其它的需求與實作,應該把這些工作交給其它類別。


Open/Closed Principle

A class should be open for extension but closed for modification. You can extend a class or implement and interface, but you should not be able to modify a class directly. This means you should extend a class and use the new extension rather than change a class directly. Additionally, this means setting class attributes and methods as private or protected properly so they cannot be modified by external code.

一個類別應能被很有彈性地擴充,而不是直接修改其自身來解決新的意圖(需求)。以此原則來決定這個類別的屬性和方法該怎麼被限制,或該開放什麼?

ValidationModel


Liskov Substitution Principle

Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program. In PHP, this often means creating interfaces for your code to implement. You can then change (switch-out) implementations of the interfaces, Doing so should be possible without having to change how your application code interacts with the implementation. The interface serves as a contract, guaranteeing that certain methods will be available.

每當我們在一個類別引用了其它類別時,多思考如何往後如何切換或取代這件事,之後如果不用到它,會有什麼影響?如何讓更換這件事不費力? 經驗上,我們往往會遇到一些被設計用來解決相同需求(購物這個需求),但有各自實作方式(Paypal, Stripe …等支付方法)的情況,我們可以利用界面(interface),定義一個大家共同遵循的規範(我們都應該有購買這個行為),這個概念如同讓這些類別簽定了一個契約(Contract),以確保都將一定會實行哪些行為。如此,我們只要專注那些行為在流程上的安排,不管現在這個實行的對象為何都不致於讓流程出錯,甚致隨時可以將其取代成別的也簽定契約的類別。

LiskovSubstitution


Interface Segregation Principle

Many client-specific interfaces are better than one general-purpose interface. In general, it’s preferable to create an interface and implement it many times over than create a general-purpose class which attempts to work in all situations.

因為在程式語言中,一旦行為(method)在界面(interface)中被定義,在類別中就要嚴格地去實作。所以應該避免用一個很概廓性的界面讓類別去實作很多不必要的行為。 就設計的角度來看,就是思考如何拆分行為的關連性這件事。


Dependency Inversion Principle

One should depend upon abstractions rather than concrete classes. You should define class dependencies as an interface rather than a concrete class. This allows you to switch an implementation of the interface out without having to change the class using the dependency.

This principle states that high-level code should not depend on low-level code, and that abstractions should not depend upon details.

依賴界面的關係比依賴類別來得更好,目的是希望在一個系統中元件之間相互依賴的程度(耦合性)盡可能的降低,以避免牽一髮動全身的情況發生。 以這個原則來說,最常見的情況,就是該怎麼設計如何切換資料來源的機制。資料的存取可能是在 MySQL, NoSQL, 甚至是 Memory,我們如何在不影響主程式太多的情況下改變來源?

舉例來說

這段程式大概是長得像這樣的句子:

「我要去MySQL檔案庫查詢使用者名單」。

這是一個耦合性高的句子,意即我們想要稍微改變一下行為,整個「」裡的文字都要變動,所以我們稍微調整一下:

我要去 MySQL 檔案庫「查詢使用者名單」。

MySQL 檔案庫為 來源地, 而 查詢使用者名單 為動作。

雖然我把來源地區隔開來,但實際上如果我改去 NoSQL 檔案庫,那麼這個句子還是會出錯,因為我們明確指出我們要到 MySql 檔案庫,該用什麼句子來表達才不會有問題呢?

答案是使用統稱:「我」要去「檔案庫」「查詢使用者名單」。

白話一點就是叫我們講話講得愈模糊愈好,就像是如果我跟一個以上的女朋友同時在交往,每一位打電話來我都叫她「親愛的」,這樣就可以避免叫錯人的情況啦。





Modern Testing with Laravel 4

Coding, Log — Tags: , , — Posted by Starck on June 18, 2013


Behavior-Driven Development

簡稱 BDD, 簡單地說就是用一種情境設定的方式來進行開發的過程。

在 Laravel 4 我們需要以下三種組合之一:


Codeception Installation

# 下載 codecept
$ wget http://codeception.com/codecept.phar
# 初始化 codecept, 它會建立所需的檔案在目前的目錄中的 tests 資料夾
$ php codecept.phar bootstrap

查看有什麼指令可以使用:

php codecept.phar

修改 tests/functional.suite.yml:

因為我們新增了一個 Laravel4 的 module, 所以讓 Codeception 再次重新建立所需的 Class:

php codecept.phar build


Codeception Usage

建立第一個測試:

php codecept.phar generate:cept acceptance Fisrt

Codeception 會在 tests/acceptance/ 下建立一個 FirstCept.php 檔案

內容為:


First Scenario

  • 我要到 / (網站首頁)。
  • 我看到了 Hello World! 這段文字出現在首頁上。

這樣的情境,寫成測試檔就變成如下:

執行測試:

php codecept.phar run


Zombie.JS and Mink

由於 Codeception 的 PhpBrowser 是基於 PHP Curl 去實現模擬 Browser 的操作, 所以面對一些會有 Javascript 互動的網頁,在測試上就會顯得力不從心。還好 Codeception 提供了 ZombieJS module, 我們可以得到一個更真實的模擬測試。

Zombie.JS

Zombie.JS 是一個 NPM 的 package, 所以在這之前我們必須先安裝好 NPM 的環境。

將 zombie 1.4.1 版安裝到全域環境:

sudo npm install -g [email protected]

2013-06-18 註: 經測試, 目前只能裝 1.4.1 的版本, 版本太新 Mink 不支援.

開啟 ~/.bashrc, 加上 NODE_PATH 的設定

export NODE_PATH="/usr/local/lib/node_modules"

Mink

composer.json 中加入到 require:

"behat/mink": "1.5.*@dev"

重新更新 composer:

composer update

目前已知問題: checkOption(), uncheckOption(), executeJs() 無作用


Selenium2 and PhantomJS

比起 Zombie.JS, 我更喜歡 PhantomJS, 更穩定且資源豐富. 主要看到官方的一些範例, 真得相當地強大, 甚至可以 Screenshot… (Codeception 用來當發生測試失敗時會將快照存在 log 資料夾, 相當地方便!)

要使用 PhantomJS 當作 Browser, 我們需要 Selenium2 作 driver.

先執行 Selenium2:

java -jar selenium-server-standalone-2.33.0.jar

將會啟動一個 Selenium2 Server, 預設會使用 port 4444, 如需修改可在後面加上 -port 參數:

java -jar selenium-server-standalone-2.33.0.jar -port 8888

08:17:34.571 INFO - Java: Apple Inc. 20.45-b01-451
08:17:34.572 INFO - OS: Mac OS X 10.7.5 x86_64
08:17:34.584 INFO - v2.33.0, with Core v2.33.0. Built from revision 4e90c97
08:17:34.691 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:4444/wd/hub
08:17:34.692 INFO - Version Jetty/5.1.x
08:17:34.692 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]
08:17:34.693 INFO - Started HttpContext[/selenium-server,/selenium-server]
08:17:34.693 INFO - Started HttpContext[/,/]
08:17:34.738 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@56d73c7a
08:17:34.738 INFO - Started HttpContext[/wd,/wd]
08:17:34.746 INFO - Started SocketListener on 0.0.0.0:4444
08:17:34.746 INFO - Started org.openqa.jetty.jetty.Server@54cbf30e

接著啟動 PhantomJS 跟 Selenium2 連接:

phantomjs --webdriver=4444

PhantomJS is launching GhostDriver...
[INFO  - 2013-06-18T00:17:27.627Z] GhostDriver - Main - running on port 4444

然後設定 Codeception 的 tests/acceptance.suite.yml 設定檔, 指定用 Selenium2 module:

  • url (required) – start url for your app
  • browser (required) – browser that would be launched
  • host – Selenium server host (localhost by default)
  • port – Selenium server port (4444 by default)
  • delay – To set delay between actions in milliseconds (1/1000 of second) if they run too fast
  • capabilities – To set Selenium2 desired capabilities. Should be a key-value array.

The Beauty of Data Visualization

Design — Tags: , — Posted by Starck on April 18, 2013

數據分析不是去尋找某個答案, 而是讓我們透過數據去想像, 將數字融入內容當中, 成為故事的一部分。

Git Deployment Solution

Coding, Log — Tags: , , , , — Posted by Starck on April 2, 2013

Why we need a Git Deployment Solution ?

In our past website develop workflow, we may often update files to production server by FTP, but it maybe causes some problems due to human factors, and it can’t be controlled and tracked for file version. As we increasingly need to maintain our projects by Git, if we are able to update server files while we do Git push, it sounds like a good idea, right? so we dont need to check which files want to upload any more. even we can push our project to development server and production server according to our needs.

在以往網站建置的工作流程中, 我們可能常常是用 FTP 的方式來更新 production server 的檔案. 這之中, 常會發生一些人為因素的失誤造成混亂, 而且也沒有一個紀錄來追蹤檔案的版本情況. 現在的工作愈來愈需要 Git 來維護專案, 而如果我們在每次 Git push 的時候, 也更動 server 的檔案, 似乎是個不錯的 idea? (我們不再需要用 FTP 自已判斷要上傳哪些有被異動過的檔案了!) 甚至我們可以先在本機測完, Git push 到 development server, 沒問題後, 再正式發佈到 production server 上線.

聽起來好像不錯? 那該怎麼作?

需要的工具

  • Git
  • 一台已經設好 SSH-Server 的主機 ex: production.com

設定流程

假設我們現在是這個情況:

  • 伺服器的 domain name 叫 production.com
  • 伺服器網站的根目錄設在 /var/www/production.com
本機端 (local)
mkdir "your project folder"
cd "your project folder"
# 建立 git 檔案庫
git init
# create release branch
git branch master
git checkout master
git remote add prod-server [email protected]:/var/www/production.com
伺服器端 (production server)
cd /var/www/production.com
git init
git config receive.denyCurrentBranch ignore
git branch master
git checkout master

我們需要用到 Git post-receive hook 來實現當收到 push 資料之後更新到最後版本:

vim .git/hooks/post-receive

將以下 script 內容加上去

別忘了讓 post-receive 是可被執行的:

chmod +x .git/hooks/post-receive

實作流程

本機端 (local)

我們先建立一個 README.md, 然後用 Git push 看看是否伺服器上也會同步:

Git 會用 SSH 的方式連線, 所以請先確認是否能用該 user 連線登入 production server.

如果想要之後只打 git push 就佈署到主機, 請加上 -u 參數 git push -u prod-server master

伺服器端 (production server)

確認 README.md 存在, 成功!

現在, 我們現在有一個比 FTP 上傳更聰明的佈署方式了.

2013-04-03 update: 今天在找資料的時候發現 Stack Overflow 有人在討論這個方式.

Stack Overflow: Deploying a Project using Git Push

.

(c) 2024 Starck Lin | powered by WordPress