1/24/2011

[読書]Head First Python 読んでみる。 Chapter3

Chapter3は File & Exception : Dealing with Errors.


全章通じてそうだけど、小さな例題アプリを少しづつ、お話と共に作りながら進んでいく形式。
いつもながらこのHead Firstのやり方はうまい。猫でもわかるようになっている。

ただ、猫でない人にとっては冗長、そういう人は対象読者ではないにしても。

英語を読み進めるスピードが遅いのと、オンラインで読んでいるせいもあって
だんだん、面倒になってきたぞ。どんどん飛ばして読んでもいいのだが。。うーむ。

この内容ならこの章は2~3ページでいいじゃない。というのは野暮なのでしょう。たぶん。

もうちょっと頑張ってみよう。

    1/16/2011

    [読書]Head First Python 読んでみる。 Chapter2

    Head First Python 読んでみる。 Chapter2


    読書記録としてはWikiの方がよいのだろうけど、続かなそうなので。。
    この心理的作用はなんだんだろう?お手軽だから?うーん。

    Chapter 2 はモジュール作成から配布を中心に、名前空間など書いてある。
    この段階からPyPIの使い方が書かれているのは良いことなんだと思う。

    • How to distribution the own Python Modules.
    • How to use the Python Package Index ("PyPI"). Registration, Upload Module, Update Module.
    • Python Name Space. Main name space is known __main__.
    1. A module is a text file that contains Python code.
    2. We can use the distribution utilities to turn own module into a shareable package.
    3. The setup.py program provides metadata about your module and is used
      to build, install, and upload your packaged distribution.
    4. The range() BIF can be used with 'for' to iterate a fixed number of times.
    5. Including end=’’ as a argument to the print() BIF switches off its automatic inclusion of a new-line on output.
    6. Arguments to your functions are optional if you provide them with a default value.

    1/06/2011

    [読書]Head First Python 読んでみる。 Chapter1

    Head FirstシリーズのPython版が出ていたので読んでみるなど。

    まだ翻訳版は出ていない?っぽいので原書で。英語出来ないのにw

    Head Firstシリーズは「学習」に重点を置いていて、如何に効率的に理解させて記憶に残すか?
    を考えて作られている本。絵が一杯で楽しいんだけど、その分冗長でちんたら進むので、飽きてしまう事も。

    所謂、リファレンスではないので初学者向け。「浅く、広くPythonに触れてみる」といったところ。
    ちゃんとするなら、ネズミ本。Apple AppsStoreで600円だし。




    Chapter1
    Meet Python : Everyone Loves Lists.

    IDLEの使い方
    Python のList
    For Loop
    BIFs instance()
    Function
    再帰

    など。小気味よく読了。
    後半の章ではGoogle App Engine を用いてWebアプリつくったりもしていて期待出来そう。

    なんだけど、とある事情で今月中に読み終えないといけず、496 Pages もあるからなぁ。
    単純日割りで24page/day。英語が最大のネックか。

    12/16/2010

    [読書]ケント・ベック 実装パターン 

    Kent Beck の実装パターンを読んだ。
    日常的にコードを書く人ではない、趣味の園芸な自分が読んでも楽しめる。

    一通り読んだものの咀嚼出来ていないところがあるので、時間を見つけて査読したいな。

    ちょっと前に、Junitのソースコードを眺めていたんだけど、この本読んでる最中に、
    「ああ、ここの記述はあそこのソースでもやってたなぁ」と思いだした。
    もう一度、Junitちゃんと読んでみようかなぁ。

    コレクションAPIのパフォーマンス比較をするために、測定用フレームワークを作っていて付録Aで
    そのコードを公開している、コード自体はとても小さいシンプルなものだけど、本書の中のパターン
    が使われている。

    後でWebで見たかったので写経してみた。
    コメントいれてる。変なところがあったら御免なさい。

    このListSerchクラスのserchメソッドの実行時間を計測すると
    search 25.30 77.81 551.10 と結果出力される。
    要素数が増加すると実行時間が増えている。



    import java.util.ArrayList;
    import java.util.List;

    public class ListSerch {
    private List<Integer> numbers;
    private int probe;

    public ListSerch(int size){
    numbers = new ArrayList<Integer>();
    for (int i = 0; i < size; i++) {
    numbers.add(i);
    probe = size / 2;
    }
    }
    //ArrayListの真ん中を取得させる。
    public void search(){
    numbers.contains(probe);
    }
    }

    ドライバークラスは以下。
    テストしたいクラスに含まれるメソッドの配列を引数にしてMethodsTimerインスタンス化。
    report()メソッドを実行する。
    結果はコンソールに
    メソッド名 1回実行での時間 10回実行での合計時間 100回実行での合計時間 ・・・合計が1秒こえるまでやる。

    public class TestDriver {

    public static void main(String[] args) throws Exception{
    MethodsTimer tester = new MethodsTimer(ListSerch.class.getDeclaredMethods());
    tester.report();
    }
    }



    import java.lang.reflect.Method;

    public class MethodsTimer {
    public final Method[] methods;
    private static final int MAXIMUM_SIZE = 100;
    public static final int ONE_SECOND = 1000000000;

    public MethodsTimer(Method[] methods){
    this.methods = methods;
    }

    public void report() throws Exception{
    for(Method each : methods){
    System.out.print(String.format("%.2f\t", r.getMethodTime()));
    for (int size = 1; size <= MAXIMUM_SIZE; size *=10)
    {
    MethodTimer r = new MethodTimer(size, each);
    r.run();
    System.out.print(r.getMethodTime() + "\t");
    }
    System.out.println();
    }
    }
    }



    import java.lang.reflect.Constructor;
    import java.lang.reflect.Method;

    public class MethodTimer {
    private final int size;
    private final Method method;
    private Object instance;
    private long totalTime;
    private int iterations;
    private long overhead;

    public MethodTimer(int size, Method method) throws Exception{
    this.size=size;
    this.method=method;
    instance = createInstance();
    }

    public MethodTimer(int iterations) throws Exception{
    this(0,MethodTimer.Overhead.class.getMethod("nothing", new Class[0]));
    this.iterations = iterations;
    }

    private static MethodTimer overheadTimer(int iterations) throws Exception{
    return new MethodTimer(iterations);
    }

    private Object createInstance() throws Exception {
    Constructor<?> constructor = method.getDeclaringClass().getConstructor(
    new Class[] { int.class });
    return constructor.newInstance(new Object[] { size });
    }

    double getMethodTime(){
    return (double)(totalTime - overhead) / (double)iterations;
    }


    void run() throws Exception {
    iterations = 1;
    while (true) {
    totalTime = computeTotalTime();
    if (totalTime > MethodsTimer.ONE_SECOND)
    break;
    iterations *= 2;
    }
    overhead = overheadTimer(iterations).computeTotalTime();
    }



    private long computeTotalTime() throws Exception {
    long start = System.nanoTime();
    for (int i = 0; i < iterations; i++) {
    method.invoke(instance, new Object[0]);
    }
    return System.nanoTime() - start;
    }

    public static class Overhead {
    public Overhead(int size) { }
    public void nothing() { }
    }

    }

    10/06/2010

    Pydevのデバッガーがエラーになる件

    EclipseでPythonを触ってみようとして、はまったのでメモしておく

    python 3.1
    pydev 1.6.2
    実行環境は別途落としてきた3.1を利用。

    debug実行時にソースにダブルバイトのコメントが存在した場合、

    #あ
    n=1
    print("hoge")


    エラーは以下

    pydev debugger: starting
    Traceback (most recent call last):
    File "C:\util\ide\eclipse36\eclipse\plugins\org.python.pydev.debug_1.6.3.2010100513\pysrc\pydevd.py", line 1147, in
    debugger.run(setup['file'], None, None)
    File "C:\util\ide\eclipse36\eclipse\plugins\org.python.pydev.debug_1.6.3.2010100513\pysrc\pydevd.py", line 925, in run
    line = stream.readline() #Should not raise an exception even if there are no more contents
    UnicodeDecodeError: 'cp932' codec can't decode bytes in position 55-56: illegal multibyte sequence

    encodeはutf-8に設定してるのだが?pydevd.pyのエラー箇所をみてる。

    if not IS_PY3K:
    execfile(file, globals, locals) #execute the script
    else:
    stream = open(file)
    try:
    encoding = None
    #Get encoding!
    for i in range(2):
    line = stream.readline() #Should not raise an exception even if there are no more contents
    #Must be a comment line
    if line.strip().startswith('#'):
    #Don't import re if there's no chance that there's an encoding in the line
    if 'coding' in line:
    import re
    p = re.search(r"coding[:=]\s*([-\w.]+)", line)
    if p:
    encoding = p.group(1)
    break
    finally:
    stream.close()


    の line = stream.readline()でエラー。
    その上で、cp932でstream = open(file)してしまってるためっぽい。
    openしてcodingを調べたいのに、cp932で開いてエラーになる

    ソース先頭2行目までに、# coding: utf-8
    があれば、stream = open(file, encoding='utf-8')のとこだけの変更でよいが、
    encoding='utf-8'もいれといた。


    if not IS_PY3K:
    execfile(file, globals, locals) #execute the script
    else:
    #stream = open(file) #2010.10.6 cahnge
    stream = open(file, encoding='utf-8')
    try:
    #encoding = None #2010.10.6 cahnge
    encoding='utf-8'
    #Get encoding!
    for i in range(2):
    line = stream.readline() #Should not raise an exception even if there are no more contents
    #Must be a comment line
    if line.strip().startswith('#'):
    #Don't import re if there's no chance that there's an encoding in the line
    if 'coding' in line:
    import re
    p = re.search(r"coding[:=]\s*([-\w.]+)", line)
    if p:
    encoding = p.group(1)
    break
    finally:
    stream.close()

    if encoding:
    stream = open(file, encoding=encoding)
    else:
    stream = open(file)
    try:
    contents = stream.read()
    finally:
    stream.close()


    他にもっと、ちゃんとしたやり方がある筈なのだが、初心者なのでよくわからん。
    動くようになったから、とりあえずこれでいいや。


    2011-01-16 追記
    バグトラッカに報告された模様、有難いことです。
    http://suzumizaki.blog6.fc2.com/blog-entry-157.html
    私も世の中に、貢献できるような人間になりたい。。

    5/02/2010

    ヒツジヤ殺人未遂事件

    ヒツジヤは洋傘を売ってる小さなお店。
    昔住んでた街の商店街の一角にポツンとあって、猫が出入りしてた。
    それをHPに記事を書いたのが4年前くらい。

    先日、新聞記事を見ててびっくり、

    2010年2月19日午前11時50分ごろ、大阪府東大阪市足代1丁目の洋傘販売店「ヒツジヤ」
    において、男性2人(60歳代と40歳代)が、店主の74歳の男に包丁(刃渡り10センチ)
    で切りつけられるという事件が発生した。

     ~朝日新聞より


    僕は2006年に大阪を離れたのだけど、勝手なもので
    なんとなく、その後の物語が進んだのは僕の方でだけで
    他はそのままな気がしていたのだけれど、
    あの時間が止まっていそうなヒツジヤですら、
    殺人未遂事件がおきちゃうのだから、何が起きててもおかしくないなぁ。
    と考えさせられるものがあった。

    2/16/2010

    good

    内容:

    "create or replace function get_key (p_col in lookup_table.col1%type) return lookup_table.col2%type as v_result lookup_table.col2%type; begin select col2 into v_result from lookup_table where col1 = p_col; return v_result; end get_key; / Then you can use that function in your SQL*Loader control file, like so: Code: [Select all] [Show/ hide]load data infile 'data.txt' into table dwh_table fields terminated by ',' trailing nullcols (col1, col2 "get_key (:col1)")"
    - OraFAQ Forum: Server Utilities » SQL Loader & lookup tables issueGoogle サイドウィキで表示