Doge log

Abby CTO 雑賀 力王のオフィシャルサイトです

2008-01-01から1年間の記事一覧

今年のふりかえり

とりあえず裏でコツコツ勉強しようと思ってた年だった。 来年からは少しエントリ増やす予定です。 まああと来年からPythonに本腰入れますよ!!

入れてるモジュール

最近Ubuntuをセットアップしたので入れたモジュールを挙げてみる。 In /opt/python26/lib/python2.6/site-packages: Zipped packages: PyAMF-0.3.1-py2.6.egg Tempita-0.2-py2.6.egg WebOb-0.9.4-py2.6.egg WebTest-1.1-py2.6.egg pyOpenSSL-0.7-py2.6-linux…

実装モジュールを切り替えるモジュール

昨日のコードをモジュール化するtry except ImportErrorとか多い書く人にはいいかも知れないな modinstaller.py from abc import ABCMeta, abstractmethod import sys def install(imports, *args, **kwargs): global _mod_name, _imports local = sys._getf…

実装モジュールを切り替える

pythonの定番パターンだと思うんだけどあんまり書いてなさげなので書いておく。 twisted厨にはおなじみのコードなのでtwisted厨はよまなくて良し!! 実装を抽象化しておき、モジュール切り替え + シングルトン化で使用する側は実装 を意識しないでよくする…

Python3000のパフォーマンス

The net result of the 3.0 generalizations is that Python 3.0 runs the pystone benchmark around 10% slower than Python 2.5. Most likely the biggest cause is the removal of special-casing for small integers. 以前は33%ぐらい遅かったはずなので…

Python3000のリスト内包表記

