2008年5月13日火曜日

Calendar

calendar あるいは dateutil http://labix.org/python-dateutil
もあるけれども、 range を使うのがわかりやすいか。



y1 = 2007
y2 = 2008
yy = range(y1,y2) # (2001,2002 ..2008)
mm = range(1,13) # (1,2 ... 12 )
print yy, mm

for y in yy:
for m in mm:
m = str(m)
if len(m) == 1:
m = '0' + m
ym = str(y) + '-' + str(m)
print ym # 2008-01,2008-02

import datetime

today = date.today()
print today # 2008-05-13

t = datetime.timedelta(weeks=2)
print t

import calendar
print calendar.setfirstweekday(calendar.SUNDAY)
print calendar.calendar(2008)

[2007] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
2007-01
2007-02
2007-03
2007-04
2007-05
2007-06
2007-07
2007-08
2007-09
2007-10
2007-11
2007-12
2008-05-13
14 days, 0:00:00
None
2008

January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 1 2 1
6 7 8 9 10 11 12 3 4 5 6 7 8 9 2 3 4 5 6 7 8
13 14 15 16 17 18 19 10 11 12 13 14 15 16 9 10 11 12 13 14 15
20 21 22 23 24 25 26 17 18 19 20 21 22 23 16 17 18 19 20 21 22
27 28 29 30 31 24 25 26 27 28 29 23 24 25 26 27 28 29
30 31

April May June
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 1 2 3 1 2 3 4 5 6 7
6 7 8 9 10 11 12 4 5 6 7 8 9 10 8 9 10 11 12 13 14
13 14 15 16 17 18 19 11 12 13 14 15 16 17 15 16 17 18 19 20 21
20 21 22 23 24 25 26 18 19 20 21 22 23 24 22 23 24 25 26 27 28
27 28 29 30 25 26 27 28 29 30 31 29 30

July August September
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 1 2 1 2 3 4 5 6
6 7 8 9 10 11 12 3 4 5 6 7 8 9 7 8 9 10 11 12 13
13 14 15 16 17 18 19 10 11 12 13 14 15 16 14 15 16 17 18 19 20
20 21 22 23 24 25 26 17 18 19 20 21 22 23 21 22 23 24 25 26 27
27 28 29 30 31 24 25 26 27 28 29 30 28 29 30
31

October November December
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 1 1 2 3 4 5 6
5 6 7 8 9 10 11 2 3 4 5 6 7 8 7 8 9 10 11 12 13
12 13 14 15 16 17 18 9 10 11 12 13 14 15 14 15 16 17 18 19 20
19 20 21 22 23 24 25 16 17 18 19 20 21 22 21 22 23 24 25 26 27
26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
30

2008年5月12日月曜日

urlfetch で rss.xml より img を検出





http://localhost:8080/_ah/admin/interactive より実行


from google.appengine.api import urlfetch
from elementtree.ElementTree import *
import re

rss1 = 'http://movies.go.com/xml/rss/intheaters.xml'
url = rss1
rss = urlfetch.Fetch(url)
print rss
tree = ElementTree(fromstring(rss.content))

img_re = re.compile('<img src="([a-zA-Z0-9/:._\-]+)" ')
for item in tree.findall('.//item'):
title = item.find('title').text.strip()
description = item.find('description').text.strip()
imageMatch = img_re.search(description)
if (imageMatch):
picture_url = imageMatch.groups()[0]
print title
print picture_url

---

Speed Racer
http://movies.go.com/i/movies/895881/895881.jpg



--- 特定のページの src = .jpg 検出 ---

from google.appengine.api import urlfetch
from elementtree.ElementTree import *
import re
url = 'http://webdba.blogspot.com/2008/05/urlfetch-rss-img.html'
content = urlfetch.Fetch(url).content
img_re = re.compile('<img src="([a-zA-Z0-9/:._\-]+)" ',re.I)
img_re = re.compile('src="([a-zA-Z0-9/:._\-]+).jpg" ',re.I)
img_re = re.compile('src="([a-zA-Z0-9/:._\-]+[jpg|gif|png])" ',re.I)

imageMatch = img_re.search(content)
imageMatch = img_re.findall(content)
print imageMatch
for i in imageMatch:
print i

---

['http://code.google.com/appengine/images/appengine_lowres.jpg']
http://code.google.com/appengine/images/appengine_lowres.jpg

2008年5月9日金曜日

正規表現, 文字列の置換



文字列の置換 string.replace()


---
import string
str1 = "http://localhost"
str2 = string.replace(str1, 'http', 'file')
print str1
print str2
---
http://localhost
file://localhost
-----------------------------------

