2009年4月6日

[軟體與現實生活的類比-1] 為什麼某些軟體只接受特定格式

有時候要跟完全沒有資訊背景的人(例如上一代的長輩們,或者是客戶!)解釋軟體如何運作是一件很困難的事情,我覺得一個很重要的原因就如同之前推薦的超長好文中的 Lessons From The Science of Nothing At All (Richard P. Gabriel) 中講的,因為軟體是極度抽象的東西,很多時候沒辦法直接對應到現實世界中大家習慣的事物,因此一般人很難理解資訊人員腦袋裡面到底裝了什麼東西,偶爾旁聽到資訊人員間的聊天內容,想必也覺得那根本就是火星話。

但其實如果仔細思考一下,還是有很多時候可以找到軟體跟現實世界之間的類比,
所以我打算慢慢把自己想出來的例子記錄下來,看看假以時日可以蒐集到多少 :D

Analogy 1: 軟體只能吃特定的檔案格式 <---> 車子加油的時候一定要加到油箱裡面

緣起:
上禮拜我老爸(沒錯,又是他)在整理數位相機的照片,問我說能不能把存在電腦裡面的照片存回去數位相機裡面,我說這當然 ok 阿,基本上記憶卡就跟硬碟是一樣的東西嘛。結果嘗試了一下,發現圖片檔的檔名要 follow 固定的格式,並且要放到指定的資料夾,
才可以從相機裡面預覽,為了跟我老爸解釋這種「詭異」的現象,於是我想到一些類比。

首先,為什麼圖檔一定要放到指定的資料夾,否則相機就讀不到?這就跟車子加油的時候一定要把油加到油箱裡面是一樣的道理,如果你莫名其妙的把油加到水箱(把圖檔放到非指定的資料夾中),那麼不管加再多的油進水箱,油表絕對是顯示見底的。

再來,為什麼圖檔的檔名要 follow 固定的格式?突然發現這題沒有想到一個好的類比,等之後有緣想到再補。

接下來討論為什麼每個軟體只能接受特定的格式。這就跟車子只能吃 92、95 或者 98 無鉛汽油一樣,雖然組成比例略有不同(e.g., word 檔的內容不同),但本質上都是汽油(格式都是 word),因為汽車的引擎就是設計要來吃汽油的,如果你無聊拿一桶蘋果汁倒進油箱,就算位置正確了,因為格式不對,車子是絕對不會跑的(e.g., word 沒辦法讀 powerpoint)。
呼,第一篇就先寫到這裡吧,最近遇到一些問題,可以寫成另外一篇。我不知道練習這樣的思考對工作上會不會有很實質的幫助,但是如果在資訊人員之間偶爾也可以透過這種類比的方式來溝通,也許效果會比較好吧!

MS Chart Control 製作學生成績表

雖然坎尼在Chart Control出來時就裝好了元件,卻一直沒去測試這個新元件
另外看到很多blog都說摸索有點瓶頸
坎尼也不敢任意踏入無止盡的研究迴圈中...

最近有遇到要畫統計圖表的功能
加上 Tech. Days 聽了章立民老師的 ChartControl 線上研討會
於是放棄以前 OWC 的方式,用新的 Chart Control 來看看

元件安裝請參考保哥的 介紹好用元件:Microsoft Chart Controls for .NET 3.5
sample 檔一定要下載回來看,有很多 code 可參考

下圖是坎尼做的 sample -- 學生成績表
此表的特性為:
  • Y軸為自訂Label
  • X軸的學生名稱為直向
  • 長條圖上有分數顯示
  • 低於60分的人以紅色顯示成績條


這個表怎麼做的呢?
請先在頁面上放置個 Chart Control (如下圖)

接著用屬性 Panel 設定圖層的一些資料
但 Chart Control 可以用的屬性實在太多太雜了
坎尼也研究了半天才了解一些實用的屬性在哪設定
下圖為已經設定好的屬性 source code,下面會有解釋

