ファイル内の行数をエレガントに数えたい
これもいいけど、
f = open("distribute_setup.py") len([None for l in f])
こうするとより速いらしい。
sum(1 for line in open('filename'))
参考:http://stackoverflow.com/questions/845058/how-to-get-line-count-cheaply-in-python
社内の人達++
すごく久しぶりにはてだにアクセスした
ちまたではブログがどーのこーの言われてるけどあまり興味は沸かず。
日々、記憶に留めておける自信がないのでメモとか書き留めておきたいこととかを保存するものとして使用できりゃなんでもいい。
デコレーター:クラスメソッド @classmethod
とあるコードを読んでいると @classmethod というデコレータが出現。
調べたりしてみた。
# -*- coding: utf-8 -*- class Hey1: def hi(self, name): print "Hi, " + name + "!" print type(Hey1) # <type 'classobj'> print type(Hey1()) # <type 'instance'> print type(Hey1.hi) # <type 'isntancemethod> Hey1().hi('Sam') # Hi, Sam! Hey1.hi('Michel') #Traceback (most recent call last): # File "<stdin>", line 1, in <module> # Hey1.hi('Michel') #TypeError: unbound method hi() must be called with Hey1 instance as first argument (got str instance instead)
うんうん
# -*- coding: utf-8 -*- class Hey2: @classmethod def yo(cls, name): print "Yo, " + name + "!" print type(Hey2) # <type 'classobj'> print type(Hey2()) # <type 'instance'> print type(Hey2.yo) # <type 'isntancemethod> Hey2().yo('David') # Yo, David! Hey2.yo('Mary') # Yo, Mary!
へぇー
classmethod(function)
function のクラスメソッドを返します。クラスメソッドは、インスタンスメソッドが暗黙の第一引数としてインスタンスをとるように、第一引数としてクラスをとります。
http://www.python.jp/doc/2.5/lib/built-in-funcs.html
ふんふん。staticmethod() については別途読む。
文字列を切って繋げるのにどちらが早いか調べた
頻繁にこんなのに出会う。
「受け取った文字列 "20110801" を "2011-08-01" にする」
文字列を切ったり繋いだりするのに、どちらが早いのかなーと思って調べた。
<?php function get_now() { return microtime(true); } $str = "20110801"; $start1 = get_now(); $result1 = substr($str, 0, 4)."-".substr($str, 4, 2)."-".substr($str, 6, 2); echo "[result1]" . $result1 . "\n"; echo get_now() - $start1."\n"; $start2 = get_now(); $result2 = $str[0].$str[1].$str[2].$str[3]."-".$str[4].$str[5]."-".$str[6].$str[7]; echo "[result2]" . $result2 . "\n"; echo get_now() - $start2."\n"; /* 結果 [result1]2011-08-01 0.000043 [result2]2011-08-01 0.000013 [result1]2011-08-01 0.000071 [result2]2011-08-01 0.000041 [result1]2011-08-01 0.000063 [result2]2011-08-01 0.000040 */ ?>
ちなみに PHP2.5.10 だった。
迷ってみたり
例1
<?php class Employee { private $db; private $name; private $position; function __construct(&$db) { $this->db =& $db; } function setName($name) { $this->name = $name; } function setPosition($position) { $this->position = $position; } function create() { // create } function destroy() { // destroy } } $employee = new Employee(DBobj); $employee->setName('nameA'); $employee->setPosition('regular'); $employee->create(); ?>
例2
<?php class Employee { private $name; private $position; function setName($name) { $this->name = $name; } function setPosition($position) { $this->position = $position; } function create(&$dbobj) { // create } function destroy(&$dbobj) { // destroy } } $employee = new Employee; $employee->setName('nameA'); $employee->setPosition('regular'); $employee->create(DBobj); ?>
個人的には例2の方が好きなんだけどなあ。
extends と implements のメモ
extends
<?php class A { function aa() { print "A - aa"; } } class B extends A { function bb() { print "B - bb"; } } $a = new B; $a->aa(); # A - aa $a->bb(); # B - bb ?>
implements
<?php Interface A_interface { function aa(); } class A implements A_interface { function aa() { print "A - aa"; } } $a = new A; $a->aa("aa"); # A - aa ?>
合!体!
<?php Interface AB_interface { function aabb(); } class A { function aa() { print "A - aa"; } } class AB extends A implements AB_interface { function aabb() { print $this->aa(); print $this->bb(); } function bb() { print "B - bb"; } } $ab = new AB; $ab->aa(); # A - aa $ab->bb(); # B - bb $ab->aabb(); # A - aaB - bb ?>
へえー
(PHP/5.2.10)