Archive for the ‘Python’ Category

 

datastore low level っぽく pythonを使ってみる|google app engine

5月 10th, 2011

まずは、 .get_by_key_name()を調べてみました。 google\appengine\ext\db\__init__.py keys = [datastore.Key.from_path(cls.kind(), name, parent=parent) for name in key_names] if multiple: return get(keys, config=config) else: return get(keys[0], config=config) こうなっていたので Key.from_pathから kindと nameにkey_nameにしていたものを入れればOKと key = Key.from_path(‘myitem’, ‘my_test’) entity = Get(key) みたいな。 Putして Getしてみる。 Blob , datetime データはどうなる? entity = Entity(“bbtune”,name=”test2″) bdata = Blob(zlib.compress(‘tttttttttttttt’)) tdata = datetime.datetime.fromtimestamp(float(130000000)) entity.update({ ‘p': 100, ‘bdata': bdata, ‘tdata': tdata […]

Read full article | コメントは受け付けていません。

quota : cpuの使用量をチェック | google app engine

5月 9th, 2011

cpuは課金してみようと思います。 CPUの利用量がわかるらしいのでやってみます。 quota.pyとして置いてみました。 関数の前後で数字を取得して、差分をチェックしている例。 http://code.google.com/intl/ja/appengine/docs/quotas.html#Per-minute_Quotas import logging from google.appengine.api import quota start = quota.get_request_cpu_usage() do_something_expensive() end = quota.get_request_cpu_usage() logging.info(“do_something_expensive() cost %d megacycles.” % (start – end))

Read full article | コメントは受け付けていません。

datastore putの速度 100件put | google app engine

5月 8th, 2011

datastore の putが遅いです。 でも複数のアイテムを同時に入れてもあまり変わらない?らしいので確認。 http://code.google.com/intl/ja/appengine/docs/python/datastore/overview.html にある通り500エンティティはput出来るようなのでついでに500も ( 0.3 sec は、ほとんどネットワークの問題です。) 1つのputに2.3秒かかります。 100個のputは4.3秒でした^-^。 500個のputは7.9秒でした^-^。 model詳細は適当に消したので気にしないでリストに100個入れてdb.put()するだけです。 model_list = [] for key in self.list: item = MyModel(key_name=self._generateKey( key )) item.en = datetime.datetime.fromtimestamp(float(self.list[key]['e'])) item.setData(self.list[key]['t'],self.list[key]['u'],self.list[key]['i'],self.list[key]['d']) item.pr = self.list[key]['p'] item.bi = self.list[key]['b'] model_list.append(item) db.put(model_list) 1件のときの結果 2.3605380058289 object(stdClass)#1 (2) { ["success"]=> int(1) ["error"]=> array(0) { } } 100件の時の結果 4.3123650550842 object(stdClass)#1 (2) { […]

Read full article | コメントは受け付けていません。

datastore putの速度 property name | google app engine

5月 8th, 2011

http://www.mail-archive.com/google-appengine@googlegroups.com/msg25095.html モデルのクラス名、プロパティ名は、短いほうがputの処理速度が速いそうです。 とりあえず、プロパティー名は、nameを付けることで、短く指定してみました。 price = db.IntegerProperty(name=’p’) CPU利用時間も変わるので、いちおモデル名も短めにしておきます。

Read full article | コメントは受け付けていません。

google app engine の速度

5月 8th, 2011

簡単な速度を。 さくらのVPSからabの結果です。 結果 300ms-500ms というところでした。 悪くはないですね。 あたり前ですが、さくらのネットワーク内のコンテンツとは比較にならないので。。。 やっぱり自前のAPIはさくら内に持つかフロントをすべてapp engineへ移行するかの2択のようです。 数段階のアクセスは無理なので。現状は大規模データだけ任せて細かい所は現状と同じく memcached +mysql , tokyotyrantで行くしかなさそうです。 1. 通常テキストページ # ab -n 100 -c 20 http://gr—–.appspot.com/ Server Software: Google Server Hostname: gr—–.appspot.com Server Port: 80 Document Path: / Document Length: 398 bytes Concurrency Level: 20 Time taken for tests: 1.746154 seconds Complete requests: 100 Failed requests: 0 Write […]

Read full article | コメントは受け付けていません。

UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 2-8: ordinal not in range(128) | Google App Engine

5月 7th, 2011

UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 2-8: ordinal not in range(128) なんぞこれ。 import sys print sys.getdefaultencoding() ascii だそうで、これが駄目なようで。。。 通常のpythonは、 sitecustomize.py に import sys sys.setdefaultencoding(‘utf-8′) を追加すればいいそうです。 app engineでは、それを各場所が無いので。直接読み込まれるファイルに書かないといけないそうです。 app.yamlに設定してあるファイルに書く必要があるそうでデフォルト main.pyでしょうか。 main.pyの先頭に import sys if ‘ascii’==sys.getdefaultencoding(): stdin = sys.stdin stdout = sys.stdout reload(sys) sys.setdefaultencoding(‘utf-8′) sys.stdin = stdin sys.stdout = stdout と書いてみました。 reload(sys)が無いとだめで。stdin stdoutのデータ保管も初回は無いとだめな感じで。 ローカルの場合は、1ファイルだけ更新とかあるので、何度も読み込まれないけど、本番環境は、更新時だけかな? とりあえず。

Read full article | コメントは受け付けていません。

for の使い方 python

5月 7th, 2011

Cやphpのようなforを使いたいとき。 あるシーケンスにわたってインデクスで反復を行うには、 range() と len() を次のように組み合わせます: >>> a = ['Mary', 'had', 'a', 'little', 'lamb'] >>> for i in range(len(a)): … print i, a[i] … 0 Mary 1 had 2 a 3 little 4 lamb

Read full article | コメントは受け付けていません。

jsonを使う。python | Google App Engine

5月 7th, 2011

2.6からは、 import json で利用できるそうですが、 app engineは 2.5なので from django.utils import simplejson # encode a = simplejson.dumps({‘message':u”TEST”,’site':u”BBTUNE”}) # {“message”: “TEST”, “site”: “BBTUNE”} # decode b = simplejson.loads(a) こんな感じで出来ました。

Read full article | コメントは受け付けていません。

変数の型 type を知る python

5月 7th, 2011

typesのtypeで。一致は以下のように typesで定義されているものを利用! import types num = 1 print type(num) print (type(num) is IntType) <type ‘int’> True でした。 一覧はこちら NoneType Noneの型です。 TypeType typeオブジェクトの型です (type()などによって返 されます)。 BooleanType boolのTrueとFalseの型です。これは組み込み関数の bool()のエイリアスです。 IntType 整数の型です(e.g. 1)。 LongType 長整数の型です(e.g. 1L)。 FloatType 浮動小数点数の型です(e.g. 1.0)。 ComplexType 複素数の型です(e.g. 1.0j)。 Pythonが複素数のサポートなしでコンパイルされていた場合には 定義されません。 StringType 文字列の型です(e.g. ‘Spam’)。 UnicodeType Unicode文字列の型です(e.g. u’Spam’)。 Pythonがユニコードのサポートなしでコンパイルされていた場合には 定義されません。 TupleType タプルの型です(e.g. (1, 2, 3, ‘Spam’))。 […]

Read full article | コメントは受け付けていません。

空のクラス pass | python

5月 6th, 2011

空のクラスを作るとき class ClassA: pass とpassを使う。 if test==0: # nothing to do pass elif test==1: # nothing to do pass else: # nothing to do pass 何もしないときは pass

Read full article | コメントは受け付けていません。