2011-08-05 7 views
0

このコードは、Sourceクラスの__str __methodがself._pathパイソンGoogleの閉鎖コンパイラのソースクラスの質問

それが自己のために特別なプロパティですが呼ばhttp://code.google.com/p/closure-library/source/browse/trunk/closure/bin/build/source.py

からのコピーですか? Cuzのは、私は場所を見つけることができませんでした

_pathがちょうどか、私が他の属性などのオブジェクトに設定することはできません属性で、ソース・クラス

import re 

_BASE_REGEX_STRING = '^\s*goog\.%s\(\s*[\'"](.+)[\'"]\s*\)' 
_PROVIDE_REGEX = re.compile(_BASE_REGEX_STRING % 'provide') 
_REQUIRES_REGEX = re.compile(_BASE_REGEX_STRING % 'require') 

# This line identifies base.js and should match the line in that file. 
_GOOG_BASE_LINE = (
    'var goog = goog || {}; // Identifies this file as the Closure base.') 


class Source(object): 
    """Scans a JavaScript source for its provided and required namespaces.""" 

    def __init__(self, source): 
    """Initialize a source. 

    Args: 
     source: str, The JavaScript source. 
    """ 

    self.provides = set() 
    self.requires = set() 

    self._source = source 
    self._ScanSource() 

    def __str__(self): 
    return 'Source %s' % self._path #!!!!!! what is self_path !!!! 

    def GetSource(self): 
    """Get the source as a string.""" 
    return self._source 

    def _ScanSource(self): 
    """Fill in provides and requires by scanning the source.""" 

    # TODO: Strip source comments first, as these might be in a comment 
    # block. RegExes can be borrowed from other projects. 
    source = self.GetSource() 

    source_lines = source.splitlines() 
    for line in source_lines: 
     match = _PROVIDE_REGEX.match(line) 
     if match: 
     self.provides.add(match.group(1)) 
     match = _REQUIRES_REGEX.match(line) 
     if match: 
     self.requires.add(match.group(1)) 

    # Closure's base file implicitly provides 'goog'. 
    for line in source_lines: 
     if line == _GOOG_BASE_LINE: 
     if len(self.provides) or len(self.requires): 
      raise Exception(
       'Base files should not provide or require namespaces.') 
     self.provides.add('goog') 


def GetFileContents(path): 
    """Get a file's contents as a string. 

    Args: 
    path: str, Path to file. 

    Returns: 
    str, Contents of file. 

    Raises: 
    IOError: An error occurred opening or reading the file. 

    """ 
    fileobj = open(path) 
    try: 
    return fileobj.read() 
    finally: 
    fileobj.close() 

答えて

1

ありませんで、この変数を定義します。先頭のアンダースコアは、作者がオブジェクトの内部詳細であると感じ、パブリックインターフェイスの一部と見なしたくないことを意味します。

この特定のケースでは、何かがそのソースファイルの外側から属性を設定していない限り、それは単なる間違いのようです。誰もがSourceオブジェクトのstr()に電話をかけようとしない限り、それは何の害も及ぼしません。

ところで、あなたは特別なものがあると思っているようです。selfselfという名前は決して特別なものではありません。メソッドの最初のパラメータにこの名前を使用するのが一般的ですが、処理されるオブジェクトを参照する他の名前と同じような名前にすぎません。したがって、エラーが発生することなくself._pathにアクセスできる場合は、オブジェクトの他の名前を使っても同じようにアクセスできます。