python

Today announcement for #birdnest, 2 new features roll out, callback parameter support and stripping out the username at the front of tweet.

Callback parameter

Requested by @iake right after #barcampbangkok2.

When supply Twitter API with a parameter called “callback”, Twitter sends back the JSON result enclosed in a function you specified with callback parameter. It’s useful for creating javascript widget but #birdnest cannot handle these result and doesn’t filter the unnecessary information out. The new #birdnest can recognize this callback function and can filter the unwanted data out.

Stripping out the username at the front of tweet

Request by @bact.

Why put @username in front of every reply tweets in reply timeline especially those who have a very long name. Let #birdnest strips it out for you. Don’t worry #birdnest doesn’t touch your name when it is on you friends timeline or public timeline.

JSON-Only Features

These feature are available only on JSON and I don’t think there are many XML format user. If you are, please tell me. I’ll try to make it for you. What? Can’t wait? You can do it yourself and send the patch to the project homepage.

Wanna try?

Just point your Twitter gateway to http://nest2.onedd.net/. Notice the gateway name, it’s nest2.onedd.net, not nest.onedd.net. Because I want these features to be tested by all of you first.

As always, all suggestions and bug reports are welcome at the project Google Code page.

Happy Tweeting

หลังจากเราสร้าง Project ด้วยคำสั่ง paster กันไปแล้ว ทีนี้ก็มาถึงเรื่องของ dependencies แล้วล่ะ

ใน Pylons ปกติผมเห็นแต่ คน เค้า ใช้ SQLAlchemy กันทีนี้ก็คือถ้าจะลงก็ใช้ easy_install ได้

easy_install SQLAlchemy

ทดลองด้วยการเรียกใช้ Interactive Python Shell แล้วพิมพ์ตาม ต้องได้ผลตามนี้

>import sqlalchemy
>sqlalchemy.__version__
‘0.5.0beta3’

ทีนี้ก็ใช้งาน SQLAlchemy ได้แล้วล่ะ

แต่เดี๋ยวก่อน ถ้าเกิดเราไม่รู้ว่าต้องลงอะไรบ้างล่ะ เช่นตอนที่สร้าง project เราเลือกใช้ template อื่นนอกจาก Mako เช่น Jinja เราก็จะต้องมาลง Jinja ด้วย แต่ช้าก่อนถ้าเราลองสั่ง paster ดู จะได้ผลลัพธ์ดังนี้

Traceback (most recent call last):
File “/home/wien/bin/paster”, line 8, in
load_entry_point(‘PasteScript==1.6.3’, ‘console_scripts’, ‘paster’)()
File “/home/wien/py-lib/PasteScript-1.6.3-py2.5.egg/paste/script/command.py”, line 68, in run
commands = get_commands()
File “/home/wien/py-lib/PasteScript-1.6.3-py2.5.egg/paste/script/command.py”, line 110, in get_commands
plugins = pluginlib.resolve_plugins(plugins)
File “/home/wien/py-lib/PasteScript-1.6.3-py2.5.egg/paste/script/pluginlib.py”, line 81, in resolve_plugins
pkg_resources.require(plugin)
File “/usr/lib/python2.5/site-packages/pkg_resources.py”, line 626, in require
needed = self.resolve(parse_requirements(requirements))
File “/usr/lib/python2.5/site-packages/pkg_resources.py”, line 524, in resolve
raise DistributionNotFound(req) # XXX put more info here
pkg_resources.DistributionNotFound: Jinja: Not Found for: hello (did you run python setup.py develop?)

เอ๊ะ มันทะแม่งๆแฮะ มาบ่นว่า Jinja ไม่มีไม่พอ มีการท้าทายว่า ได้ลองเรียก python setup.py develop รึยัง ท้าแบบนี้มีหรือจะไม่ลอง