Label 表示長條上要顯示的資料,#VAL表Y軸的值 (這邊是成績)

AxisY 為Y軸屬性,坎尼設定了 Maximum 為 100,表示最多顯示到 100
此項屬性不設定則 .NET 會自動控制 Y 軸的顯示值

接著是 CustomLabels,這邊自訂了 Y軸的標籤
這裡的重點是 ToPosition 屬性要設定,否則自訂標籤不會出現

AxisX 就是X軸屬性啦
這邊設定了 Interval=1,表示X軸上的所有標籤都會顯示
(預設為 Auto .NET會自動分配區間給 X 軸)
但 Label 的顯示會是橫向,自定義區間為1會造成標籤都擠在一起
待會兒坎尼會在後端的Code做點小處理

再來就是很重要的 DataBind 事件了

其實在 MS Chart Control 的論譠上有講到
要用 Chart 的第一步就是先 DataBind,看資料是否能正常顯示
再去設定顯示的樣式
坎尼為了容易理解,這裡就反過來講

可以看到 Chart 的 DataBind 方式不太一樣
給了第一個參數為 DataSource 接著是 x 及 y 軸的 DataMember
其實它也是可以支援
Chart1.DataSource = dt;
Chart1.DataBind();

DataBind之後就來處理顯示的樣式吧
剛剛有提到字會重疊在一起
於是坎尼把 X 軸的 Label 取出來加工,讓它變成直向
然後把 Y值小於60的長條變成紅色
如此一來,不及格的學生就一目瞭然
實際畫面會是第一張圖所顯示的那樣

其實 Code 不會寫很多
比較容易卡住的是一些前端屬性的設定
以後有空坎尼也來寫一下 Chart Control 的應用吧
大家有興趣可以上 MS Chart Control 的討論區

話說 snipping tool 的 highlighter 很好用
圖片中的黃底色都是用它來畫的

2009年4月5日

微軟的組織架構 & Developer 的工作態度

上週去 Microsoft Innovation Center (MIC, 在南港) 上課,第一天講的是 unit testing,
講師是 Bentham Chang (張濱顯),人蠻開朗的,上課的過程還蠻歡樂。其實關於 unit testing 的作法等等技術細節網路上資料很多,我比較有興趣的是他分享的在微軟工作的經驗,還有微軟的文化。

從 Developer (Dev) 的角度出發,工作上最直接相關的組織長得像下面這個樣子(接下來會大量使用到 Dev 這個 term):

MS_Dev_Map 
雖然我的圈圈畫的很醜,但我想表達的是在 MS 沒有上下階層的關係,三角形好像還是有個頂點:p 當然每種職位一定有 junior/senior 之分,也有各自的 Lead (e.g., Test Lead, Dev Lead)。


基本上從職稱就可以知道大略的工作內容(聽說很多美國公司也是採用類似的架構),PM 就是專案經理,Test(er) 在台灣常常叫 QA,Dev乍看之下好像是PG (Programmer)。

但是在台灣很多公司是 follow IBM Mainframe 時代的 terms,所以會有 PG/PA/SA/SD 等職位(很有 waterfall 的 fu),在上圖中看不出 PA(程式分析)、SA(系統分析)、SD(系統設計) 等職位,但是顯然這絕對不是說在微軟做軟體都不需要經過這些步驟。

我的感覺是 MS 的 PM & Dev 都是一起 co-work 的,只是 PM 面對客戶的時間比較多,主要負責寫 spec(相當於台灣的 PA/SA 的工作),但是過程中要密切跟 Dev (lead) 討論系統設計(SD)的問題,最後由 Dev 負責把程式實作出來(PA 大概算基本能力吧)。

