Sean's Note

2014年7月16日 星期三

[Python] 跟檔案有關的常用函式

os.path.join('c' 'folder') # 'c\\folder'
os.path.join('c:' 'folder') # 'c:folder'

# Get file name.
os.path.basename('C:\\Windows\\123.txt') # '123.txt'

# Get file path and file extension.
filePath, fileExtension = os.path.splitext('C:\\Windows\\123.txt') # 'C:\\Windows\\123', '.txt'

# Get directory path.
os.path.dirname('C:\\Windows\\123.txt') # 'C:\\Windows'

# Get modify time from epoch time.
os.path.getmtime('C:') # 1409646173.7839031

# Traverse a directory.
for root, subdirs, files in os.walk("C:\\Pictures"):
  print root, subdirs, files 

2014年7月4日 星期五

[Python] How to get the last day of the month

想要知道某個特定月份有幾天,可以直接呼叫 calendar.monthrange(2014, 2),

會回傳第一天是禮拜幾和該月有幾天。

範例:
import calendar
calendar.monthrange(2014, 2)
# (5, 28) # 禮拜六,28天
calendar.monthrange(2014, 3)
# (5, 31) # 禮拜六,31天
calendar.monthrange(2014, 4)
# (1, 30) # 禮拜二,30天

2014年5月13日 星期二

Can's see the newly created picture in Android's default gallery.

當我們寫了一個會儲存圖片在裝置上的 APP 時,卻在開啟 Android gallery 或其他專門
瀏覽圖片的 APP 時看不到圖片,是因為系統還沒將其掃進 MediaStore,想必如果系統
不斷的掃描檔案和操作資料庫是相當耗資源與沒有效率的。所以,Android 會在每次
開機時做一次掃描,如果 APP 想要系統對某個檔案做即時的掃描,可以加上下面的

程式碼:
MediaScannerConnection.scanFile(activity, new String[]{path}, null, 
    new MediaScannerConnection.OnScanCompletedListener() {
        @Override
        public void onScanCompleted(final String path, final Uri uri) {
           ...
        }
    }
); 

Ref: http://developer.android.com/reference/android/media/MediaScannerConnection.html

2014年5月10日 星期六

Launch Mode for Activity

Activity 的 launch mode 可以透過 android:launchMode 來設定,總共有四種 launch mode:
  1. standard
    這是 default 的 launch mode,Activity 每次都會建立一個新的實體。
    EX1: A -> B(standard),在 B 啟動 B,A -> B -> B。
    EX2: A(standard) -> B,返回上層,Activity A 會被重新建立。
  2. singleTop
    如果當下的 Activity 在 stack 的最上面,則不會建立新的實體,反之則會。
    EX1: A(singleTop) -> B -> C,在 C 啟動 A,A -> B -> C -> A。
    EX2: A(singleTop) -> B,返回上層,Activity A 不會被重新建立。
  3. singleTask
    不會建立新的實體,而且總是在 stack 中的最前頭(下層)。
    (Google 說總是在 stack 中的最前頭(下層) 也不全然正確)
    EX1: A -> B(singleTask) -> C -> D ,在 D 啟動 B,A -> B,C 和 D 會被移除。
  4. singleInstance
    不會建立新的實體,而且是 stack 中唯一的 Activity。
    EX1: A -> B -> C ,在 C 啟動 D(singleInstance),[A -> B -> C] -> [D]。

2014年5月1日 星期四

Android 4.4 behavior changes for read/write external storage

在 Android 4.4 之後,應用程式不再可以任意的寫入 SD 卡:
我寫了一個間單的應用程式 (com.example.test) 跑在 HP 平板(Android 4.4) 上做測試。
呼叫新的 API getExternalFilesDirs(null) 去要在 SD 卡上的私有儲存資料夾,
得到了路徑: /storage/sdcard1/Android/data/com.example.test/files
在這個資料夾下做了一些操作,發現有以下的行為:
  1. 我的 test app 可以對該資料夾寫入跟讀取。 
  2. 該資料夾下的檔案可以被電腦端或其它應用程式如 “ES File Explorer” 瀏覽到。
  3. “ES File Explorer” 不能移除該資料夾下的檔案。 (無法被其它應用程式修改)
  4. 電腦端可以移除該資料夾下的檔案。 (可以被電腦端修改)
  5. 當移除我的應用程式後,該資料夾也會被移除。
