- リンク先の django-ajax を django-ajax.html という名前で保存する
- Polymer 内で core-ajax を使っていた部分を django-ajax に置き換える
- インポートを忘れずに
- {% csrf_token %} を html に含めてレンダリングする
先人の知恵というのはありがたいものですね。リンク先に感謝です。
from django.views.decorators.csrf import csrf_exempt .... url(r'^category$', csrf_exempt(MyViewClass.as_view())),
fetched_list = MyModel.query().fetch() json_without_keyid = json.dumps([c.to_dict() for c in fetched_list ]) json_with_keyid = json.dumps([dict(c.to_dict(), **dict(id=c.key.id())) for c in fetched_list ])
from datetime import date
class JSONDateEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, date):
return obj.isoformat()
return json.JSONEncoder.default(self, obj)
fetched_list = MyModel.query().fetch()
json_without_keyid = json.dumps([c.to_dict() for c in fetched_list ], cls=JSONDateEncoder)
json_with_keyid = json.dumps([dict(c.to_dict(), **dict(id=c.key.id())) for c in fetched_list ], cls=JSONDateEncoder)
{
"shell_cmd": "python -u \"$file\"",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"windows":
{
"encoding": "cp932", // Windowsコンソールの文字コード指定
"path": "C:/Python27" // 自分のPythonへのパスを追記
}
}
│ app.yaml
│ cron.yaml
│ favicon.ico
│ index.yaml
│ main.py
│ twitter.py
│
├─oauth2
│ │ _version.py
│ │ _version.pyc
│ │ __init__.py
│ │ __init__.pyc
│ │
│ ├─clients
│ │ imap.py
│ │ smtp.py
│ │ __init__.py
│ │
│ └─httplib2
│ cacerts.txt
│ iri2uri.py
│ iri2uri.pyc
│ socks.py
│ socks.pyc
│ __init__.py
│ __init__.pyc
│
└─simplejson
│ compat.py
│ decoder.py
│ encoder.py
│ ordered_dict.py
│ scanner.py
│ tool.py
│ _speedups.c
│ __init__.py
Google App Engineでは、スタティックなファイルを作成等はできない。python-twitterはキャッシュとして、ファイルを作るために、そのままでは動作しない。APIオブジェクトを作成する際にcache=Noneをキーワードとしてつけておくこと。です。なので、twitter.Api オブジェクトを作成するときは
api = twitter.Api(consumerKey, consumerSecret, accessToken, accessSecret, cache=None)cron: - description: job url: / schedule: every 2 hoursあとは呟く内容を変える仕組みを作れば、自分専用の BOT としては形になりそうです。
# 0, 1, 2, 3, 4, 5, ..., 498, 499 range (0, 500) # 499, 498, 497, ..., 3, 2, 1, 0 range (499, -1, -1)
from xml.etree.ElementTree import parse
# 読み込み
mapping = {}
tree = parse('books.xml')
for B in tree.findall('book'):
isbn = B.attrib['isbn']
for T in B.findall('title'):
mapping[isbn] = T.text
pprint.pprint(mapping)
# 書き込み。文字コードを指定し xml_declaration=True にすると、
# 先頭に宣言() が入る
tree.write('out.xml', encoding="utf-8", xml_declaration=True)
import xml.sax, xml.sax.handler, pprint
class BookHandler(xml.sax.handler.ContentHandler):
def __init__(self):
self.inTitle = False
self.mapping = {}
def startElement(self, name, attributes):
if name == 'book':
self.buffer = ""
self.isbn = attributes["isbn"]
elif name == "title":
self.inTitle = True
def characters(self, data):
if self.inTitle:
self.buffer += data
def endElement(self, name):
if name == "title":
self.inTitle = False
self.mapping[self.isbn] = self.buffer
parser = xml.sax.make_parser()
handler = BookHandler()
parser.setContentHandler(handler)
parser.parse('books.xml')
pprint.pprint(handler.mapping)
print handler.mapping
import sqlite3
conn = sqlite3.connect('sq.db')
cursor = conn.cursor()
cursor.execute('select * from sqlite_master WHERE type="table"')
for item in cursor.fetchall():
print item
import sqlite3
conn = sqlite3.connect('sq.db')
cursor = conn.cursor()
cursor.execute('drop table people')
create_table_command = 'create table people (name char(30), age int(4))'
cursor.execute(create_table_command)
cursor.execute('insert into people values (?, ?)', ('Aoki',50))
cursor.executemany('insert into people values (?, ?)', [('Ishikawa', 70), ('Ueno', 40)])
conn.commit()
cursor.execute('select * from people')
print cursor.fetchall()
cursor.execute('select * from people where age >= 50')
print cursor.fetchall()
cursor.execute('update people set age=? where name = ?', (51, 'Aoki'))
cursor.execute('select * from people where age >= 50')
print cursor.fetchall()
import shelve
dbase = shelve.open('database')
dbase['1'] = ['a', 'b', 'c']
dbase['2'] = 2
for key in dbase:
print dbase[key]
# shelv.open で writeback=True を指定しないと
# これでは dbase['1'] の中身は変更されない。
dbase['1'].append('d')
dbase['2'] = 'Two'
for key in dbase:
print dbase[key]
# dbase['1'] の中身を変更する。
tmp = dbase['1']
tmp.append('d')
dbase['1'] = tmp
for key in dbase:
print dbase[key]
# データの削除
del dbase['1']
dbase.close()
import re
filetext = open('list_inserter.hpp').read()
pattern = re.compile('#include\s+<.*$', re.MULTILINE)
print re.findall(pattern, filetext)