另一個很明顯的重點是 Tester (QA) 是很重要的職位,必須要請人專職負責,而不是把 testing 當做 Dev 的工作中的其中一項。從這裡也可以思考,如果像微軟這樣重視 testing 的公司做出來的產品還是有可能被批的滿頭包,那麼沒有專業請人做 testing 的公司做出來的產品,品質/安全性會是怎麼樣?

扯遠了,Bentham 說,在 MS 內你可以自由選擇要做上述三種工作的哪一種,並且每種職位還可以進一步選擇要做 Indivisual Contributor (IC) 或是 Lead (manager)。

MS 的 Dev 都是很聰明而且非常有自主性的,基本上可以說整間公司都是自我實現的熱血的人(自我實現這個 term 請參考 Maslow’s hierarchy of needs),所以不適合用「公司政策」這種政治性的手段來強迫推行什麼制度(e.x., unit testing),因此 MS 管理 Dev 的原則是先盡可能提供他們一個舒適的工作環境(食物啦、飲料啦),之後要推動政策的時候,只要能夠說出個道理(e.g., unit testing 的好處),就能讓 Dev 自動自發的去執行了。

MS 的 Dev 有什麼需求呢:
  1. (Free) Food:Google 也是這樣搞的。台灣微軟好像也差不多。MIC 和 MIT 都有很多零食飲料。
  2. Quite (environment):就是說喔,不要跟 sales 坐同一間辦公室啦!
  3. Keyboard:在 199x 年的時候 MS 很多 top Devs 比較習慣 unix-like 的 text editor/command-prompt tool,很不喜歡用滑鼠的
  4. Cool idea:設法把公司要推動的東西塑造成很酷的,讓 Dev 認為做這些事情是他們自己想要的
  5. Ship it:「我的程式被幾千萬人使用」是 MS Devs 成就感的來源,所以儘快 ship product 是很重要的
所以要推動 unit testing,首先滿足上述前四項之後,利用第五項來設定一個門檻:unit testing 沒達到門檻就不給你 check in,就不能 ship product,這樣 Devs 就會嚇到了。

另外一個故事是以前 MS 每週會舉辦一個 tool 比賽,因為 Devs 在平常工作的時候很喜歡自己做一些小 tool 來自動化一些重複、繁瑣的工作,因此為了鼓勵這個風氣,每週舉辦一個 tool 比賽讓大家炫耀一下自己的作品,大家覺得不錯的 tool 就會考慮整合到產品中,據說 VSTS 的 testing framework 就是把這類 tool 整合進產品的結果。

呼,寫到這裡有點像流水帳,不過其中有很多可以好好思考的地方。就讓我用「這跟台灣真是差很多阿!」來草草結尾吧,千言萬語,不知從何說起阿~ :p

如何快速取出內嵌於 Microsoft Word 檔的大量圖片

上個週末老爸問我一個問題:要怎樣把 word 裡面的圖片抓下來?
 
我想這是很簡單的問題嘛,所以我就叫他在 word 裡面點選那張圖, 然後用右鍵選單的「複製」或者按「ctrl+v」來複製圖片,再貼進小畫家另存新檔。

然後我就繼續回頭做自己的事,過了幾分鐘老爸又丟了一個新的需求:我這個 word 檔裡面圖片很多耶,這樣每張圖片都要重複一次這個動作,很麻煩耶,而且我不想每張圖片都要自己重新命名啦!

……

說實話從來沒遇到過這麼實際的需求,於是我開始思考要怎樣很方便的一次取出內嵌在 word 檔的所有圖片,而且這方法必須是對一般正常人是很簡單的,我總不能叫老爸打開 VBE 寫一段 VBA recursive 的把所有圖檔取出來,然後 call FileSystem API 去存檔吧?!

這時候我想起來去年有一次研討會講如何客製化 Office 2007 的 Ribbon,好像可以用來解決這個問題!

