• 一个发挥到极致的yield案例


     1 def find_commands(management_dir):
     2     """
     3     Given a path to a management directory, returns a list of all the command
     4     names that are available.
     5 
     6     Returns an empty list if no commands are defined.
     7     """
     8     command_dir = os.path.join(management_dir, 'commands')
     9     # l = []
    10     # for _, name, is_pkg in pkgutil.iter_modules([npath(command_dir)]): #生成器走一回,返回对象含多个元素
    11     #     if not is_pkg and not name.startswith('_'):
    12     #         l.append(name)
    13     # l结果:['check', 'compilemessages', 'createcachetable', 'dbshell', 'diffsettings', 'dumpdata', 'flush', 'inspectdb', 'loaddata', 'makemessages',
    14     # 'makemigrations', 'migrate', 'runserver', 'sendtestemail', 'shell', 'showmigrations', 'sqlflush', 'sqlmigrate', 'sqlsequencereset', 'squashmigrations',
    15     # 'startapp', 'startproject', 'test', 'testserver']
    16 
    17     return [name for _, name, is_pkg in pkgutil.iter_modules([npath(command_dir)])
    18             if not is_pkg and not name.startswith('_')]
     1 def iter_modules(path=None, prefix=''):
     2     """Yields (module_loader, name, ispkg) for all submodules on path,
     3     or, if path is None, all top-level modules on sys.path.
     4 
     5     'path' should be either None or a list of paths to look for
     6     modules in.
     7 
     8     'prefix' is a string to output on the front of every module name
     9     on output.
    10     """
    11 
    12     if path is None:
    13         importers = iter_importers()
    14     else:
    15         importers = map(get_importer, path)
    16 
    17     yielded = {}
    18     for i in importers: #生成器走一回,返回对象只有一个元素
    19         for name, ispkg in iter_importer_modules(i, prefix): #生成器走一回,返回对象含多个元素,数量与path路径的文件数一致
    20             if name not in yielded:
    21                 yielded[name] = 1
    22                 yield i, name, ispkg
    23 
    24 
    25 @simplegeneric
    26 def iter_importer_modules(importer, prefix=''):
    27     if not hasattr(importer, 'iter_modules'):
    28         return []
    29     return importer.iter_modules(prefix)
     1 def get_importer(path_item):
     2     """Retrieve a PEP 302 importer for the given path item
     3 
     4     The returned importer is cached in sys.path_importer_cache
     5     if it was newly created by a path hook.
     6 
     7     The cache (or part of it) can be cleared manually if a
     8     rescan of sys.path_hooks is necessary.
     9     """
    10     try:
    11         importer = sys.path_importer_cache[path_item]
    12     except KeyError:
    13         for path_hook in sys.path_hooks:
    14             try:
    15                 importer = path_hook(path_item)
    16                 sys.path_importer_cache.setdefault(path_item, importer)
    17                 break
    18             except ImportError:
    19                 pass
    20         else:
    21             importer = None
    22     return importer
  • 相关阅读:
    20135326、20135303linux实验二实验报告
    家庭作业汇总
    信息安全系统设计基础第十周学习总结
    信息安全系统设计基础第十二周总结
    20135326王亦可信息安全系统设计基础期末总结
    第十一周学习总结
    20135326、20135303linux实验四实验报告
    20135326、20135303linux实验一实验报告
    第十四周学习笔记
    20135326、20135303linux实验三实验报告
  • 原文地址:https://www.cnblogs.com/Fmaj7/p/13175417.html
Copyright © 2020-2023  润新知