2013-07-20 40 views

回答

2

也许下面的帮助您开始...

import sys 
import os 
import random 


class GrammarNaziError(SyntaxError): 
    pass 


class GrammarNaziImporter(object): 
    def find_module(self, module_name, package_path): 
     if package_path: 
      search_paths = package_path 
      module_name = module_name.split('.')[-1] 

     else: 
      search_paths = sys.path + [ '.' ] 

     for i in search_paths: 
      path = os.path.join(i, module_name) 
      if os.path.isdir(path): 
       path = os.path.join(path, '__init__.py') 
      else: 
       path += '.py' 

      if os.path.exists(path): 
       if not self.valid_syntax(path): 
        raise GrammarNaziError(
         "The module %s failed Grammar Nazi Inspection" % path) 

       break 

    def valid_syntax(self, path): 
     return random.randint(0, 10) # substitute with logic of your choice 

rest = GrammarNaziImporter() 
sys.meta_path.insert(0, rest) 

附:祝你好运;)

P.P.S.编辑以便代码适用于初始化 .py's并且快速找到模块失败