リテラルのバックスラッシュにマッチさせるには、正規表現文字列として
'\\\\'


Python では raw 文字列(raw string)表記を正規表現に利用
先頭に  r  を付加する
p = re.compile(r'([^"])\b((http|https)://[^ \t\n\r<>\(\)&"]+' \
r'[^ \t\n\r<>\(\)&"\.])')


http://www.python.jp/Zope/articles/tips/regex_howto/regex_howto_3
http://reddog.s35.xrea.com/wiki/Python%C8%F7%CB%BA%CF%BF.html Python備忘録


改行文字を取り除くときは、 s[:-1] または s.rstrip('\n')

-------------------------------------------------------------
import re
p = re.compile("ab.", re.I)
result = p.findall("AbdABCAAbb")
print result

p = re.compile("ab.")
print p.sub("xxx", "abcdeaabcde")
print p.sub("xxx", "Abcdeaabcde")

p = re.compile("ab.", re.I)
print p.sub("xxx", "Abcdeaabcde")
print "------------"

url = "aa https://aa.com/ bb"

p = re.compile(r'([^"])\b((http|https)://[^ \t\n\r<>\(\)&"]+' \
r'[^ \t\n\r<>\(\)&"\.])')

m = p.match('aa http://aa.com/ bb' + url + url )
print m
m = p.sub('XXX','aa http://aa.com/ bb' + url + url )
print m
print "------------"

# print m.group(0)

print "-- " + url + " --"
print "->"
print p.sub("xxx", url )

p = re.compile('[a-z]+')
m = p.match('tempo')
print m
print m.group(0)

-----

from google.appengine.api import urlfetch
from elementtree.ElementTree import *
import re

url = 'http://blog.goo.ne.jp/xxxxxx/m/200803'
#content = urlfetch.Fetch(url).content.decode("euc-jp")
content = url.split('/')

print content[0]
print content[1]
print content[2]
print content[3]

content = url.split('/')[2]
print content

google.appengine.api.datastore


cccwiki サンプルではPage オブジェクトを作成している
 
 この内容は Datastore Viewerでも参照することができる。
























 その基本動作の確認

c:/Program Files/Google/google_appengine/google/appengine/api/datastore.py

import os
from google.appengine.api import datastore
from google.appengine.api import datastore_types

name = 'test'
entity = datastore.Entity('Page')
entity['name'] = name

query = datastore.Query('Page')
query['name ='] = name
entities = query.Get(1)
print >> sys.stdout, len(entities)
print >> sys.stdout, entities[0]
print >> sys.stdout, entities[0]['user']
print >> sys.stdout, entities[0].key()
print >> sys.stdout, entities[0].key().id()
------
1
{u'content': u'<h1>test</h1>', u'user': users.User(email='test@example.com'), u'modified': datetime.datetime(2008, 5, 9, 14, 41, 30, 62000), u'name': u'test', u'created': datetime.datetime(2008, 5, 9, 14, 41, 30, 62000)}
test@example.com
agpoZWxsb3dvcmxkcgsLEgRQYWdlGJQBDA
148

DatePropertyにはやはり不具合(bug)あり



date = db.DateTimeProperty(auto_now_add=True) の値が
対象の datastore のデータを更新する度に、時刻が進んでいってしまう現象は
以下の Patch により直った。 ( SDK 1.0.1)

DateProperty does not work properly in dev environment
http://code.google.com/p/googleappengine/issues/detail?id=131

c:/Program Files/Google/google_appengine/google/appengine/api/datastore_types.py

I tried a patch to module datastore_types.py, which seems to work:

change line 1033
from
lambda val: datetime.datetime.fromtimestamp(float(val) / 1000000.0),
to
lambda val: datetime.datetime.utcfromtimestamp(float(val) / 1000000.0),

Per the Python doc, fromtimestamp() converts to the local timezone.

2008年5月2日金曜日

Key names and IDs cannot be used like property values


   key と key_name と ID があり、これを理解していないいけない。
   
   GQL で key は select できるが、 key_name や ID は select できない。
   ・ select してきた key から key_name や ID を知ることはできる。
            ・ 検索条件に ID や key_name を指定することができる。
            ・ 検索条件に key を指定することは GQL ではできない。

注意: datastore には2つある   
from google.appengine.ext import db       これが GQL  api の解説あり
from google.appengine.api import datastore これは cccwiki のサンプルなどで使用されている


key_name 指定して put すると













ID は空欄で、Key_Name に値が保存される。






import sys #import sys module for printing to stdout
from google.appengine.ext import db

class Car(db.Model):
brand = db.StringProperty(required=True)
wheels = db.ListProperty(db.Key)

