Blogs with tag [Python]
Google Wave Workshop - Day 1
ในที่สุด bot ก็คุยกับผมได้แล้ว ใส่คำสั่งภาษา python ลงไปให้มันทำงานได้ด้วย

code ที่ใช้
# -*- encoding: utf-8 -*-
from waveapi import events
from waveapi import model
from waveapi import robot
from waveapi import document
import logging
def OnParticipantsChanged(properties, context):
"""Invoked when any participants have been added/removed."""
added = properties['participantsAdded']
for p in added:
Notify(properties, context)
def OnRobotAdded(properties, context):
"""Invoked when the robot has been added."""
root_wavelet = context.GetRootWavelet()
root_wavelet.CreateBlip().GetDocument().SetText("Wazzup!")
def Notify(properties, context):
root_wavelet = context.GetRootWavelet()
root_wavelet.CreateBlip().GetDocument().SetText("Yo Man!")
def on_blip_submitted(properties, context):
blip = context.GetBlipById(properties['blipId'])
contents = blip.GetDocument().GetText()
if 'mr.fusion' in contents.lower():
man_img = document.Image('http://www.pantip.com/cafe/toy/image/man.gif')
new_blip = blip.CreateChild()
new_blip.GetDocument().AppendElement(man_img)
new_blip.GetDocument().AppendText(u'...เข้ามาดู')
else:
try:
pyobj = eval(contents)
except Exception, e:
pyobj = u'%s' % contents
new_blip = blip.CreateChild()
new_blip.GetDocument().SetText(unicode(pyobj))
if __name__ == '__main__':
myRobot = robot.Robot('chaiwat-suttipongsakul',
image_url='http://cwt.bashell.com/files/img/cwt.png',
version='9',
profile_url='http://cwt.bashell.com/')
myRobot.RegisterHandler(events.WAVELET_PARTICIPANTS_CHANGED, OnParticipantsChanged)
myRobot.RegisterHandler(events.WAVELET_SELF_ADDED, OnRobotAdded)
myRobot.RegisterHandler(events.BLIP_SUBMITTED, on_blip_submitted)
myRobot.Run()
2 comment(s) 18/7/2009 23:11
ทดสอบ libthai บน Mac OS X
พอดีลอง compile libdatrie ตัวใหม่ที่คุณเทพแก้เรื่องการ compile บน Mac ก็เลยลองใช้ python เปิด libthai.dylib ที่ compile กับ libdatrie ตัวนี้แล้วทดสอบตัดคำเทียบกับการใช้ PyICU ดูครับ
Input:
เพนแทกซ์คือแป้นแตก แม้ดูแปลกมีที่มา
ยอดเลนส์ทาคูมาร์ พัฒนาเป็นเมาท์เค
กล้องฟิล์มสีสวยสด ดังสีหยดสาดรดเท
ดิจิตอลดีไซน์เท่ แป้นแสนเก๋ตัวเล็กดี
บางภาพเราโอเวอร์ บ้างอันเดอร์ไม่พอดี
หื่นฮาเป็นพิธี ฟุ้งวจีมิตรภาพอวล
น้าป้าหลายหลากรุ่น ทั้งวัยรุ่นและจอมกวน
แตกต่างกันแต่ล้วน จุดมุ่งหมายถ่ายภาพเอย
libthai(th_brk_line) :
|เพ|นแทกซ์|คือ|แป้น|แตก |แม้|ดู|แปลก|มี|ที่|มา
|ยอด|เลนส์|ทา|คู|มา|ร์ |พัฒนา|เป็น|เมาท์|เค
|กล้อง|ฟิล์ม|สี|สวย|สด |ดัง|สี|หยด|สาด|รด|เท
|ดิจิตอล|ดีไซน์|เท่ |แป้น|แสน|เก๋|ตัว|เล็ก|ดี
|บาง|ภาพ|เรา|โอเวอร์ |บ้าง|อัน|เด|อร์|ไม่|พอดี
|หื่น|ฮา|เป็น|พิธี |ฟุ้ง|วจี|มิตรภาพ|อวล
|น้า|ป้า|หลายหลาก|รุ่น |ทั้ง|วัยรุ่น|และ|จอม|กวน
|แตก|ต่าง|กัน|แต่|ล้วน |จุด|มุ่งหมาย|ถ่าย|ภาพ|เอย
icu :
|เพนแทกซ์|คือ|แป้น|แตก| |แม้|ดู|แปลก|มี|ที่มา|
|ยอด|เลนส์|ทา|คู|มาร์| |พัฒนา|เป็น|เมา|ท์เค|
|กล้อง|ฟิล์ม|สี|สวย|สด| |ดัง|สี|หยด|สาด|รด|เท|
|ดิจิตอล|ดีไซน์|เท่| |แป้น|แสน|เก๋|ตัว|เล็ก|ดี|
|บาง|ภาพ|เรา|โอเวอร์| |บ้าง|อันเดอร์|ไม่|พอดี|
|หื่น|ฮา|เป็น|พิธี| |ฟุ้ง|วจี|มิตรภาพ|อวล|
|น้า|ป้า|หลาย|หลาก|รุ่น| |ทั้ง|วัย|รุ่น|และ|จอม|กวน|
|แตก|ต่าง|กัน|แต่|ล้วน| |จุด|มุ่ง|หมาย|ถ่าย|ภาพ|เอย
icu(line) :
|เพนแทกซ์|คือ|แป้น|แตก |แม้|ดู|แปลก|มี|ที่มา
|ยอด|เลนส์|ทา|คู|มาร์ |พัฒนา|เป็น|เมา|ท์เค
|กล้อง|ฟิล์ม|สี|สวย|สด |ดัง|สี|หยด|สาด|รด|เท
|ดิจิตอล|ดีไซน์|เท่ |แป้น|แสน|เก๋|ตัว|เล็ก|ดี
|บาง|ภาพ|เรา|โอเวอร์ |บ้าง|อันเดอร์|ไม่|พอดี
|หื่น|ฮา|เป็น|พิธี |ฟุ้ง|วจี|มิตรภาพ|อวล
|น้า|ป้า|หลาย|หลาก|รุ่น |ทั้ง|วัย|รุ่น|และ|จอม|กวน
|แตก|ต่าง|กัน|แต่|ล้วน |จุด|มุ่ง|หมาย|ถ่าย|ภาพ|เอย
icu(character) :
|เพ|น|แท|ก|ซ์|คื|อ|แป้|น|แต|ก| |แม้|ดู|แป|ล|ก|มี|ที่|มา|
|ย|อ|ด|เล|น|ส์|ทา|คู|มา|ร์| |พั|ฒ|นา|เป็|น|เมา|ท์|เค|
|ก|ล้|อ|ง|ฟิ|ล์|ม|สี|ส|ว|ย|ส|ด| |ดั|ง|สี|ห|ย|ด|สา|ด|ร|ด|เท|
|ดิ|จิ|ต|อ|ล|ดี|ไซ|น์|เท่| |แป้|น|แส|น|เก๋|ตั|ว|เล็|ก|ดี|
|บา|ง|ภา|พ|เรา|โอ|เว|อ|ร์| |บ้า|ง|อั|น|เด|อ|ร์|ไม่|พ|อ|ดี|
|หื่|น|ฮา|เป็|น|พิ|ธี| |ฟุ้|ง|ว|จี|มิ|ต|ร|ภา|พ|อ|ว|ล|
|น้า|ป้า|ห|ลา|ย|ห|ลา|ก|รุ่|น| |ทั้|ง|วั|ย|รุ่|น|และ|จ|อ|ม|ก|ว|น|
|แต|ก|ต่า|ง|กั|น|แต่|ล้|ว|น| |จุ|ด|มุ่|ง|ห|มา|ย|ถ่า|ย|ภา|พ|เอ|ย
0 comment(s) 15/4/2009 0:28
diff list by set part 2
จากที่เมื่อวานใช้ set มาช่วยในการ diff list แล้วปรากฏว่ามันจะมีปัญหากับสมาชิกของ list ที่เป็น dict เลยจัดการแปลงสมาชิกทุกตัวให้เป็น string representative ก่อน แล้วค่อยแปลงกลับด้วย eval (ใช้ร่วมกับ list comprehensions เพื่อทำให้ code กระชับ)
def list_diff(list_a, list_b):
set_a = set([item.__repr__() for item in list_a])
set_b = set([item.__repr__() for item in list_b])
set_int = set_a.intersection(set_b)
return {'---':[eval(item) for item in list(set_a - set_int)],
'===':[eval(item) for item in list(set_int)],
'+++':[eval(item) for item in list(set_b - set_int)]}
เมื่อใช้งานจริงจะได้ดังนี้
In [2]: a = [{'x':123},'Hello World', 789]
In [3]: b = ['Hello World', 456, {'y':567}]
In [4]: list_diff(a,b)
Out[4]: {'+++': [{'y': 567}, 456], '---': [{'x': 123}, 789], '===': ['Hello World']}
1 comment(s) 26/2/2009 22:19
diff list by set
ใช้ความรู้เรื่อง set ที่เคยเรียนสมัยมัธยม
def list_diff(list_a, list_b):
set_a = set(list_a)
set_b = set(list_b)
set_int = set_a.intersection(set_b)
gone_items = set_a - set_int
new_items = set_b - set_int
return {'---':list(gone_items),
'+++':list(new_items)}
ถ้าเอาแบบ advance หน่อย
def list_diff(list_a, list_b):
set_a = set(list_a)
set_b = set(list_b)
set_int = set_a.intersection(set_b)
return {'---':list(set_a - set_int),
'===':list(set_int),
'+++':list(set_b - set_int)}
1 comment(s) 25/2/2009 21:13