2009 年 5 月的 Microsoft TechNet Magazine 有一篇「A Guide to Customizing the Office 2007 Ribbon」,很適合拿來當做 reference,對 dev 來說可以當做 Office 2007 的檔案格式的入門文章。對於要做 Office Automation 的人來說也很實用,如果要深入底層追問題的話。

關鍵在於 Office 2007 的檔案格式:Office Open XML(MSDN、Wiki),這是一個 zip 格式,可以想像成一份 Office 2007 Document 就跟一份 HTML Document 一樣,是由文字、圖片、影片等等的資料兜出來的,這些資料都是各自獨立的檔案,最後透過 Office 把這些檔案合併為 .docx/.xslx/.pptx 等格式的單一檔案。Office 2007 Document 也是用 Markup Language 來描述的,並不是 binary 編碼的格式。

(所以在 Office System 裡面應當也有類似 Web Browser 的 Layout Engine 之類的東西)

那麼到底要如何一次取出 word 檔內的所有圖片呢?請按照以下操作步驟:
  1. 若是由 Word 2003 建立的 .doc 檔,請先用 Word 2007 打開。(若是 .docx 格式請直接跳到步驟 3)
  2. 將 .doc 檔案另存為 .docx 格式。(這時可能會跳出一些警告訊息說格式可能會跑掉,請直接忽略它,圖片並不會因此被扭曲變形)
  3. 將附檔名從 .docx 改為 .zip。
  4. 解壓縮這個 zip 檔,word 檔中所有的圖片會位於 word\media 資料夾中。
就是這麼簡單,雖然這種需求可以說十年才遇到一次,但是要是不知道這種接近 hack 的技巧,處理起來還真是很頭大呢!(我猜可能網路上可以找到現成的小工具來做這件事,但是我懶惰沒有去找 XD 如果這個小工具還要安裝的話,感覺也不太保險…)

PS. A Guide to Customizing the Office 2007 Ribbon 中有提到:
After you've edited the contents of the package, you need to change the file extension back from .zip to its original extension. But you don't have to keep changing the extension to .zip each time you want to edit the underlying package. Instead, you can use a utility that will recognize your Office Open XML Format document as a ZIP package without ever changing the extension in the first place. There are at least a few of these. One open source option that I like is 7-zip. After you install it, just right-click your Office Open XML Format document, point to 7-zip, and then click Open Archive. You can even edit XML document parts directly in the package, and the archive utility will prompt you to update the package after you save your changes.


我只能說 7-zip 真是太優秀啦!又免費阿!有裝 7-zip 的話就不用把副檔名改成 zip 了,直接就可以把 .docx/.xslx/.pptx 內的檔案解壓縮出來到你指定的資料夾囉!

另外,A Guide to Customizing the Office 2007 Ribbon 裡面的 xml sample code 是有問題的,不能用「<Ribbon></Ribbon>」,要全部用小寫:「<ribbon></ribbon>」

2009年4月4日

網頁視窗全螢幕化

之前坎尼也有遇過這個問題
客戶希望我們的地圖畫面能大一點
還能兼顧查詢出來的結果顯示
所以最後用了程式控制 browser 全螢幕

這個方法要運用的就是 dom 裡的 window.open
window.open(url, name, specs, replace)

對,重點就是 specs 裡所填的參數
一般來講,可以用 left & top 屬性來控制開啟視窗的初始位置
height & width 控制長寬
menubar、toolbar、statusbar 來控制 browser 上一些哩哩扣扣東西的開啟
而全螢幕則是利用 fullscreen=yes

在程式裡的寫法則是如下

點下 OpenWinFullScreen 之後,即會開啟全螢幕的新視窗
下圖由於要表現全螢幕,所以就抓了整個視窗的畫面 XD
可以看到,ToolBar、MenuBar、Status通通不見了
(當然也沒有關閉鍵,想關視窗請用 Alt+F4 )


另外剛剛坎尼在測試 window.open 方法時,又發現了一些有趣的東西
window.open要傳入的第二個參數 name,表示要開啟視窗名稱
若是輸入 _self 則是會在自身的視窗上作開啟

