Djangoでblogをはじめてみました
http://www.hexacosa.net/blog/
Djangoでサイト構築しました。仕事の合間をぬいつつ2週間くらい。
ほとんど機能が満たせていませんが、メモ帳程度には使えるようになったので、今後は
こちらを更新していきたいと思います。
構成的には、Ubuntu + mod_python + Django1.0 です。
Djangoはドキュメントがそろっていて、開発がサクサク進みます。
日本語ドキュメントに感謝
http://djangoproject.jp/doc/ja/1.0/index.html
Django関連のエントリにも感謝。
http://michilu.com/blog/posts/
http://d.hatena.ne.jp/CortYuming/20080917/p1
smtpd.py
http://d.hatena.ne.jp/perezvon/20080204/1202148894
import asyncore, smtpd smtpd.DebuggingServer((('localhost', 20025), ('localhost', 25)) asyncore.loop()
今頃やけど。。。なんでこんな書き方できるのかわからなかったけど、smtpd.pyを読んだらわかった。asyncore(asynchat)で実装されてるんですね。日々勉強。
mechanize使ってみた。
http://d.hatena.ne.jp/tokuhirom/20080103/1199341429がかっちょいいので、ぼくもPythonでやってみる。
$ sudo easy_install mechanize
from mechanize import Browser base = "http://example.com/pukiwiki.php?cmd=edit&page=" def post(page, body): br = Browser() #br.add_password(base + page, 'user', 'pass') br.open(base + page) br.select_form(nr=1) br['msg'] = body res = br.submit('write') if __name__ == '__main__': wikitext = "hoge" post('testtesthoge', wikitext)
う〜ん、credential()に対応するメソッドがよくわかりませんでした。add_password()かな?テストできない。ローカルでためせたらあとで試そっと。
ドキュメントとか見ましたが、べんりですね。スパムっぽくなりそうな気がしないでもない。
Reference
easy_installを使って、mechanize for Pythonを試してみる - ふにゃるん
mechanize — Documentation
coverage.pyを使ってみた。
http://nedbatchelder.com/code/modules/coverage.html
普通にコードカバレッジをだしてみる。
$ python coverage.py -e $ python coverage.py -x ../pyramidTest.py ...
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
"-e"でデータ消して、"-x"でテスト実行。"-r"でレポート。13〜15行目がテストできていないらしい。
続けて、チェック済みコードを出力してみる。
$ python coverage.py -a ../pyramid.py $ cat ../pyramid.py,cover > def returnAster(count): > ret = "" > for i in range(count): > for j in range(i+1): > ret += "*" > ret += "\n" > ret = ret[:-1] > return ret > if __name__ == "__main__": ! for i in range(5): ! print returnAster(i) ! print "\n"
"ファイル名.cover"でチェック済みのソースが出力される。実際に中身をみてみると、
テストがカバーしきれていないところ(ほんとにそう言えるかは別問題やね)に"!"が付くみたい。
Pythonのunittestモジュールのほんとにちょっとしたtips
だれかが書いてそうだけど、Pythonのunittestモジュールのtipsをめも。
私はほとんど以下のようにunittest.main()しか使わないのですが、
ユニットテスト実行時にオプションを指定することで、個別にユニットテストが実行できるみたい。
ということに今頃気づきました。
if __name__ == "__main__": unittest.main()
まず、"-h"オプションでUsageが表示されます。
$ python pyramidTest.py -h Usage: pyramidTest.py [options] [test] [...] Options: -h, --help Show this message -v, --verbose Verbose output -q, --quiet Minimal output Examples: pyramidTest.py - run default set of tests pyramidTest.py MyTestSuite - run suite 'MyTestSuite' pyramidTest.py MyTestCase.testSomething - run MyTestCase.testSomething pyramidTest.py MyTestCase - run all 'test*' test methods in MyTestCase
おぉ。で、個別に実行したいときは、自分で書いたテスト名とかも覚えてないので、pydocとかでメソッド名みて、それを指定してやればいいわけです。
いままで、全実行しかやってなかったけど、これでテストケースごとにできるね。
$ pydoc pyramidTest Help on module pyramidTest: NAME pyramidTest FILE /Users/hattori/Programming/Python/ut_sample/pyramidTest.py CLASSES unittest.TestCase(__builtin__.object) pyramidTestCase class pyramidTestCase(unittest.TestCase) | Method resolution order: | pyramidTestCase | unittest.TestCase | __builtin__.object | | Methods defined here: | | testdouble(self) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
で、さらに。"-v"オプションなんかをつけると、れぽーとにできそうな出力になります。
$ python pyramidTest.py -v pyramidTestCase.testdouble testdouble (__main__.pyramidTestCase) ... ok
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
と、ここまでです。なんかテストツールに興味わいてきたなぁ。
伝え聞いたことのあるテストスーツは以下くらい。(関係ないものもあるかも。。。)ちょっと調べてみよっと。
- pytest : http://codespeak.net/py/dist/test.html
- nose : http://pypi.python.org/pypi/nose
- fixture : http://code.google.com/p/fixture/
- testtools : http://testtools.python-hosting.com/ : fixtureはこちらに取り込まれたのかな。
- coverage : http://cheeseshop.python.org/pypi/coverage/2.6/
テストコードと本体のコードをいちお。
#pyramidTest.py import unittest import pyramid class pyramidTestCase(unittest.TestCase): def testsingle(self): self.assertEqual("*", pyramid.returnAster(1)) def testdouble(self): self.assertEqual("*\n**", pyramid.returnAster(2)) if __name__ == "__main__": unittest.main()
#pyramid.py def returnAster(count): ret = "" for i in range(count): for j in range(i+1): ret += "*" ret += "\n" ret = ret[:-1] return ret if __name__ == "__main__": for i in range(5): print returnAster(i) print "\n"