開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc., Emacs(Text Editor)
- プログラミング言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7)のV部(モジュール)のまとめ演習5.(パッケージインポート)を解いてみる。
その他参考書籍
5.(パッケージインポート)
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-
import mypkg.mymod
if __name__ == '__main__':
name = 'sample.py'
for func in [mypkg.mymod.countLines, mypkg.mymod.countChars,
mypkg.mymod.test]:
print(func(name))
print(mypkg.mymod.test1())
mypkg/mymod.py
#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-
def countLines(name):
with open(name) as f:
return len(f.readlines())
def countChars(name):
with open(name) as f:
return len(f.read())
def test(name):
return (countLines(name), countChars(name))
def test1():
name = input("ファイル名: ")
with open(name) as f:
lines = len(f.readlines())
f.seek(0)
chars = len(f.read())
return (lines, chars)
if __name__ == '__main__':
name = 'sample.py'
print(test(name))
print(test1())
入出力結果(Terminal)
$ ./sample.py 30 687 (30, 687) ファイル名: sample.py (30, 687) $
0 コメント:
コメントを投稿