VScode python插件

1.LiveCode

从扩展商店安装完以后初次使用可能异常

要配置一下解释器的路径

设置(ctrl+,)-> 搜索 Livecode:Python Path

然后填解释器的路径

如果我们有循环或需要展示一些中间变量状态,就可以使用该插件,LiveCode主要拥有下面四个功能

实时评估:不需要运行Python脚本就可以查看各个变量的值

变量显示:每当声明或更改一个变量时,它的新值都会同时改变并显示

2在 VS Code 调试器中自动显示变量形状和维度信息

原文在这里

win下

复制代码
C:\Users\<你的用户名>\.vscode\extensions\ms-python.debugpy-<版本号>-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_xml.py

linux下

复制代码
  ~/.vscode/extensions/ms-python.debugpy-*\bundled\libs\debugpy\_vendored\pydevd\pydevd_plugins\extensions\types/

修改 get_variable_details() 函数

打开 pydevd_xml.py 文件,找到get_variable_details()函数,完整修改之后如下

复制代码
def get_variable_details(val, evaluate_full_value=True, to_string=None, context: Optional[str] = None):
    """
    :param context:
        This is the context in which the variable is being requested. Valid values:
            "watch",
            "repl",
            "hover",
            "clipboard"
    """
    try:
        # This should be faster than isinstance (but we have to protect against not having a '__class__' attribute).
        is_exception_on_eval = val.__class__ == ExceptionOnEvaluate
    except:
        is_exception_on_eval = False

    if is_exception_on_eval:
        v = val.result
    else:
        v = val

    _type, type_name, resolver = get_type(v)
    type_qualifier = getattr(_type, "__module__", "")
    if not evaluate_full_value:
        value = DEFAULT_VALUE
    else:
        try:
            # 添加形状信息
            shape_info = ""
            try:
                # 处理 PyTorch Tensor
                if type_qualifier == "torch" and hasattr_checked(v, 'shape') and hasattr_checked(v, 'dtype'):
                    shape = tuple(v.shape)
                    dtype = str(v.dtype)
                    shape_info = f"{{Tensor: {shape}}} "
                # 处理 NumPy ndarray
                elif type_qualifier == "numpy" and hasattr_checked(v, 'shape') and hasattr_checked(v, 'dtype'):
                    shape = tuple(v.shape)
                    dtype = str(v.dtype)
                    shape_info = f"{{ndarray: {shape}}} "
                # 处理 Pandas DataFrame
                elif type_qualifier == "pandas.core.frame" and hasattr_checked(v, 'shape'):
                    shape = tuple(v.shape)
                    shape_info = f"{{DataFrame: {shape}}} "
                # 处理 Pandas Series
                elif type_qualifier == "pandas.core.series" and hasattr_checked(v, 'shape'):
                    shape = tuple(v.shape)
                    dtype = str(v.dtype)
                    shape_info = f"{{Series: {shape}}} "
                # 处理其他有 shape 属性的对象
                elif hasattr_checked(v, 'shape'):
                    shape_info = f"{{{v.shape}}} "
                # 处理可计数对象
                elif hasattr_checked(v, '__len__'):
                    try:
                        length = len(v)
                        # 对于字符串类型,只显示 {str} 而不显示长度
                        if type_name == "str":
                            shape_info = f"{{{type_name}}} "
                        else:
                            shape_info = f"{{{type_name}: {length}}} "
                    except:
                        pass
            except:
                pass
            
            str_from_provider = _str_from_providers(v, _type, type_name, context)
            if str_from_provider is not None:
                value = shape_info + str_from_provider

            elif to_string is not None:
                value = shape_info + to_string(v)

            elif hasattr_checked(v, "__class__"):
                if v.__class__ == frame_type:
                    value = pydevd_resolver.frameResolver.get_frame_name(v)

                elif v.__class__ in (list, tuple):
                    if len(v) > 300:
                        value = "%s: %s" % (str(v.__class__), "<Too big to print. Len: %s>" % (len(v),))
                    else:
                        value = "%s: %s" % (str(v.__class__), v)
                else:
                    try:
                        cName = str(v.__class__)
                        if cName.find(".") != -1:
                            cName = cName.split(".")[-1]

                        elif cName.find("'") != -1:  # does not have '.' (could be something like <type 'int'>)
                            cName = cName[cName.index("'") + 1 :]

                        if cName.endswith("'>"):
                            cName = cName[:-2]
                    except:
                        cName = str(v.__class__)

                    value = "%s: %s" % (cName, v)
            else:
                value = shape_info + str(v)
        except:
            try:
                value = repr(v)
            except:
                value = "Unable to get repr for %s" % v.__class__

    # fix to work with unicode values
    try:
        if value.__class__ == bytes:
            value = value.decode("utf-8", "replace")
    except TypeError:
        pass

    return type_name, type_qualifier, is_exception_on_eval, resolver, value

再重启即可

3 Better Comments

写注释的好帮手

它能根据关键词用不同的颜色高亮代码片段。支持以下类型的高亮:

感叹号 "!" 代码警告。

问号"?"代表存留疑问。

TODO 代码未来将要进行的操作。

@param 参数

此外,它还支持在设置中自定义需要高亮句子的首部关键词。

4 autoDocstring

写注释的好帮手

能够自动生成函数的注释格式,通过tab键快速切换填充块编写相应的注释。

Python Indent

python 自动缩进

相关推荐
知乎的哥廷根数学学派3 小时前
基于生成对抗U-Net混合架构的隧道衬砌缺陷地质雷达数据智能反演与成像方法(以模拟信号为例,Pytorch)
开发语言·人工智能·pytorch·python·深度学习·机器学习
WangYaolove13143 小时前
Python基于大数据的电影市场预测分析(源码+文档)
python·django·毕业设计·源码
知乎的哥廷根数学学派3 小时前
基于自适应多尺度小波核编码与注意力增强的脉冲神经网络机械故障诊断(Pytorch)
人工智能·pytorch·python·深度学习·神经网络·机器学习
cnxy1884 小时前
Python爬虫进阶:反爬虫策略与Selenium自动化完整指南
爬虫·python·selenium
用户8356290780515 小时前
Python 实现 Excel 条件格式自动化
后端·python
深蓝电商API6 小时前
Scrapy管道Pipeline深度解析:多方式数据持久化
爬虫·python·scrapy
噎住佩奇6 小时前
(Win11系统)搭建Python爬虫环境
爬虫·python
basketball6166 小时前
python 的对象序列化
开发语言·python
rgeshfgreh6 小时前
Python流程控制:从条件到循环实战
前端·数据库·python