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
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) こんな感じで出来ました。
変数の型 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’))。 […]
外部ファイル、モジュール、ライブラリー | 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 […]
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 = […]
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/ で表示されました。
Hello, world! | Google App Engine
5月 3rd, 2011
http://code.google.com/intl/ja/appengine/docs/python/gettingstarted/helloworld.html こちらは、このままなので特に 以下のようにディレクトリ作成。 C:\apps\helloworld その中に 簡単なリクエスト ハンドラの作成 helloworld.py print ‘Content-Type: text/plain’ print ” print ‘Hello, world!’ 設定ファイルの作成 app.yaml application: helloworld version: 1 runtime: python api_version: 1 handlers: – url: /.* script: helloworld.py の2ファイルを作成。 アプリケーションのテスト コマンドプロンプトから、ここだけ、pathの設定をしていないので、全部フルパスで書くとこんな感じです。 c:\>”Program Files\Python27\python.exe” “C:\Program Files\Google\google_appengine\dev_appserver.py” C:\apps\helloworld INFO 2011-05-03 07:20:57,961 appengine_rpc.py:158] Server: appengine.google.com INFO 2011-05-03 07:20:57,970 appcfg.py:437] Checking for updates to the […]
開発環境 | Google App Engine
5月 3rd, 2011
App Engine Python の開発環境設定 http://code.google.com/intl/ja/appengine/docs/python/gettingstarted/devenvironment.html windows python 2.5 install http://www.python.org/ 2.5.5もありますが、2.7.1で検証していきます。 Downloadsより Python 2.7.1 Windows Installer (Windows binary — does not include source) をダウンロードしてインストール インストールされた IDLE ( Python GUI ) Python ( command line ) のどちらかを実行 print ‘Hello! World.’ Google App Engine SDK install http://code.google.com/intl/ja/appengine/downloads.html Google App Engine SDK for Python -> GoogleAppEngine-1.4.3.msi をダウンロードしてインストール