Archive for 5月, 2011

 

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 | コメントは受け付けていません。

文字列の圧縮・解凍 gzencode gzdecode gzcompress gzuncompress gzdeflate gzinflate

5月 5th, 2011

http://www.php.net/manual/ja/ref.zlib.php どれを使ったらよいのでしょうか? gzencode — gzip 圧縮された文字列を作成する gzdecode — gzip 圧縮された文字列をデコードする gzdeflate() – deflate圧縮する gzinflate() – deflate圧縮された文字列を解凍する gzcompress() – 文字列を圧縮する gzuncompress() – 圧縮された文字列を解凍する いろいろ条件はあるようですが。 gzencodeでとりあえず。

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

bookmarklet で ajax してみる。

5月 5th, 2011

jsonpですかね。 とりあえず、jquery使わないで、ajaxしてみたら chromeとfirefoxは OPTIONSリクエストが発生しました。 レスポンスはちゃんと受け取れないようでした。 リクエストヘッダ Host bbtune.com User-Agent Mozilla/5.0 (Windows; U; Windows NT 6.1; ja; rv:1.9.2.17) Gecko/20110420 Firefox/3.6.17 ( .NET CLR 3.5.30729; .NET4.0C) Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language ja,en-us;q=0.7,en;q=0.3 Accept-Encoding gzip,deflate Accept-Charset Shift_JIS,utf-8;q=0.7,*;q=0.7 Keep-Alive 115 Connection keep-alive Origin http://mixi.jp Access-Control-Request-Me… POST apache log [05/May/2011:02:07:36 +0900] “OPTIONS /ajaxtest HTTP/1.1″ 200 2487 “http://mixi.jp” “Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.30 […]

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

外部ファイル、モジュール、ライブラリー | Google App Engine

5月 4th, 2011

モデルをとりあえず外部ファイルにしようと思い。 ディレクトリ ‘Bbtune’ を作成 Bbtune以下に Models.pyを作成 大事なのが, Bbtune 以下に __init__.py ファイルを作成すること http://code.google.com/intl/ja/appengine/docs/python/runtime.html#Pure_Python サブディレクトリーに__init__.pyの作成を忘れるな!と。 Bbtune/Models.py import datetime from google.appengine.ext import db class Item(db.Model): title = db.StringProperty() data = db.StringProperty() enddate = db.DateTimeProperty() helloworld.py(まだhello)に from Bbtune.Models import * item1 = Item( title=’test’,data=’test’,enddate=datetime.datetime(year=2011, month=1, day=20, hour=21, minute=34, second=56, microsecond=0) ) item1.put() key = item1.key() item2 = db.get(key) print […]

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

getenv | import os python

5月 4th, 2011

環境変数はこんな感じで取得できました。 import os print os.getenv(‘REMOTE_ADDR’) print os.getenv(‘HTTP_USER_AGENT’) print os.environ['REMOTE_ADDR'] これでIP制限します。

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

key,key_nameを指定してエンティティ作成、取得 datastore | google app engine

5月 4th, 2011

さっそく、やりたいことを先に片付けようと keyによるエンティティ作成と取得をやってみました。 pointは、key_nameと get_by_key_nameでしょうか。 import cgi from google.appengine.api import users from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app from google.appengine.ext import db class Ticket(db.Model): title = db.StringProperty(multiline=False); date = db.DateTimeProperty(auto_now_add=True) class MainPage(webapp.RequestHandler): def get(self): self.response.out.write(‘tick1′) tick1 = Ticket(title=’amuro1′) tick1.put() key = tick1.key() result = db.get(key) self.response.out.write(key) self.response.out.write(”) self.response.out.write(result.title) self.response.out.write(‘tick2′) tick2 = Ticket(key_name=’keytest’,title=’amuro2′) tick2.put() key2 = […]

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

datastore の使用 | Google App Engine

5月 4th, 2011

http://code.google.com/intl/ja/appengine/docs/python/gettingstarted/usingdatastore.html ちょっととばして、サンプルのとおり helloworld.py import cgi from google.appengine.api import users from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app from google.appengine.ext import db class Greeting(db.Model): author = db.UserProperty() content = db.StringProperty(multiline=True) date = db.DateTimeProperty(auto_now_add=True) class MainPage(webapp.RequestHandler): def get(self): self.response.out.write(‘<html><body>’) greetings = db.GqlQuery(“SELECT * FROM Greeting ORDER BY date DESC LIMIT 10″) for greeting in greetings: if greeting.author: […]

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

webappフレームワーク | google app engine

5月 4th, 2011

http://code.google.com/intl/ja/appengine/docs/python/gettingstarted/usingwebapp.html Djangoとかも使えるようですがとりあえずwebappで例のとおり helloworld.pyを書き換えます。 from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app class MainPage(webapp.RequestHandler): def get(self): self.response.headers['Content-Type'] = ‘text/plain’ self.response.out.write(‘Hello, webapp bbtune World!’) application = webapp.WSGIApplication( [('/', MainPage)], debug=True) def main(): run_wsgi_app(application) if __name__ == “__main__”: main() http://localhost:8080/ で表示されました。

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