開発環境
- 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部(モジュール)のまとめ演習4.(ネストインポート)を解いてみる。
その他参考書籍
4.(ネストインポート)
コード(BBEdit)
sample.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())
myclient.py
#!/usr/bin/env python3.3 #-*- coding: utf-8 import sample name = "sample.txt" print(sample.test(name))
myclient1.py
#!/usr/bin/env python3.3 # -*- coding:utf-8 from sample import * name = "sample.txt" print(test(name))
入出力結果(Terminal)
$ ./myclient.py (5, 73) $ ./myclient1.py (5, 73) $ python Python 3.3.1 (default, Apr 6 2013, 12:29:18) [GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import myclient (5, 73) >>> import myclient1 (5, 73) >>> dir(myclient) ['__builtins__', '__cached__', '__doc__', '__file__', '__initializing__', '__loader__', '__name__', '__package__', 'name', 'sample'] >>> dir(myclient1) ['__builtins__', '__cached__', '__doc__', '__file__', '__initializing__', '__loader__', '__name__', '__package__', 'countChars', 'countLines', 'name', 'test', 'test1'] >>> quit() $
0 コメント:
コメントを投稿