Ian Lewis
Ian Lewis is a web developer living in Tokyo Japan. His current interests are in Django, python, alternative databases and rapid web application development. About Me...
  • growltestrunner の pynotify 対応 / pynotify の使い方

    最近、会社の AE35 さんが growltestrunner を作っていて、 modipyd を使って、ファイルを更新したタイミングで自動テストを自動的に実行してくれて、growlで通知するように素敵な環境を設定した。

    俺はlinuxなので、当然 growl がないけど、Mac OS の growl みないな libnotify があって、pynotify と言うpythonバインディングがあるから、pynotifyでも使えるように、 fork を作った。

    ちなみに、pynotify はこういう使い方

    import pynotify
    pynotify.init("My App")
    n = pynotify.Notification("Title", "Message", "/path/to/my/icon.png")
    n.show()
    
    Send feedback このエントリーを含むはてなブックマーク はてなブックマーク - growltestrunner の pynotify 対応 / pynotify の使い方
  • Python 例外のひどい仕様

    Pythonの例外オブジェクトは苦手です。例外のメッセージが何でもasciiとして扱われることがひどい。

    In [1]: t = ValueError("テスト".decode("utf8"))

    In [2]: print t
    ---------------------------------------------------------------------------
    UnicodeEncodeError                        Traceback (most recent call last)

    /home/ian/src/project/<ipython console> in <module>()

    UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

    In [3]: t = ValueError(u"テスト") # Unicode object's don't work either

    In [4]: print t
    ---------------------------------------------------------------------------
    UnicodeEncodeError                        Traceback (most recent call last)

    /home/ian/src/project/<ipython console> in <module>()

    UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

    Djangoのテストフレームワークで、doctestを使おうと思ったら、例外のメッセージがasciiじゃないとダメというのが判明

    u"""
    >>> test(-1)
        Traceback (most recent call last):
            ...
        ValueError: エラーですよ!
    """

    とやっても、うまくうごかない。以下のUnicodeDecodeErrorがでる

    ======================================================================
    ERROR: Doctest: app.tests
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/home/ian/.virtualenvs/test/lib/python2.5/site-packages/django/test/_doctest.py", line 2175, in runTest
        test, out=new.write, clear_globs=False)
      File "/home/ian/.virtualenvs/test/lib/python2.5/site-packages/django/test/_doctest.py", line 1403, in run
        return self.__run(test, compileflags, out)
      File "/home/ian/.virtualenvs/test/lib/python2.5/site-packages/django/test/_doctest.py", line 1291, in __run
        got += _exception_traceback(exc_info)
      File "/home/ian/.virtualenvs/test/lib/python2.5/site-packages/django/test/_doctest.py", line 269, in _exception_traceback
        return excout.getvalue()
      File "/usr/lib/python2.5/StringIO.py", line 270, in getvalue
        self.buf += ''.join(self.buflist)
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 24: ordinal not in range(128)
    
    ----------------------------------------------------------------------
    

    Update: moriyoshiさんにより、Python 2.6 でちゃんと動くみたい

    Send feedback このエントリーを含むはてなブックマーク はてなブックマーク - Python 例外のひどい仕様