Read More: https://developer.android.com/about/versions/android-4.4.html#Behaviors

2014年4月29日 星期二

How to get place and wheather information by latitude and longitude

How to get place information?


我們可以用 Facebook API 和 Foursquare API 來取得附近地點的資訊。

這裡主要介紹怎麼使用 Foursquare API,Foursquare 是一間提供社群服務的網站公司,

其所提供的 API目前是屬於免費的。

部分的版權說明:

Access to the API is currently provided for free, but Foursquare reserves the right to charge for access to the API in the future, at its sole discretion. If we do charge a fee for using the API or any feature thereof, you do not have any obligation to continue to use the API or the applicable feature.”

詳細的版權說明可參考: https://foursquare.com/legal/api/platformpolicy

使用步驟如下:
  1. 先到 Foursquare API 註冊一個帳號。
  2. 登入後,點選 My Apps -> CREATE A NEW APP,建立並填寫 app 的相關資訊。
  3. 建立完成後,會得到一組 Client IDClient Secret
  4. 接著我們只要送出一個 HTTPS request:
    https://api.foursquare.com/v2/venues/search?client_id=CLIENT_ID&client_secret=CLIENT_SECRET&v=20140430&ll=24.983163,121.536084
  5. 傳回的 response 就是附近地點啦!
    以上面的 request 為例,第一個回傳的地點就是 “XXX 燒臘小館”,
    回傳的資訊還包含了該地點的 phone, address, latitude, longitude, distance(36m), category name(Asian Restaurant) 等。
Note: Foursquare API 在中國可使用。

How to get weather information?


我們可以用 Weather Underground 所提供的 Weather API 來取得天氣資訊。

Weather Underground 是一間提供全球氣象資訊的網站公司,

其所提供的 API 根據流量來決定費用。

部分的版權說明:

“(a.)KEY; MONITORING. There is a daily limit and minute rate limit to the number of requests that can be made to the API for free. You may exceed this threshold only if you are or become a fee paying subscriber. WUL will monitor your daily usage of the API to determine if you have exceeded the free-use threshold by using an application programming interface key (the “Key”). Refer to the “Key Settings” page for product pricing, package features, and rate limit levels.”

詳細的版權說明可參考: http://www.wunderground.com/weather/api/d/terms.html

使用步驟如下:
  1. 先到 Weather API 註冊一個帳號。
  2. 登入後,點選 Key Settings,先選擇服務方案,再填寫 app 的相關資訊。
  3. 建立完成後,會得到一組 Key ID
  4. 接著我們只要送出一個 HTTP request:
    http://api.wunderground.com/api/69f520a20f42ecd9/conditions/settings/lang:TW/q/24.983163,121.536084.json
    (69f520a20f42ecd9 為官網提供的測試 ID 可直接使用)
  5. 傳回的 response 就是詳細的天氣資訊啦!
    以上面的 request 為例,會回傳微雨(weather)、21.4(temp_c)、東北偏東(wind_dir)、
    85%(relative_humidity) 等資訊。


2014年4月24日 星期四

Capturing HTTP traffic with Fiddler

在使用 RESTful API 諸如 Facebook API、Google API 或 Foursquare API
等等開發程式時,有時候會想要攔截 HTTP request 和 response 的值來
除錯,而這時候就可以使用 Fidder 來查看。
需要設定的步驟如下:
  1. 開起手機端的 Wifi 與 PC 端連上同樣的網域。
  2. 將手機端 Wifi 的 "Proxy 主機名稱" 設成 PC 端的 IP;port 設 8888。
  3. 如果要攔截 HTTPS request,需要在手機上安裝憑證。安裝的方法,
    只要開啟手機上任意的瀏覽器,在網址列輸入 PC 端的 IP 位置,如圖1
    所示,點選 FiddlerRoot certificate 下載憑證後,即可攔截 HTTPS request。
         
圖1. 手機上的 Chrome