Python 3.1a0 (py3k:67517, Dec 4 2008, 22:33:17) [GCC 4.0.1 (Apple Inc. build 5484)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> l = [n for n in range(5)] >>> l [0, 1, 2, 3, 4] >>> l.__class__ >>> s …

selfの話

メソッドのself (2)について少し書いてみる。pythonの不満点でよく上がる定番の話は self地獄 インデント地獄 GIL外せよ! なんだけどそのうちのひとつであるselfの話。 まあ死ぬほど聞く話ではあるけど。多分和訳より原文の方を読むと掴めるんじゃないかな…

Shibuya.pm

まあShibuya.pmの申し込みに間に合わなくてustで見てたわけだけど。 見てるとやっぱ凄いなあ、おもろいなあという印象。 最近は裏でずっとocaml勉強してたんだけど見ててpython熱が復活した。 Shibuya.pm関係者のみなさんホントお疲れさまでした。

namedtupleの話

python2.6から導入されたnamedtupleについてちょっくら書いておくか。 namedtupleって? namedtupleは名前の通り名前付きでアクセスできるtupleを返す。 >>> from collections import namedtuple >>> p = namedtuple('Point', 'x y') >>> p1 = p(11, 22) >>>…

stackless pythonのお話

比較 stackless python 処理系自身に手を入れている(srcのStackless/、Python/以下) 処理の評価(eval)毎にcstack objectを生成し、状態を保存する いわゆるスケジューラー(ラウンドロビン)がデフォルトで組み込まれている bytecode的には通常のCPytonと…

Macでstackless pythonをビルドする

enable-stacklessfewerregistersを有効にする。 frameworkは別にいらんですよ。 CPPFLAGS=-I/opt/local/include LDFLAGS=-L/opt/local/lib ./configure --prefix=/opt/xxx--enable-stacklessfewerregisters && make && sudo make install

python2.6用にkqreactorを書き直す

2.6からはwrapperいらないので書き直す。 """ A kqueue()/kevent() based implementation of the Twisted main loop. To install the event loop (and you should do this before any connections, listeners or connectors are added):: | from twisted.int…

続Ruby libevent

テストをちょっと書いてみたら色々おかしかった。 githubに置いておこう。 GitHub - mopemope/rbevent: ruby extension library for libevent

Ruby libevent

rubyでlibeventを使うライブラリを書いてみた。 通常のeventも合わせてテスト全然してないお。 環境はruby1.9。1.8でも動くかな。 #include "ruby.h" #include <stdio.h> #include <sys/time.h> #include <event.h> VALUE mRubyEvent, mRubyEventConst, rb_cEvent, rb_cSignalEvent, rb_cTi</event.h></sys/time.h></stdio.h>…

pyeventでecho

ノンブロッキングのサンプル import event import socket import errno import signal def _read(sock): try: return sock.recv(1024) except socket.error, e: if e[0] == errno.EWOULDBLOCK: return None raise def start_client(sock): res = _read(sock)…

Pythonの言語仕様

[Python][書籍] インタプリタ で言語仕様ってねえんじゃねーの?みたいな話が出てる。 基本リファレンスが言語仕様という認識なんだけど。 日本語訳が曖昧でわかりにくいところもあるよねっていうだけで「言語仕様がない」という話ではないと思うのだが。あ…

pyeventでecho

使い方わかんないとか言う人向けサンプル (書きなぐりです) import event import socket def read(sock): msg = sock.recv(1024) sock.send(msg) event.read(sock.fileno(), read, sock).add() def accept(sock): conn, addr = sock.accept() event.read(c…

Pythonクイズ

>>> import [1] >>> class A(object):pass ... >>> def test(self): ... print "test" ... >>> a = A() >>> a.test = [2] >>> a.test > >>> a.test() test >>> b = A() >>> b.test() Traceback (most recent call last): File "", line 1, in AttributeError…

Pythonクイズの答え

http://d.hatena.ne.jp/mopemope/20081017/p1 >>> import types >>> class A(object):pass ... >>> def test(self): ... print "test" ... >>> a = A() >>> a.test = types.MethodType(test, a, a.__class__) >>> a.test > >>> a.test() test >>> b = A() >>…

Pythonクイズ

>>> 3.0/2 1.5 >>> 3.0??2 1.0 となるような ?? にあてはまる二文字を答えよ

局所的にクラスを拡張する

withを使ってなにか出来ないかずっと考えてたんだけどありきたりなアイディアしか浮かばなかった。 でもあまりwithをどうこうっていうのを書いてる人もいなかったので書いておく。 with句のブロック間のみクラスを拡張する。 from contextlib import context…

Pythonクイズの答え

http://d.hatena.ne.jp/mopemope/20081016/p1 >>> 3.0/2 1.5 >>> 3.0//2 1.0 切り捨て除算がある言語は珍しいのかな?と思って書いてみました。 id:morchinさん、id:odzさん回答ありがとうございました。 id:morchinさんの回答は強制型変換などさらに詳しく…

heapq

標準でないのかな??? なのでpythonから引っ張ってくる。 Arrayに突っ込む。 class Array def heappush(item) self.push(item) siftdown(0, self.length - 1) end def heappop lastelt = self.pop() if self.length > 0 returnitem = self[0] self[0] = la…

bisect, bsearch

pythonから引っ張ってくる class Array def insert_right(item, lo=0, hi=nil) if lo < 0 raise IndexError.new('lo must be non-negative') end if hi == nil hi = self.length end while lo < hi mid = ((lo+hi)/2).truncate if item < self[mid] hi = mid…

富山県民 id:yone098 スルーされてる?

わんくま同盟 ミスター富山県民ことid:yone098が出ないなんておかしいぞ!?

RubyInline

RubyInlineを使うとCを埋め込めるらしいので試してみた。 $:.unshift File.dirname(__FILE__) require 'rubygems' require 'inline' VERSION = '0.0.1' class KQueue inline do |builder| builder.include "<sys/event.h>" builder.include "<sys/time.h>" builder.include "<unistd.h>" builder</unistd.h></sys/time.h></sys/event.h>…

NeverBlockの話

ちょっと思うところがあってrubyはじめました。 eventletライクなものを作ってみようかなと思ってイチのイチからrubyを勉強中です。とりあえずrubyだとNeverBlockっていうのがあるようです。 http://steps.dodgson.org/?date=20080920基本の考え方はeventlet…

Fiberメモ

require 'fiber' def io_switch f = Fiber.current $h.add_watch(:f, proc{ $h.remove_watch(:f) f.transfer }) $h.switch end class Hub def initialize @process @wait = Hash.new end def add_watch(key,block) @wait[key] = block end def remove_watch(…

自作multiprocessingプログラムサンプル

別に2.6じゃなくても簡単に書こうと思えば書けるよねえ。 でmultiprocessingモジュールの中身はどうなってるのかって話があまり書かれてない感じだったので書いてみた。概略的なコードは以下。 import os import sys import time class Process(object): def…

Pythonクイズ(辞書の非破壊的操作)

Pythonクイズ(辞書の非破壊的操作) >>> d1 = {'a':1, 'b':2, 'c':3} >>> d2 = {'c':4, 'd':5, 'e':6} >>> print dict(d1.items()+d2.items()) {'a': 1, 'c': 4, 'b': 2, 'e': 6, 'd': 5} >>> d1 {'a': 1, 'c': 3, 'b': 2} >>> d2 {'c': 4, 'e': 6, 'd': 5} >…