坎尼異想天開的把 name 指定為 _self 且 fullscreen=yes
你猜怎麼著??? 全螢幕的屬性就失效啦 :P

經測試,Specs裡所填的屬性在 name=_self 的情況下,通通沒效用
所以坎尼大膽推測 window.open(URL,"_self") == location.href=URL

其實還有測出蠻多有趣的東西
但目前坎尼用的環境是 win7 + IE8
所以很多還待驗證 (想用 winxp 或 win2k3 + IE 6 7 8 測)
這些留待未來再來驗證吧 XD

剛剛發現 windows 7 裡有個 snipping tool
可以用來框選畫面抓圖,算是還不錯用
(記得大學時都是print screen小畫家去修圖)

2009年3月25日

world community grid (WCG)– Help Fight Childhood Cancer

Just received the newsletter from WCG, which is about a new project – Help Fight Childhood Cancer. This project aims to find a therapy which helps to cure a severe childhood cancer – neuroblastoma.

Since I’m clearly no expert in cancer or the related research, I think it’s best to post the entire letter from the WCG team.The following is the fulltext of the newsletter:

You Can Help End Childhood Cancer
Alert to all members of World Community Grid - your donated computer cycle time may now help find a cure for childhood cancers! There is a group of cancers that are particularly loathsome because they normally only strike young children.

Neuroblastoma is one of these cancers, arising in children under the age of two and resulting in a less than 40% survival rate.

While scientists have uncovered the three proteins that enable this cancer to grow, they now need to search the three million drug candidates for a treatment. And your computer can help us complete this search in the next year.

If you are currently contributing and want to check to see if you are contributing to this project, click here. If you are no longer contributing but would like to contribute to this project, please click here.

The cause of neuroblastoma is unknown, but most physicians believe that it is an accidental cell growth that occurs during normal development of the sympathetic ganglia and adrenal glands. It occurs most often during the first two years of a child's life, and has a high risk for disease relapse with survival rates of less than 40 percent.

The rapid advancement of genetic research at Chiba Cancer Center Research Institute holds great promise for treating neuroblastoma. The new Help Fight Childhood Cancer project will use the idle computational power from your computer to identify which of the three million potential drug candidates can inhibit growth of three particular proteins believed to prevent successful treatment via conventional approaches, such as chemotherapy.

"Our promising research will be further advanced by the free computing power we will use from World Community Grid," said Dr. Akira Nakagawara, the principal investigator at the Chiba Cancer Center Research Institute. "It would take us about 100 years using our own computing resources to make progress, but with access to one of the world's largest virtual supercomputers, we estimate to complete this project in two years, and begin laboratory trials."

Dr. Nakagawara recently earned the Princess Takamatsu Cancer Research Fund Prize 2008 for his neuroblastoma research. In his work, he discovered that one protein, TrkB, is expressed at high levels in aggressive neuroblastomas and enhances the tumor cell's growth. World Community Grid will conduct complex chemistry simulations to determine which drug candidates bond to TrkB, as well as the proteins ALK and SCxx, so that those can be tested further in the laboratory. All results will be made available to the general scientific community to advance the field of cancer biology and drug discovery.

For more information about the Help Fight Childhood Cancer Project and other projects running on World Community Grid, please click here.

We sincerely appreciate your wonderful support!
The following is the fulltext of the project introduction:

Project Status and Findings:  
Information on the Help Fight Childhood Cancer project may be found on these pages, on the Chiba University Help Fight Childhood Cancer website (Japanese here) and on Chiba Cancer Center's Help Fight Childhood Cancer website (Japanese here). The latest status updates may also be found at this site. To discuss or ask questions about this project, please visit the Help Fight Childhood Cancer Forum.

Mission

