Python

MacでPython3k (or X-MAC-JAPANESE codecの作り方)

Mac使っている人はどうやってPython3kにコンパイルをやってるんでしょうか。とりあえず、下のようにしてからコンパイルすると通る。 詳しい人情報求む。 # http://python.org/download/svn/ $ svn co http://svn.python.org/projects/python/trunk python-sv…

追随

http://use.perl.org/~miyagawa/journal/34461 また真似してUser-Agentの変更とhandlerの指定を出来るようにした。UserAgentの変更がうまくいっているかをdebuglevelが1のHTTPHandlerをつかって 確認する例。 #!/usr/bin/env python2.5 #-*- coding: utf-8 -…

Web::Scraper 0.14 released

http://use.perl.org/~miyagawa/journal/34457 また便利になってる。 相対URIは絶対URIに自動変換 (img@src, script@src etc) 便利なショートカット、HTML,RAWの追加 2の方はすでにクリアしてるので1の方はPython版でもlxmlのmake_links_absolute()で自動変…

Scraping Yahoo! Search with Web::Scraper in Python

次はhttp://menno.b10m.net/blog/blosxom/perl/scraping-yahoo-search-with-web-scraper.htmlと同じことをやってみる。比較しやすいようにPerlのコードも並べてみた。 コードの量も見た目もあんまり変わらない。 Perlは括弧がなくてすっきり、Pythonの方はセ…

lxmlメモ

HTMLパースするときにはlxml.htmlを使う etree.parse(url, etree.HTMLParser())みたいにParser明示的に書かなくてもOKだし、便利なメソッドも使える。 etree.parse(URL)と書かず、自分でurlopen & decode parse(URL)と短く書けるようになってるけど、UTF-8じ…

scraperにscraperを渡せるようにした

