2012年8月8日水曜日

開発環境

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

4.

コード(TextWrangler)

sample.py

#!/usr/bin/env python
#encoding: utf-8

def countLines(name):
 file = open(name)
 return len(file.readlines())

def countChars(name):
 file = open(name)
 return len(file.read())

def test(name):
 result = ("行数: " + str(countLines(name)) +  
   ", 文字数: " + str(countChars(name)))
 return result

if __name__ == "__main__":
 name = "sample.py"
 for f in [countLines,countChars,test]:
  print(f(name))

myclient1.py

#!/usr/bin/env python

from sample  import *
name = 'sample.py'
for f in [countLines,countChars,test]:
 print(f(name))

myclient2.py

#!/usr/bin/env python

import sample
name = "sample.py"
for f in [sample.countLines,sample.countChars,sample.test]:
 print(f(name))

入出力結果(Terminal)

$ ./myclient1.py
20
405
行数: 20, 文字数: 405
kamimuras-MacBook-Pro:python kamimura$ ./myclient2.py
20
405
行数: 20, 文字数: 405
kamimuras-MacBook-Pro:python kamimura$ python
Python 3.2.3 (default, Apr 18 2012, 20:17:30) 
[GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.12)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import myclient1
20
405
行数: 20, 文字数: 405
>>> import myclient2
20
405
行数: 20, 文字数: 405
>>> for m in [myclient1,myclient2]:
...     print(m.__dict__)
... 
{'name': 'sample.py', '__warningregistry__': {("unclosed file <_io.TextIOWrapper name='sample.py' mode='r' encoding='UTF-8'>", <class 'ResourceWarning'>, 6): True}, 'f': <function test at 0x1076041e8>, '__builtins__': {'bytearray': <class 'bytearray'>, 'IndexError': <class 'IndexError'>, 'all': <built-in function all>, 'help': Type help() for interactive help, or help(object) for help about object., 'vars': <built-in function vars>, 'SyntaxError': <class 'SyntaxError'>, 'UnicodeDecodeError': <class 'UnicodeDecodeError'>, 'memoryview': <class 'memoryview'>, 'isinstance': <built-in function isinstance>, '__build_class__': <built-in function __build_class__>, 'copyright': Copyright (c) 2001-2012 Python Software Foundation.
All Rights Reserved.

Copyright (c) 2000 BeOpen.com.
All Rights Reserved.

Copyright (c) 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.

Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved., 'NameError': <class 'NameError'>, 'BytesWarning': <class 'BytesWarning'>, 'dict': <class 'dict'>, 'IOError': <class 'IOError'>, 'oct': <built-in function oct>, 'bin': <built-in function bin>, 'SystemExit': <class 'SystemExit'>, 'format': <built-in function format>, 'repr': <built-in function repr>, 'sorted': <built-in function sorted>, 'False': False, 'RuntimeWarning': <class 'RuntimeWarning'>, 'list': <class 'list'>, 'iter': <built-in function iter>, 'Warning': <class 'Warning'>, '__package__': None, 'round': <built-in function round>, 'dir': <built-in function dir>, 'set': <class 'set'>, 'bytes': <class 'bytes'>, 'UnicodeTranslateError': <class 'UnicodeTranslateError'>, 'issubclass': <built-in function issubclass>, 'EOFError': <class 'EOFError'>, 'locals': <built-in function locals>, 'BufferError': <class 'BufferError'>, 'slice': <class 'slice'>, 'FloatingPointError': <class 'FloatingPointError'>, 'sum': <built-in function sum>, 'getattr': <built-in function getattr>, 'abs': <built-in function abs>, 'exit': Use exit() or Ctrl-D (i.e. EOF) to exit, 'print': <built-in function print>, 'True': True, 'FutureWarning': <class 'FutureWarning'>, 'ImportWarning': <class 'ImportWarning'>, 'None': None, 'hash': <built-in function hash>, 'ReferenceError': <class 'ReferenceError'>, 'len': <built-in function len>, 'credits':     Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands
    for supporting Python development.  See www.python.org for more information., 'frozenset': <class 'frozenset'>, '__name__': 'builtins', 'ord': <built-in function ord>, 'super': <class 'super'>, 'TypeError': <class 'TypeError'>, 'license': Type license() to see the full license text, 'KeyboardInterrupt': <class 'KeyboardInterrupt'>, 'UserWarning': <class 'UserWarning'>, 'filter': <class 'filter'>, 'range': <class 'range'>, 'staticmethod': <class 'staticmethod'>, 'ResourceWarning': <class 'ResourceWarning'>, 'SystemError': <class 'SystemError'>, 'BaseException': <class 'BaseException'>, 'pow': <built-in function pow>, 'RuntimeError': <class 'RuntimeError'>, 'float': <class 'float'>, 'MemoryError': <class 'MemoryError'>, 'StopIteration': <class 'StopIteration'>, 'globals': <built-in function globals>, 'divmod': <built-in function divmod>, 'enumerate': <class 'enumerate'>, 'Ellipsis': Ellipsis, 'LookupError': <class 'LookupError'>, 'open': <built-in function open>, 'quit': Use quit() or Ctrl-D (i.e. EOF) to exit, 'UnicodeError': <class 'UnicodeError'>, 'zip': <class 'zip'>, 'hex': <built-in function hex>, 'next': <built-in function next>, 'ImportError': <class 'ImportError'>, 'chr': <built-in function chr>, 'type': <class 'type'>, '__doc__': "Built-in functions, exceptions, and other objects.\n\nNoteworthy: None is the `nil' object; Ellipsis represents `...' in slices.", 'Exception': <class 'Exception'>, 'tuple': <class 'tuple'>, 'reversed': <class 'reversed'>, 'UnicodeEncodeError': <class 'UnicodeEncodeError'>, 'input': <built-in function input>, 'hasattr': <built-in function hasattr>, 'delattr': <built-in function delattr>, 'setattr': <built-in function setattr>, 'SyntaxWarning': <class 'SyntaxWarning'>, 'compile': <built-in function compile>, 'ArithmeticError': <class 'ArithmeticError'>, 'str': <class 'str'>, 'property': <class 'property'>, 'GeneratorExit': <class 'GeneratorExit'>, 'int': <class 'int'>, '__import__': <built-in function __import__>, 'KeyError': <class 'KeyError'>, 'PendingDeprecationWarning': <class 'PendingDeprecationWarning'>, 'EnvironmentError': <class 'EnvironmentError'>, 'ascii': <built-in function ascii>, 'id': <built-in function id>, 'OSError': <class 'OSError'>, 'DeprecationWarning': <class 'DeprecationWarning'>, 'min': <built-in function min>, 'UnicodeWarning': <class 'UnicodeWarning'>, 'any': <built-in function any>, 'complex': <class 'complex'>, 'bool': <class 'bool'>, 'ValueError': <class 'ValueError'>, 'NotImplemented': NotImplemented, 'map': <class 'map'>, 'exec': <built-in function exec>, 'max': <built-in function max>, 'object': <class 'object'>, 'TabError': <class 'TabError'>, 'callable': <built-in function callable>, 'ZeroDivisionError': <class 'ZeroDivisionError'>, 'eval': <built-in function eval>, '__debug__': True, 'IndentationError': <class 'IndentationError'>, 'AssertionError': <class 'AssertionError'>, 'classmethod': <class 'classmethod'>, 'UnboundLocalError': <class 'UnboundLocalError'>, 'NotImplementedError': <class 'NotImplementedError'>, 'AttributeError': <class 'AttributeError'>, 'OverflowError': <class 'OverflowError'>}, '__file__': 'myclient1.py', 'countChars': <function countChars at 0x107604160>, '__package__': None, 'test': <function test at 0x1076041e8>, 'countLines': <function countLines at 0x1076040d8>, '__cached__': '__pycache__/myclient1.cpython-32.pyc', '__name__': 'myclient1', '__doc__': None}
{'name': 'sample.py', '__warningregistry__': {("unclosed file <_io.TextIOWrapper name='sample.py' mode='r' encoding='UTF-8'>", <class 'ResourceWarning'>, 6): True}, 'f': <function test at 0x1076041e8>, '__builtins__': {'bytearray': <class 'bytearray'>, 'IndexError': <class 'IndexError'>, 'all': <built-in function all>, 'help': Type help() for interactive help, or help(object) for help about object., 'vars': <built-in function vars>, 'SyntaxError': <class 'SyntaxError'>, 'UnicodeDecodeError': <class 'UnicodeDecodeError'>, 'memoryview': <class 'memoryview'>, 'isinstance': <built-in function isinstance>, '__build_class__': <built-in function __build_class__>, 'copyright': Copyright (c) 2001-2012 Python Software Foundation.
All Rights Reserved.

Copyright (c) 2000 BeOpen.com.
All Rights Reserved.

Copyright (c) 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.

Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved., 'NameError': <class 'NameError'>, 'BytesWarning': <class 'BytesWarning'>, 'dict': <class 'dict'>, 'IOError': <class 'IOError'>, 'oct': <built-in function oct>, 'bin': <built-in function bin>, 'SystemExit': <class 'SystemExit'>, 'format': <built-in function format>, 'repr': <built-in function repr>, 'sorted': <built-in function sorted>, 'False': False, 'RuntimeWarning': <class 'RuntimeWarning'>, 'list': <class 'list'>, 'iter': <built-in function iter>, 'Warning': <class 'Warning'>, '__package__': None, 'round': <built-in function round>, 'dir': <built-in function dir>, 'set': <class 'set'>, 'bytes': <class 'bytes'>, 'UnicodeTranslateError': <class 'UnicodeTranslateError'>, 'issubclass': <built-in function issubclass>, 'EOFError': <class 'EOFError'>, 'locals': <built-in function locals>, 'BufferError': <class 'BufferError'>, 'slice': <class 'slice'>, 'FloatingPointError': <class 'FloatingPointError'>, 'sum': <built-in function sum>, 'getattr': <built-in function getattr>, 'abs': <built-in function abs>, 'exit': Use exit() or Ctrl-D (i.e. EOF) to exit, 'print': <built-in function print>, 'True': True, 'FutureWarning': <class 'FutureWarning'>, 'ImportWarning': <class 'ImportWarning'>, 'None': None, 'hash': <built-in function hash>, 'ReferenceError': <class 'ReferenceError'>, 'len': <built-in function len>, 'credits':     Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands
    for supporting Python development.  See www.python.org for more information., 'frozenset': <class 'frozenset'>, '__name__': 'builtins', 'ord': <built-in function ord>, 'super': <class 'super'>, 'TypeError': <class 'TypeError'>, 'license': Type license() to see the full license text, 'KeyboardInterrupt': <class 'KeyboardInterrupt'>, 'UserWarning': <class 'UserWarning'>, 'filter': <class 'filter'>, 'range': <class 'range'>, 'staticmethod': <class 'staticmethod'>, 'ResourceWarning': <class 'ResourceWarning'>, 'SystemError': <class 'SystemError'>, 'BaseException': <class 'BaseException'>, 'pow': <built-in function pow>, 'RuntimeError': <class 'RuntimeError'>, 'float': <class 'float'>, 'MemoryError': <class 'MemoryError'>, 'StopIteration': <class 'StopIteration'>, 'globals': <built-in function globals>, 'divmod': <built-in function divmod>, 'enumerate': <class 'enumerate'>, 'Ellipsis': Ellipsis, 'LookupError': <class 'LookupError'>, 'open': <built-in function open>, 'quit': Use quit() or Ctrl-D (i.e. EOF) to exit, 'UnicodeError': <class 'UnicodeError'>, 'zip': <class 'zip'>, 'hex': <built-in function hex>, 'next': <built-in function next>, 'ImportError': <class 'ImportError'>, 'chr': <built-in function chr>, 'type': <class 'type'>, '__doc__': "Built-in functions, exceptions, and other objects.\n\nNoteworthy: None is the `nil' object; Ellipsis represents `...' in slices.", 'Exception': <class 'Exception'>, 'tuple': <class 'tuple'>, 'reversed': <class 'reversed'>, 'UnicodeEncodeError': <class 'UnicodeEncodeError'>, 'input': <built-in function input>, 'hasattr': <built-in function hasattr>, 'delattr': <built-in function delattr>, 'setattr': <built-in function setattr>, 'SyntaxWarning': <class 'SyntaxWarning'>, 'compile': <built-in function compile>, 'ArithmeticError': <class 'ArithmeticError'>, 'str': <class 'str'>, 'property': <class 'property'>, 'GeneratorExit': <class 'GeneratorExit'>, 'int': <class 'int'>, '__import__': <built-in function __import__>, 'KeyError': <class 'KeyError'>, 'PendingDeprecationWarning': <class 'PendingDeprecationWarning'>, 'EnvironmentError': <class 'EnvironmentError'>, 'ascii': <built-in function ascii>, 'id': <built-in function id>, 'OSError': <class 'OSError'>, 'DeprecationWarning': <class 'DeprecationWarning'>, 'min': <built-in function min>, 'UnicodeWarning': <class 'UnicodeWarning'>, 'any': <built-in function any>, 'complex': <class 'complex'>, 'bool': <class 'bool'>, 'ValueError': <class 'ValueError'>, 'NotImplemented': NotImplemented, 'map': <class 'map'>, 'exec': <built-in function exec>, 'max': <built-in function max>, 'object': <class 'object'>, 'TabError': <class 'TabError'>, 'callable': <built-in function callable>, 'ZeroDivisionError': <class 'ZeroDivisionError'>, 'eval': <built-in function eval>, '__debug__': True, 'IndentationError': <class 'IndentationError'>, 'AssertionError': <class 'AssertionError'>, 'classmethod': <class 'classmethod'>, 'UnboundLocalError': <class 'UnboundLocalError'>, 'NotImplementedError': <class 'NotImplementedError'>, 'AttributeError': <class 'AttributeError'>, 'OverflowError': <class 'OverflowError'>}, '__file__': 'myclient2.py', '__package__': None, 'sample': <module 'sample' from 'sample.py'>, '__cached__': '__pycache__/myclient2.cpython-32.pyc', '__name__': 'myclient2', '__doc__': None}
>>>quit() 
$

0 コメント:

コメントを投稿