The mission of the Help Fight Childhood Cancer project is to find drugs that can disable three particular proteins associated with neuroblastoma, one of the most frequently occurring solid tumors in children. Identifying these drugs could potentially make the disease much more curable when combined with chemotherapy treatment.

Significance

Neuroblastoma is one of the most common tumors occuring in early childhood and is the most common cause of death in children with solid cancer tumors. If this project is successful, it could dramatically increase the cure rate for neuroblastoma, providing the breakthrough for this disease that has eluded scientists thus far.
 
Approach

Proteins (molecules which are a bound collection of atoms) are the building blocks of all life processes. They also play an important role in the progress of diseases such as cancer.

Scientists have identified three particular proteins involved with neuroblastoma, which if disabled, could make the disease much more curable by conventional methods such as chemotherapy. This project is performing virtual chemistry experiments between these proteins and each of the three million drug candidates that scientists believe could potentially block the proteins involved. A computer program called AutoDock will test if the shape of the protein and shape of each drug candidate fit together and bond in a suitable way to disable the protein.

This work consists of 9 million virtual chemistry experiments, each of which would take hours to perform on a single computer, totaling over 8,000 years of computer time. World Community Grid is performing these computations in parallel and is thus speeding up the effort dramatically. The project is expected to be completed in two years or less.
In an effort to spread the words about WCG, I will include a link to my last post about WCG – here it is: WCG-The Clean Energy Project

WCG Program Status Update:


Global
Total Members: 434,685
Total Devices: 1,212,255
Total Run Time: 227,282 years

Taiwan
Total Members: 2,370
Total Devices: 11,861
Total Run Time: 2,554 years
Me: please see the bottom of the page :)

2009年3月23日

DataSource 好好玩

今天坎尼突然發現 WebControl 中常見的 DataSource 屬性是 object
於是想說除了 DataView 以外,是否還支援其他類型?

首先來看一下 DataSource 始祖 BaseDataBoundControl.DataSource 定義
「設定 DataSource 屬性後,會呼叫 ValidateDataSource 方法。此外,如果資料繫結控制項已初始化,則會呼叫 OnDataPropertyChanged 方法,將 RequiresDataBinding 屬性設定為 true。」

似乎沒講到 object 可支援的類型
再來看一下上述的句子中所提到的 ValidateDataSource 定義
「要驗證的物件。通常是 IEnumerable、IListSource、IDataSource 或 IHierarchicalDatasource 的執行個體。」

這邊很明確的指出 DataSource 應該會支援以上四種泛型資料

ok~實驗開始

先在畫面上放個擁有 DataSource 屬性的 WebControl (這邊是放ListBox)
再放上兩個觸發事件的 Button

第一個 Button 事件,寫下呼叫 System.IO.Directory.GetFiles 方法
製造一個 String Array,再將 Array 傳給 ListBox1.DataSource
第二個 Button 事件則是傳入一個字串給 ListBox1

DataSource 來源為 String Array,恩~可以做 DataBind

但當來源為字串時,非但沒有出錯
還將 hello world 切成 array來顯示!!! (據測試,應為 CharArray )

看來 .NET 會自動的將字串轉為 CharArray 再進行 DataBind 的動作
坎尼再用 Gridview 來看一下繫結的欄位是啥~ 疑~ Item?

可是當坎尼自己建立個欄位,並將 DataField 設為 Item 時
此時反倒是出現錯誤:「找不到欄位 Item」(還真是令人搞不懂

不過坎尼此時也發覺到,雖然 DataSource 有這些特性
平常 Coding 用到的機率少之又少,於是就此打住

會用到的作法大概就是列出清單之類的 (如範例中的檔案清單

Google Spreadsheet 裡用規則運算式

最近因為工作關係,遇到要用 Google Form 及 Google Sheet 所以研究了 Google Sheet 裡的一些 function 怎麼用 首先,分享一下如何在 Google Sheet 裡用規則運算 :D