utils.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:portia2code 作者: scrapinghub 项目源码 文件源码
def merge_sources(*sources):
    def sort_imports(import_string):
        order = 0
        if import_string.startswith('import .'):
            order = 1
        elif import_string.startswith('from .'):
            order = 3
        elif import_string.startswith('from '):
            order = 2
        return order, import_string
    sources = [getsource(source).splitlines() for source in sources]
    imports = []
    for source in sources:
        for line in source:
            if line.startswith(('from', 'import')):
                imports.append(line)
    without_imports = (line for source in chain(sources) for line in source
                       if not line.startswith(('from', 'import')))
    imports.sort(key=sort_imports)
    return '\n'.join(chain(imports, without_imports))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号