mercedes = Car(brand="Mercedes", key_name ="b")
# mercedes.put()
print >> sys.stdout, mercedes.brand

select = mercedes.get_by_id( 130 )
print >> sys.stdout, select.brand

select = mercedes.get_by_key_name( "b" )
print >> sys.stdout, select.brand

id = 130
select = Car.get(db.Key.from_path('Car', id))
print >> sys.stdout, select.brand

key_name = "b"
select = Car.get(db.Key.from_path('Car', key_name))
print >> sys.stdout, select.brand

print >> sys.stdout, select.key()
print >> sys.stdout, select.key().id()
print >> sys.stdout, select.key().name()



select * from Greeting where key = "xxxx"
select * from Greeting where id = xxx
のような検索はできない。

select user_name from Greeting where id = xxxx
update Greeting set user_name = 'new name' where id = xxxx
のかわりに
greetings = db.GqlQuery("SELECT * FROM Greeting")

id = greeting.key().id()

id = int(self.request.get('id'))
greeting = Greeting.get(db.Key.from_path('Greeting', id))

row id から再度検索する必要なない。
get(db.Key.from_path('Greeting', id) これが再検索と同じ

key さえわかれれば db.get() で OK
from google.appengine.ext import db
r = db.get("agpoZWxsb3dvcmxkcgsLEgRCbG9nGNQBDA")
print r.title



Tip: Key names and IDs cannot be used like property values in queries. However, you can use a named key, then store the name as a property. You could do something similar with numeric IDs by storing the object to assign the ID, getting the ID value using obj.key().id(), setting the property with the ID, then storing the object again.

http://code.google.com/appengine/docs/datastore/keysandentitygroups.html#Kinds_Names_and_IDs

http://code.google.com/appengine/articles/djangoforms.html

追加仕様検討
Issue 178: Add unique=True/False to db.property constructor to ensure uniqueness
http://code.google.com/p/googleappengine/issues/detail?id=178

Trunsaction / run_in_transaction
http://code.google.com/appengine/docs/datastore/functions.html#run_in_transaction

2008年5月1日木曜日

Google App Engine / ワイルドカード検索



 部分一致検索はできないようだが、擬似的に行うことができる。
ただ、これも unicode がからむと少々注意が必要

下記を参考に helloworld.py にてテストしたが、
 #!-*- coding:utf-8 -*- がきいているためか、

 OK
greetings = db.GqlQuery("SELECT * FROM Greeting where content >= :1 and content < :2 ",
search_key, search_key + u"\uFFFD" )
 NG
# greetings = db.GqlQuery("SELECT * FROM Greeting where content >= :1 and content < :2 ", # search_key, urllib.unquote(search_key).decode("utf8") + u"\uFFFD" )

でした。 http://code.google.com/appengine/docs/datastore/queriesandindexes.html
Tip: Query filters do not have an explicit way to match just part of a string value, but you can fake a prefix match using inequality filters:

db.GqlQuery("SELECT * FROM MyModel WHERE prop >= :1 AND prop < :2", "abc", "abc" + "\xEF\xBF\xBD")

This matches every MyModel entity with a string property prop that begins with the characters abc. The byte string "\xEF\xBF\xBD" represents the largest possible Unicode character. When the property values are sorted in an index, the values that fall in this range are all of the values that begin with the given prefix.

http://groups.google.com/group/google-appengine/browse_thread/thread/47abf4b8842ab865?hl=en

Next problem is that I can't search the datastore with a parameter
from the query string, something like this:

import urllib

q = "Ambj%C3%B6" # Url encoded u"Ambjö"

fsgs = db.GqlQuery("SELECT * FROM Fsg WHERE fsg >= :1 AND fsg < :2", urllib.unquote(q), urllib.unquote(q) + u'\uFFFD')

Works perfect if I only have ascii characters. Is urllib.unquote wrong
utility for this?

> q = "Ambj%C3%B6" # Url encoded u"Ambjö"
> fsgs = db.GqlQuery("SELECT * FROM Fsg WHERE fsg >= :1 AND fsg < :2", > urllib.unquote(q), urllib.unquote(q) + u'\uFFFD')


You've got a similar problem as before. The string urllib.unquote()
returns isn't a Unicode string either, so you need to convert it to
Unicode. Eg:
urllib.unquote(q).decode("utf8") + u"\uFFFD"

Unityでドアの開閉はAnimatorそれともiTween?

Mac Mini M2 の Unity で Sketchup のデータを復元したつづき。 以前、苦労して作成したドアの開閉が動作しないので修復する。 どうやって動かしていたのか、また忘れそうなので記録しておく。             Animator 左右のドア PlaneL,...