首页 > 编程语言 >PYTHON 判断引用路径的类型

PYTHON 判断引用路径的类型

时间:2022-12-04 01:11:23浏览次数:38  
标签:None return PYTHON 路径 module dict 引用 path mod

PYTHON 判断引用路径的类型

如下方法, 用于实现在不加载模块的前提下, 判断某一个python引用路径是否存在, 以及属于什么类型(模块目录, 模块文件, 类名). 用于在一些不便加载(如项目较大,加载需要额外的环境支持, 或者文件本身较大,加载较慢) 等场景下, 扫描 python 类/模块 是否存在

import ast
import imp

def check_module(module_reference_path):
    def _get_module(module_reference_path):
        if not module_reference_path:
            return None
        module_dict = module_reference_path.split(".")
        module_name = module_dict[-1]
        module_path = "/".join(module_dict[:-1])
        try: 
            mod = imp.find_module(module_name, [module_path])
        except:
            return None
        return mod
    mod = _get_module(module_reference_path)

    # find module not a module, maybe a class or not existed
    if mod is None:
        module_dict = module_reference_path.split(".")
        up_mod = _get_module( ".".join(module_dict[:-1]))
        # check if the upper level is a module, if not, then it not existed
        if up_mod is None or len(module_dict)==1:
            return None
        class_name = module_dict[-1]
        # if upper level is module, then check the if it is a class
        code_body = ast.parse(up_mod[0].read()).body
        for item in code_body:
            if not isinstance(item, ast.ClassDef):
                continue
            if item.name == class_name:
                return "Class"
        return None
    if mod[0] is None:
        return "Module Path"
    return "Module File" 

标签:None,return,PYTHON,路径,module,dict,引用,path,mod
From: https://www.cnblogs.com/coreylin/p/16949268.html

相关文章

  • AI人工智能-python
    AI概念语音合成可将文字信息转化为声音信息语音识别可将语音识别为文字百度ai平台页面熟悉              创建应用     ......
  • 【python】使用百度api进行音频文件转写
     【python】使用百度api进行音频文件转写脚本目标:智能云的音频文件转写文档只给了个demo,每次只能传1分钟以内的音频啥的,不好直接用,简单打包一下,做到把音频放文件夹,直......
  • nodejs、ts 上传文件之根据文件路径实现本地文件上传至服务器
    安装依赖库npminstallmyjs-common 参数//待上传文件数组letfiles=["C:\\Users\\bm\\img\\0.jpg","C:\\Users\\bm\\img\\1.jpg","C:\\Users\\bm\\img\\2.......
  • python循环
    python学习1.for循环range()函数foriinrange(1,10):print(i)#输出为123456789该函数还可以制定步长如:foriinrange(1,10,2)即为以2为步长,在1到......
  • python报错 ModuleNotFoundError: No module named ‘win32api‘
    参考链接https://blog.csdn.net/weixin_43149311/article/details/120806116报错信息如下:ModuleNotFoundError:Nomodulenamed‘win32api‘解决方法参考1.重新......
  • [自用]c++值传递和引用传递
    https://baijiahao.baidu.com/s?id=1702573193376441989&wfr=spider&for=pc总结:1.函数参数传递主要分为值传递和&引用传递,两种传递都是形参对实参的拷贝;2.直接点的用法......
  • 在linux虚拟机中运行python
    在linux虚拟机中运行python方法1:运用python指令运行一般情况linux系统会自动安装python所以在终端中输入python3就自动进入python的交互模式输入ctrl+z退出交互模式......
  • python推导式
    python推导式推导式是用一行式子来完成循环操作的语句,一般与for循环结合来使用。列表推导式公式[exprforvalueincollection[ifcondition]]例子对循环内元素......
  • Python基础之函数
    一、函数的作用函数就是将⼀段具有独⽴功能的代码块整合到⼀个整体并命名,在需要的位置调⽤这个名称即可完成对应的需求。函数在开发过程中,可以更⾼效的实现代码重⽤。二、......
  • Python处理PDF
    目录ReadinfoRotatePageMergePDFsSplitPDFsEncryptaPDFDecryptaPDFAddwatermarkreference:HowtoWorkWithaPDFinPythonreference:给PDF添加水印本文使......