[wien@localhost]$ python setup.py develop
Processing dependencies for hello==0.1dev
Searching for Jinja
Reading http://www.pylonshq.com/download/
Reading http://pypi.python.org/simple/Jinja/
Reading http://wsgiarea.pocoo.org/jinja/
Reading http://jinja.pocoo.org/
Best match: Jinja 1.2
Downloading http://pypi.python.org/packages/source/J/Jinja/Jinja-1.2.tar.gz#md5=1235a005ade00b213800ff1e798c0241
Processing Jinja-1.2.tar.gz
Running Jinja-1.2/setup.py -q bdist_egg —dist-dir /tmp/easy_install-HQeuAp/Jinja-1.2/egg-dist-tmp-fgE5yD
Adding Jinja 1.2 to easy-install.pth file

อ่าวเฮ้ย มีท่านี้ด้วย เด็ดจริงๆ เลยเป็นความรู้ว่า ใช้ท่า python setup.py develop เพื่อลง dependencies ได้ด้วย ตรงนี้ยังไม่รู้ว่ามันจะเอามาใช้ตอน deploy ได้รึเปล่า แต่เดาว่ามันน่าจะใช้ได้ล่ะ ใครลองแล้ววานบอก

ด้วยรักและไพลอนส์66

รำลึกอดีตให้ฟังหน่อยนึงว่าหลังจากเข้าไปร่วมงาน NWA แล้วผมก็โดนเวทล้างสมองของ Sirn ให้หันมามอง Pylons เลยลองเล่นดูนิดหน่อย แต่ก็ไม่วายโดนรับน้องจนได้

ถ้าเราติดตั้ง Pylons ด้วย easy_install โดยไม่ระบุเวอร์ชัน ตัว Easy_install จะลง เป็นเวอร์ชัน 0.9.7rc1 ให้โดยอัตโนมัติ ซึ่งใน 0.9.7 นั่นมันจะมี Breaking Change อยู่จำนวนหนึ่ง แต่ในวิกิของ Pylons ยังไม่มีข้อมูลนี้

บังเอิญว่าผมดันไปเลือกใช้ 0.9.7 โดยไม่รู้อิโหน่อิเหน่ ค้นวิธีแก้ยังไงก็ไม่เจอ สถานการณ์แบบนี้ผมเรียกว่า ซวย ครับ

หนึ่งใน Breaking Change ที่โดนแน่ๆคือเรื่อง Webhelper แต่นั่นเป็นเรื่องที่เราจะไม่พูดกันครั้งนี้ เพราะครั้งนี้เราจะบอกถึง Breaking Change ที่ทำให้ผมเซ้งเซ็งอีกเรื่องแทน นั่นคือ Routing เป็นยังไงมาติดตามดู

เนื่องจากเป็นเด็กใหม่ เราก็ต้องทำตัวเป็นเด็กใหม่ที่ดีด้วยการศึกษาตาม Getting started ที่เค้าทำมาให้ หลังจากกำลังภายในของผมเริ่มหมดไปกับการถาม sirn และ vsatayamas ไปแล้ว ผมก็ฟันฝ่าตัว Getting Started มาจนถึงการทำ controller และ action แล้วล่ะ
จากใน Docs หลังจากสร้าง HelloController เสร็จแล้วก็ต้องก็สร้าง method index ขึ้นมา หลังจากทำขั้นตอนดังกล่าวผมก็เปิดด้วยความกระหยิ่มยิ้มย่อง เข้าหน้า http://localhost:5000/hello ตามสเต็ปเป๊ะๆๆๆ สิ่งที่โผล่ขึ้นมาต้อนรับเป็นข้อความอันคุ้นเคย

404 Not Found

ความคิดที่แว่บเข้ามาในหัว “นั่นไง โดนเข้าแล้ว”
เป็นที่ Routing ชัวร์ๆ อีแบบนี้ ก็เลยเปิดดูใน config/routes.py ก็มีบรรทัดแบบนี้อยู่แล้ว

map.connect(‘/{controller}/{action}’)
map.connect(‘/{controller}/{action}/{id}’)

อ่านคร่าวๆ หมายความว่าจะได้ Route URL ในลักษณะนี้

/{controller}/{action}/{id}
แล้วถ้าไม่ระบุ action ก็ต้อง route แบบนี้
/{controller}/{action}

