2011年4月12日 星期二

計算list(串列)數量

list.count(x)

計算得串列中x的數量

len(list)
取得串列中的元素的數量

參考來源:易記學

2011年1月19日 星期三

GAE Blob最大檔限制

當我們使用GAE(BlobProperty class)的DB存取圖片或檔案時,須注意單筆資料1MB的上限!!

2011年1月13日 星期四

使用remote_api直接調用遠端的db model

#!/usr/bin/env python
#
import os
import sys

# Hardwire in appengine modules to PYTHONPATH
# or use wrapper to do it more elegantly
appengine_dirs = ['/Applications/blah/blah/google_appengine'...]
sys.path.extend(appengine_dirs)
# Add your models to path
my_root_dir = os.path.abspath(os.path.dirname(__file__))
sys.path.insert(0, my_root_dir)

from google.appengine.ext import db
from google.appengine.ext.remote_api import remote_api_stub
import getpass

from models.mystuff import Foo

APP_NAME = 'my-app'
os.environ['AUTH_DOMAIN'] = 'gmail.com'
os.environ['USER_EMAIL'] = 'me@gmail.com'

def auth_func():
 return (raw_input('Username:'), getpass.getpass('Password:'))

# Use local dev server by passing in as parameter:
# servername='localhost:8080'
# Otherwise, remote_api assumes you are targeting APP_NAME.appspot.com
remote_api_stub.ConfigureRemoteDatastore(APP_NAME,
 '/remote_api', auth_func)

# Do stuff like your code was running on App Engine

foos = Foo.all().fetch(100)
for foo in foos:
 foo.note = 'Hello World!'
db.puts(foos)
由ConfigureRemoteDatastore換掉所操作的model,由ConfigureRemoteDatastore
對model定義,code中沒看到定義 Foo

2010年7月22日 星期四

Python的UUID產出辦法

python的uuid模組有四種方式可以產生uuid

  1. 使用硬體及時間
  2. 以MD5對字串取值
  3. 隨機產生
  4. 使用SHA-1


以上分別對應的方式為:


#-*- encoding: utf-8 -*-
import uuid

print uuid.uuid1()
print uuid.uuid3(uuid.NAMESPACE_DNS, 'testname')
print uuid.uuid4()
print uuid.uuid5(uuid.NAMESPACE_DNS, 'testme')

2010年7月17日 星期六

在寫GAE時,如果上傳檔案出現錯誤

TypeError: Blob() argument should be str instance, not unicode

可參考
http://code.google.com/p/googleappengine/issues/detail?id=1784

其中提到

要將From送出的資料改成
enctype="multipart/form-data"(多部分表單張貼)
 
格式才會正常,不然圖片會以UTF8送,發生錯誤。

2010年4月29日 星期四

Python標準輸入的使用方法

import sys
data = sys.stdin.readlines()
print "Counted", len(data), "lines."
 
 
要匯入sys物件,可以開始使用 

2009年5月24日 星期日

Python在台灣的好用連接

蒐集了一些Python的好用連接,以及可以提供學習Python的途徑

名稱:Python3.0.1 for win下載點
網站:http://www.ziddu.com/download/5071693/python-3.0.1.msi.html
簡介:Python 3.0 於 2009年2月13日發行了,歡迎大家去下載來玩,這裡提供的是在Win32平台
上的版本,如果有需要Unix-Like的版本,可以至官方網站下載。

名稱:電腦做什麼事
網站:http://pydoing.blogspot.com/
簡介:這是一個程式語言Python教學的網誌。Python,讓寫程式更簡單。


名稱:Python 台灣使用者群組
網站:http://beta.python.org.tw/
簡介:PyTUG (Python Taiwan User Group);Python 台灣使用者群組,是台灣一群 Python 程式語言的愛好者。這裡是 PotWiki,由 PyTUG 所建立與維護的 wiki 網站。

pot 是 python.org.tw domain 的縮寫,所以 PyTUG 拿 pot 當作 PyTUG 在網路上的代稱,也用來稱呼所有 PyTUG 在 python.org.tw domain 下提供的服務。這便是本網站叫作 PotWiki 的原因。本組織 PyTUG 的口號 (目前 beta 中,歡迎到 Freenode/#python.tw 或 python.tw googlegroup 來討論) 是:

*「載入 Python,少作多玩!」

名稱:拜Python教之Django光明會支部
網站:http://timchen119.blogspot.com/
簡介:"拜Python教" 是一個低耦合高熱血密度的小型地下傳教組織. 專門研究並推廣Python崇拜
"Django光明會支部" 是一實驗性分享Python, Django及自由軟體心得的中文部落格