Web::Scraper の"Twitter Friends (complex)"ができるようになった。 s = scraper( process('span.vcard', people=scraper( process('a', link='@href', name='@title'), process('img', thumb='@src') ) ) ) 下のパッチ加えただけ。 + elif hasattr(val, 's…

Web::Scraper in Python hacks #2: Extract javascript and css content

http://use.perl.org/~miyagawa/journal/34398第二弾。普通にtextで取れた。 s = scraper( process("script", js="text"), )

リストや辞書の中のUnicodeを表示

Google グループ 昨日書いた、「Pythonはリストや辞書の中にある日本語をそのままprintで表示できない」という問題の解決方法が紹介されていました。 print (repr(s.scrape('http://d.hatena.ne.jp/keyword/%BA%B0%CC%EE%A4%A2%A4%B5%C8%FE')).decode('unico…

Web::Scraper in Python hacks #1: Extract links linking to images

http://use.perl.org/~miyagawa/journal/34325の真似をできるようにした。 #!/usr/bin/env python2.5 from scraper import scraper, process import codecs, sys sys.stdout = codecs.getwriter('utf-8')(sys.stdout) s = scraper( process("//a[contains(@h…

Web::Scraper in Python (もしくは scrAPI in Python)

lxml2.0からCSSSelectorが導入されたので、Web::Scraperのようなものを作ってみました。 とりあえず動作するところまでいったので載せておきます。機能はまだ全然足りてないので、簡単なことしかできません。 Python2.5とlxml2.0alpha が必要です。Pythonは…

Python3000 alpha リリース、そしてインストールが失敗

Python 3.0a1 Release http://python.org/download/releases/3.0/ おめでとうございます。 MacPortsでもすでに用意されているようです。 $ port search python3 python30 lang/python30 3.0a1 An interpreted, object-oriented programming language あ、por…

Unixコマンド in Python #2 不完全telnet

telnetlibというのがあるらしいのでテスト。 どういうときに便利なんだろうか。 #!/usr/bin/env python import sys import os import telnetlib if len(sys.argv) < 1: sys.stderr.write("error") sys.exit(1) port = 23 if len(sys.argv) == 2: host = sys.…

The Python 3000 PEP Parade まとめ

[Python-3000] PEP Parade PEP 0 -- Index of Python Enhancement Proposals (PEPs) | Python.org Whole Lotta PEPping goin' on (dirtSimple.org) reddit.com S 3101 Advanced String Formatting Talin http://www.python.org/dev/peps/pep-3101/ S 3108 St…

Emacs内でIPython

python-modeでIPython使うときにはipython.elが必要。 # ~/site-lispをelisp置き場としている場合 $ cd ~/site-lisp $ wget http://ipython.scipy.org/dist/ipython.el $ vi ~/.emacs # (require 'ipython)を追記 $ emacs これで``C-c C-!``でIPythonが起動…

dotfiles の svn 管理

Developer Enviroments Conference の発表資料 - 2nd life 今さらながらdotfilesをsvnで管理するようにしました。上のリンク先にあったスクリプトはRubyだったのでPythonで書き直し。 元のファイルには'.'をつけたくないので、例えば'zshrc'としてチェックイ…

IPython8.0 日本語Unicode対応完了

IPythonのバージョンがあがったらしいのでアップグレード。 以前あった文字化け問題は解消されたようです。 IPythonのUnicode問題 - rants $ ipython Python 2.4.3 (#1, Nov 4 2006, 21:43:51) Type "copyright", "credits" or "license" for more informati…

Writing Python Extensions

CでPythonのExtensionを書くためのチュートリアルを発見。 http://starship.python.net/crew/mwh/toext/toext.html頭から終わりまで通読して意味が通るように書かれているので http://docs.python.org/ext/ext.htmlよりわかりやすい気がする。 半分くらい読…

python-modeでエラー

http://w.koshigoe.jp/study/?%5Bpython%5Dpython-mode.el%A4%CE%A5%AD%A1%BC%A5%D0%A5%A4%A5%F3%A5%C9 python-modeの機能を一つも使っていないことが発覚。 python-send-buffer便利そうだなと思って試すと Traceback (most recent call last): File "<stdin>", lin</stdin>…

Unixコマンド in Python #1 cat

Pythonになれるために不定期にUnixコマンドをPythonで書いてみることにした。 まずはcat。 #!/usr/bin/env python import sys import fileinput def usage(): sys.stderr.write("Usage: %s [-n] [file ...]\n" % sys.argv[0]) def raw_print(args): for line…

Writing Python Extensions読了

後半殆ど"TODO"になってて何も書いてなかった。やはり本家のを読めということか。 でもそれなりに勉強になったと思う。

Pythonの拡張モジュール勉強メモ2

Cの関数とPythonをつなげるための準備。 メソッドテーブルを作る。 static PyMethodDef SpamMethods[] = { ... {"system", spam_system, METH_VARARGS, "Execute a shell command."}, ... {NULL, NULL, 0, NULL} /* Sentinel */ }; PyMethodDefは 名前 C関数…

Pythonの拡張モジュール勉強メモ1

http://docs.python.org/ext/intro.html "spam"という作るときには慣例でその実装をするCファイルを"spammodule.c"とつける。 でもモジュール名がある程度長いときには、その最後に".c"をつけただけのファイル名になる。 "spammify" -> spammify.c ファイル…

Python Challengeを始めた

The Python Challengeを始めた。

xxxはEnterpriseに向かない?

http://jesusphreak.infogami.com/blog/vrp1 読了。Djangoはenterpriseに向かない - Doge log http://wataru.justblog.jp/blog/2007/03/ll.html http://wataru.justblog.jp/blog/2007/03/python_ll2_7211.html「*はEnterpriseに向かない」というよう話はredd…

Python web development and frameworks in 2007

必読らしい。 http://jesusphreak.infogami.com/blog/vrp1 Djangoのところだけ読んだ。残りは明日以降。

あなたが知らない(かもしれない)PythonとDjango関係のドキュメント

Django tips: documentation and resources にPythonとDjangoのお勧めリソース一覧が載っていたのでメモ。 Python プログラミングの、またはPythonの初心者はThe Python Tutorial — Python 3.7.3 documentation、 ベストはhttp://diveintopython.org/Python…

IPythonのUnicode問題

python manage.py shellでテストしているときに気づいたこと。 IPythonだとUnicodeが正しく扱われない様子。標準のインタプリタだと $ python Python 2.4.3 (#1, Nov 4 2006, 21:43:51) [GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin Type "help…