最近用HTMLParser的時候發生了因為EOF(EOF in middle of construct)而產生exception的問題
後來將網頁儲存後發現是因為網頁內容不符合規範所造成的
範例:
< img //這裡不是用空白而是用換行(\n '0A')
src="" />
因此將內容中的換行全部replace成空白得以解決
看來以後要避免這種鳥問題還是先做這些前處理比較好...
2008年9月13日星期六
2008年7月29日星期二
TSQL 筆記 - 利用 SQL Server 2005 的 row_number() 選出在資料庫中指定資料的分頁
前幾天研究室學長問的問題,嘗試了一下,可以用 SQL Server 2005 的 row_number() 函數來解決
範例問題:
我們要知道 Table 中指定資料所在的分頁
範例 Table [t1]:
id data
1 'CCN'
2 'icools'
3 'GDX'
4 'jpst'
目標:
取得 data 欄位的 'GDX' 所在列數 (row)
解決方法:
DECLARE @id int
SET @id = (SELECT [id] FROM [t1] WHERE [data] = 'GDX')
SELECT count(*) / 3 + 1 FROM [test] WHERE id < @id
目標(2):
取得 data 欄位的 'GDX' 所在列數 (用 data 欄位做排序)
解決方法:
set @id = (
SELECT TOP 1 [rid]
FROM (
SELECT row_number() over (order by [data] ASC) as [rid], [data]
FROM [t1]
) AS b
WHERE b.[data] = 'GDX'
)
SELECT count / 3 + 1
FROM (
SELECT row_number() over (order by [data] ASC) as rid
FROM [t1]) AS b
WHERE b.rid < @id
範例問題:
我們要知道 Table 中指定資料所在的分頁
範例 Table [t1]:
id data
1 'CCN'
2 'icools'
3 'GDX'
4 'jpst'
目標:
取得 data 欄位的 'GDX' 所在列數 (row)
解決方法:
DECLARE @id int
SET @id = (SELECT [id] FROM [t1] WHERE [data] = 'GDX')
SELECT count(*) / 3 + 1 FROM [test] WHERE id < @id
目標(2):
取得 data 欄位的 'GDX' 所在列數 (用 data 欄位做排序)
解決方法:
set @id = (
SELECT TOP 1 [rid]
FROM (
SELECT row_number() over (order by [data] ASC) as [rid], [data]
FROM [t1]
) AS b
WHERE b.[data] = 'GDX'
)
SELECT count / 3 + 1
FROM (
SELECT row_number() over (order by [data] ASC) as rid
FROM [t1]) AS b
WHERE b.rid < @id
2008年7月16日星期三
Python 學習筆記 - 自訂模組路徑
在windows中直接設定環境變數PYTHONPATH即可
在Linux中必須修改~/.profile,加入一行 export PYTHONPATH=/path/to/module
在Linux中必須修改~/.profile,加入一行 export PYTHONPATH=/path/to/module
2008年6月23日星期一
Ubuntu 學習筆記 - sudo 指令後面的參數不能用 tab auto complete
前幾天發生這怪怪的問題
以前輸入 sudo apt-g 之後按 tab 應該會自動完成 sudo apt-get
或是輸入 sudo apt- 案兩次 tab 後會顯示可輸入指令
不過好像某次安全性更新後這功能就失效了,若不加上 sudo 指令是可以正常 work 的
後來 google 的結果找到了解決方法
用文字編輯器開啟 /etc/bash.bashrc 檔案
然後把以下內容去註解或是加入
# enable bash completion in interactive shells
if [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
這樣就可以正常 work 囉^_^
以前輸入 sudo apt-g 之後按 tab 應該會自動完成 sudo apt-get
或是輸入 sudo apt- 案兩次 tab 後會顯示可輸入指令
不過好像某次安全性更新後這功能就失效了,若不加上 sudo 指令是可以正常 work 的
後來 google 的結果找到了解決方法
用文字編輯器開啟 /etc/bash.bashrc 檔案
然後把以下內容去註解或是加入
# enable bash completion in interactive shells
if [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
這樣就可以正常 work 囉^_^
2008年6月16日星期一
Google App Engine - urlfetch
在 GAE 中不能使用 Python Standard Library 的任何有用到 Socket 的模組
像是 urllib、httplib 等都是被禁止的
因此若有需要存取其他網頁,則必須使用 GAE 提供的 urlfetch
範例:
from google.appengine.api import urlfetch
r = urlfetch.fetch('http://www.google.com/', method="GET")
if r.status_code == 200:
print result.content
在 GAE 的 urlfetch 介紹頁提到
urlfetch 僅能對 port 80 (http) 和 port 443 (https) 進行存取
timeout 也不能設定
而 Http Redirect 也以最大五次為限
而在實際的測試中,也發現 urlfetch 似乎有內定的 cooldown
不能在短時間內發出大量 urlfetch,否則會出現 DownloadError Exception
這對某些類型的網站並不是很方便 :(
不知道付費版本會不會有這些限制 :P
參考資料:Urlfetch Overview
像是 urllib、httplib 等都是被禁止的
因此若有需要存取其他網頁,則必須使用 GAE 提供的 urlfetch
範例:
from google.appengine.api import urlfetch
r = urlfetch.fetch('http://www.google.com/', method="GET")
if r.status_code == 200:
print result.content
在 GAE 的 urlfetch 介紹頁提到
urlfetch 僅能對 port 80 (http) 和 port 443 (https) 進行存取
timeout 也不能設定
而 Http Redirect 也以最大五次為限
而在實際的測試中,也發現 urlfetch 似乎有內定的 cooldown
不能在短時間內發出大量 urlfetch,否則會出現 DownloadError Exception
這對某些類型的網站並不是很方便 :(
不知道付費版本會不會有這些限制 :P
參考資料:Urlfetch Overview
Google App Engine - 上傳靜態檔案
在一個 App Engine 專案裡面所有要上傳的檔案都必須在 app.yaml 檔案中設定
如果我們要上傳 css 和 js 資料夾,則必須在 app.yaml 加入以下的敘述:
handlers:
- url: /css
static_dir: css
- url: /js
static_dir: js
如果我們要上傳 css 和 js 資料夾,則必須在 app.yaml 加入以下的敘述:
handlers:
- url: /css
static_dir: css
- url: /js
static_dir: js
2008年6月15日星期日
Python 學習筆記 - copy
copy 這個模組可以讓你複製 Python 的物件
範例:
import copy
a = copy.copy(x)
b = copy.deepcopy(x)
copy 和 deepcopy 不同之處在於 copy 儘會複製當前物件
而 deepcopy 則是若該物件有屬性指向其他物件,則也會一併複製
範例:
class test:
def __init__(self, data):
self.data = data
l = [1, 2, 3, 4]
t = test(l)
import copy
a = copy.copy(t)
b = copy.deepcopy(t)
l.append(5)
print a.data
print b.data
將會輸出:
[1, 2, 3, 4, 5]
[1, 2, 3, 4]
範例:
import copy
a = copy.copy(x)
b = copy.deepcopy(x)
copy 和 deepcopy 不同之處在於 copy 儘會複製當前物件
而 deepcopy 則是若該物件有屬性指向其他物件,則也會一併複製
範例:
class test:
def __init__(self, data):
self.data = data
l = [1, 2, 3, 4]
t = test(l)
import copy
a = copy.copy(t)
b = copy.deepcopy(t)
l.append(5)
print a.data
print b.data
將會輸出:
[1, 2, 3, 4, 5]
[1, 2, 3, 4]
訂閱:
文章 (Atom)