2013年3月19日火曜日

開発環境

『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のV部(モジュール)のまとめ演習1.(簡単なモジュールファイルのインポート)を解いてみる。

その他参考書籍

1.(簡単なモジュールファイルのインポート)

コード(BBEdit)

sample.py

#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-

def countLines1(name):
    with open(name) as f:
        return len(f.readlines())

def countChars1(name):
    with open(name) as f:
        return len(f.read())

def test1(name):
    for x, y in [("行数",countLines1(name)), ("文字数", countChars1(name))]:
        print("{0}: {1}".format(x, y))

# 結果的にopen関数を2回呼び出す事になるのをseek関数を使ってファイルの先頭に移動して
# open関数の呼び出しを1回だけにするように修正

def countLines2(file):
    return len(file.readlines())

def countChars2(file):
    return len(file.read())

def test2(name):
    with open(name) as f:
        print(countLines2(f))
        f.seek(0)
        print(countChars2(f))

入出力結果(Terminal)

$ python
Python 3.3.0 (default, Sep 29 2012, 08:16:08) 
[GCC 4.2.1 Compatible Apple Clang 3.1 (tags/Apple/clang-318.0.58)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sample
>>> name="sample.py"
>>> sample.test1(name)
行数: 29
文字数: 646
>>> sample.test2(name)
29
646
>>> quit()
$ wc sample.py
      28      58     766 sample.py
$

UNIXのwcコマンドの実行結果と違う。。コメント中に日本語(マルチバイト)がコードに混ざってるから文字数がずれるのかなぁと思って日本語を削除して再び実行。(ちなみにwcコマンドの出力結果は順に行数、単語数、バイト数(文字数)みたい。)

コード(BBEdit)

sample.py

#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-

def countLines1(name):
    with open(name) as f:
        return len(f.readlines())

def countChars1(name):
    with open(name) as f:
        return len(f.read())

def test1(name):
    for x in [countLines1(name), countChars1(name)]:
        print(x)

def countLines2(file):
    return len(file.readlines())

def countChars2(file):
    return len(file.read())

def test2(name):
    with open(name) as f:
        print(countLines2(f))
        f.seek(0)
        print(countChars2(f))

入出力結果(Terminal)

$ python
Python 3.3.0 (default, Sep 29 2012, 08:16:08) 
[GCC 4.2.1 Compatible Apple Clang 3.1 (tags/Apple/clang-318.0.58)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sample
>>> name="sample.py"
>>> sample.test2(name)
26
529
>>> quit()
$ wc sample.py
      25      47     529 sample.py
$

単語数は一致。でも行数は1行ずれてる。ちゃんとman wcでマニュアルを読んでみると、行数はnewline(改行\n)で数えてるみたいだから1行ずれるみたい。さらに、オプションで-mを指定すれば、ちゃんとマルチバイトな日本語の文字数もちゃんと数えてくれるみたい。ということで再度、最初の日本語を含むコードを実行。

コード(BBEdit)

sample.py

#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-

def countLines1(name):
    with open(name) as f:
        return len(f.readlines())

def countChars1(name):
    with open(name) as f:
        return len(f.read())

def test1(name):
    for x, y in [("行数",countLines1(name)), ("文字数", countChars1(name))]:
        print("{0}: {1}".format(x, y))

def countLines2(file):
    return len(file.readlines())

def countChars2(file):
    return len(file.read())

def test2(name):
    with open(name) as f:
        print(countLines2(f))
        f.seek(0)
        print(countChars2(f))

入出力結果(Terminal)

$ python
Python 3.3.0 (default, Sep 29 2012, 08:16:08) 
[GCC 4.2.1 Compatible Apple Clang 3.1 (tags/Apple/clang-318.0.58)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sample
>>> name="sample.py"
>>> sample.test2(name)
29
647
>>> quit()
$ wc -clmw sample.py
      29      55     647 sample.py
$

0 コメント:

コメントを投稿