แล้วผิดตรงไหน ลองไปลองมาก็เลยฉุกคิดได้นิดนึง เปลี่ยนไปเข้า http://localhost:5000/hello/index แทน ปรากฏว่าหรามาเลยครับ Hello world!!!

เลยตะหงิดๆ ขึ้นไปดูในคู่มือของ Routes ได้ความว่า ใน Routes รุ่นใหม่ที่มาพร้อมกับ Pylons 0.97 จะมีเรื่อง minimization เข้ามา คือจะไม่มีการ map url แบบครึ่งๆกลางๆแล้ว
ดังนั้นจากที่คิดว่า /{controller}/{action} จะได้ map ไปผูกกับ controller ที่ระบุแล้วกำหนด action เป็น index เองถ้าไม่กำหนด ก็หมดสิทธิทำแบบนี้แล้ว

อธิบายเป็นภาษาคนก็คือ ต้องระบุ action ทุกครั้งด้วยนะจ๊ะเด็กโง่

แต่เฮ้ย ชาวบ้านชาวเมืองเค้าไม่เห็นต้องระบุ action ก็ยังเรียก index ให้ Pylons จะยอมน้อยหน้าได้ไง ขนาด ASP.NET MVC ยังทำได้เลย ถ้า Pylons ทำไม่ได้ย้ายกลับนะเฟร้ย

เลยไปค้นๆมา ได้ความว่ามันก็มีทางเปิดใช้ minimization เพื่อให้ Routes ทำงานตามพฤติกรรมเดิม โดยในไฟล์ Routes.py จะมีบรรทัดนึงที่เขียนว่า

map.minimization = False

ก็แก้เป็น True ซะก็สิ้นเรื่อง ง่ายมะๆ

แต่ช้าก่อน ถ้ามันง่ายขนาดนั้น แล้วไม่เปิดใช้ไว้ตั้งแต่แรกฟระ ความจริงปรากฏว่า Routes รุ่นถัดไปจะไม่ยอมให้เปิดการทำงานของ Minimization แล้ว วิธีแก้ที่ยั่งยืนกว่าคือ เพิ่มกฏเอง
ฟังดูเหมือนจะยาก แต่แค่เพิ่มเข้าไปบรรทัดนึงเท่านั้นเอง

map.connect(‘/{controller}’, action=’index’)

แบบนี้ ถ้าเข้า http://localhost:5000/hello ก็จะวิ่งไป index ให้แล้ว

ก็ง่ายๆเช่นนี้แล
ด้วยรักและไพลอนส์

ปล. แต่ว่าเข้า http://localhost:5000/hello/ ก็ยัง 404 นะ

Updates on IronRuby

26 Aug 2008

Last week I went to Thailand Next Web App and joined in a Rails session. They had a talk and discussed about running Rails in production. Of course, JRuby, Glassfish and other Ruby implementation including IronRuby were mentioned. Then I was asked about it because I was the only .NET guy there. At that time, I didn’t follow the IronRuby for a long time so I replied them that no one other than John Lam and Phil Haacked have used the IronRuby.

Few days ago, while I read the feed about asp.net, I found an open source IronRuby project that aims to help developer implement WPF/Silverlight in rails-like fashion. And that’s mean we, the developers, now can use IronRuby.

After some googling, I’ve recalled that IronRuby, like IronPython, is part of the Microsoft Dynamic Language Runtime. They both are released with full source under Microsoft Public License(MS-PL). Though the source code of IronRuby is hosted on RubyForge and IronPython is on the CodePlex, the license doesn’t mention that they’re open source projects. However, the IronRuby libraries are tended to be open source and are opened to receive the contributions.

From the IronRuby home page, the implementation still does not pass the RubySpecs and can only dispatch some Rails requests. That’s mean it still cannot be used to host a Rails application. But you can utilize .NET framework and create an application with it, like IronNails. If you want to give it a try, you have to compile it yourself from the source. Of course, Windows machine is essential.

In TNWA last week, Sirn said “Don’t host your rails apps with IIS”. I’m sure the situation will change if IronRuby can host Rails app perfectly and I’m waiting for that day.

PS. As for IronPython, I saw how IronPython used with ASP.NET and it was fantastic!! However, I never have chance to try it myself.