2009年8月14日金曜日

急に都内に泊まってみた


Udon @ Tsurutontan Roppongi
Originally uploaded by vik_122

世の中はお盆休みのようで。
仕事中に急に奥様から「ホテルオークラに泊まろう」と連絡があったので」六本木で合流した。

いつも混んでいて入るのを躊躇していたおうどんのお店、「つるとんたん六本木店」に30分待ちでいってみた。

海老真蒸と胡麻団子のおうどんに温泉卵をつけてみた。なかなか美味しかったよ。

2008年8月8日金曜日

wsgiで俺フレームワークでも作ろうか

japanvik.netのリニューアルをしたくなったので、この際wsgiの勉強も兼ねてmvcな俺フレームワークでもつかって見ようかと思う。

サイトのコンセプトは「俺ポータル」 - 自分のブログのアグレゲート済みRSSやTwitterやコードなど、自分のまとめサイトにしたいと思っている。
しばらくwsgi関連のサイトを巡回しながらコードをちょこちょこ書いている。現段階では次のような仕組みで作っている:

テンプレートエンジン: mako + ドキュメント用にmarkdown2
URLディスパッチャー: selector + pasteのstatic
その他自分で書いたものちょこちょこ

本当はelixirでdb接続を行いたいのだけど、dbへのコネクションの維持のあたりがよく解っていないので、今のところdb接続はは無し。Pylonsのコードでも釣ってみようかな。


DBの接続まで出来たら今時のmvcフレームワークとして成立するので、機会があれば公開しようと思う。比較的小規模なサイトならサクサクと出来てしまいそう。ただ、このままいくと、ほぼPylonsになってしまう気配がw

2007年8月26日日曜日

A Python port of the Agile RSS Aggregator in Ruby

I came across an Agile RSS Aggregator in Ruby which had very elegant code, so I decided to port it to Python. It uses wsgi, the Universal Feed Parser, and the Mako Template.

Here is the code:


#!/usr/bin/python
from wsgiref.simple_server import make_server
from mako.template import Template
import feedparser

class Feed:
""" A Python port of the "Agile RSS Aggregator in Ruby"
See README for details
"""
def __init__(self, environ, start_response):
self.environ = environ
self.start = start_response

def __iter__(self):
status = "200 OK"
response_headers = [('Content-type','text/html')]
self.start(status, response_headers)
stories = []
for f in open('feeds.txt', 'r'):
feed = feedparser.parse(f.strip())
stories.extend(feed.entries)
page = Template(filename='news.mako', output_encoding='utf8')
yield page.render(stories=stories)

httpd= make_server("", 8000, Feed)
httpd.serve_forever()

If you compare this to the Ruby version, you can see the striking resemblance of the code. Ruby and Python are very similar when it comes to doing simple things like this.

For comparison's sake, here is the relevant portion of the mako template:

   % for item in stories:
<div class="section details">
<h3><a href="${item.link}">${item.title}</a></h3>
<p style="color:#444;font-size:90%;">${item.summary}</p>
</div>
% endfor

Again, very similar to the Erubis template the original version used. Technically, I could have used something like:

<% item.link %>

instead of :

${item.link}

which will make the code more similar, but I prefer the latter convention because it makes the template easier to read.

One caveat is with the wsgiref.simpleserver. I couldn't find a quick way to define the static directory ("files" in the original) to serve the css style sheet. The workaround was to put the style directly in the mako template, which admittedly is a bit annoying.

However, in a real world situation, chances are that we will be using an application framework like Pylons or TurboGears which will solve the problem, so I'm not going to dig too deeply into it. If anyone knows a simple way of exposing a filesystem directory in wsgi, please let me know!

Finally, here is the download link

2007年6月6日水曜日

Fixing python-setuptools in Ubuntu Feisty

Ubuntu recently upgraded its Python distribution from 2.4 to 2.5.
Along the way, setup tools (read easy_install) seems to break itself.

If your python related installs are failing and getting errors like:

E: Sub-process /usr/bin/dpkg returned an error code (1)
A package failed to install. Trying to recover:
Setting up python-setuptools (0.6c3-1ubuntu4) ...
pycentral: pycentral pkginstall: already exists: /usr/lib/python2.4/site-packages/setuptools.pth
pycentral pkginstall: already exists: /usr/lib/python2.4/site-packages/setuptools.pth

Then python-setuptools need to be reinstalled.

sudo apt-get remove python-setuptools
sudo apt-get install python-setuptools

This fixes the dependency issue and you should be able to

easy_install Pylons TurboGears

without getting errors.

2007年5月17日木曜日

[Rails] Accessing :object_name :method passed from the view in your custom helpers

While making a wrapper helper to extend the functionality of the stock date_select, I needed to access the actual date object attributes within the helper method.

The api of the date_select helper is like this:

date_select(object_name, method, options = {})

Where object_name is is the object in the template, and method is the method of that object which returns a date for the select boxes to show.

I wanted to make an enhanced version of the date_select with some javascript goodness, while maintaining the same parameters, and needed the actual date object so I can use it on the javascript output.
Basically I needed to do this:
date = object_name.method

Here is how I did it:

def ajax_date_select(object_name, method, options = {})
# Get the object_name.method value
date = instance_variable_get("@#{object_name.to_s.dup}").send(method.to_s.dup)


The instance_variable_get method allows you to reference the variables in the rendered view.
So for example in the controller, if you said something like:

@hoge = Hoge.find(1)

and in the view:

<%= ajax_date_select :hoge :created_at %>

by using instance_variable_get, you can do the equivalent of

date = hoge.created_at

from within the custom helper code.