2011年6月25日 星期六

Python 對應PHP function 的方式(nl2br、explode、implode)

def nl2br(string, is_xhtml= True ):
    if is_xhtml is True :
        return string.replace('\n','
\n')
    else :
        return string.replace('\n','
\n')

實現explode
>>> str = 'a|b|c|d|e'
>>> str.split("|")
['a', 'b', 'c', 'd', 'e']

實現implode
>>> list = ['a', 'b', 'c', 'd', 'e']
>>> "|".join(list)
'a|b|c|d|e'

GAE is not multi-line

在GAE中如果有多行文字儲存,有\n換行符號
GAE會報錯(is not multi-line)


請對model 加上參數 db.StringProperty(multiline=True)

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