2010年12月10日金曜日

self.response.out.writeで数値を出力時の罠

self.response.out.writeの引数に数値の0を指定すると、何も出力されないので注意しましょう。はまりました。

for i in range(0,6):
self.response.out.write(i)
>> 12345

for i in range(0,6):
self.response.out.write(str(i))
>> 012345

セッション処理でエラー発生中

ブラウザによっては問題なく動作しています。あとで原因調査します。

Traceback (most recent call last):
File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/__init__.py", line 515, in __call__
handler.get(*groups)
File "/base/data/home/apps/rapidproxy/1.346766960722169101/main.py", line 34, in get
session = appengine_utilities.sessions.Session()
File "/base/data/home/apps/rapidproxy/1.346766960722169101/appengine_utilities/sessions.py", line 562, in __init__
self.session = _AppEngineUtilities_Session.get_session(self)
File "/base/data/home/apps/rapidproxy/1.346766960722169101/appengine_utilities/sessions.py", line 142, in get_session
ds_session = db.get(str(session_key))
File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/db/__init__.py", line 1318, in get
keys, multiple = datastore.NormalizeAndTypeCheckKeys(keys)
File "/base/python_runtime/python_lib/versions/1/google/appengine/api/datastore.py", line 144, in NormalizeAndTypeCheckKeys
keys = [_GetCompleteKeyOrError(key) for key in keys]
File "/base/python_runtime/python_lib/versions/1/google/appengine/api/datastore.py", line 2150, in _GetCompleteKeyOrError
key = Key(arg)
File "/base/python_runtime/python_lib/versions/1/google/appengine/api/datastore_types.py", line 304, in __init__
raise datastore_errors.BadKeyError('Invalid string key %s.' % encoded)
BadKeyError: Invalid string key agpyYXBpZHByb3h5ciQLEhtfQXBwRW5naW5lVXRpbGl0aWVzX1Nl********.

Pythonのバージョンを調べる

C:\Python25>python -V
Python 2.5.4

2010年12月9日木曜日

dir / オブジェクトが持っている要素を調べる

オブジェクトにどのようなメソッド(関数)があるのか調べる。


>>> dir("hoge")
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> dir(123)
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__str__', '__sub__', '__truediv__', '__xor__']

コマンドラインからGAEにデプロイ

GUIのインターフェースからだとメールアドレスとパスワードを毎回入力する必要があり面倒。コマンドラインからなら、アカウント情報の入力は一度でよい。

appcfg.py update myapp

GAEに頻繁にデプロイする必要がある場合はコマンドライン推奨。

len / tuple, list, 文字列などの長さを取得

>>> len("hoge")
4
>>> len([1,2,3,4,5])
5
>>> len((1,2,3,4,5,6))
6

2010年10月16日土曜日

更新再開します。

しばらくPython勉強してなかったが、自宅のPCも整備できたので、勉強再開します。