有没有一种方法可以缩进Python而不会影响代码

彼得

我试图在Maya中编写用户界面,并且它与多个级别的父级和无缩进的混乱令人难以置信。目前,基本代码(无任何功能)大约有400行,需要一段时间才能找到我需要的位。

例如,使用下面的代码而不加注释:

#Earlier user interface

py.rowColumnLayout( numberOfColumns = 5 )
py.text( label="", width = 1 )
py.text( label="Column 1", enable = False, width = 250 )
py.text( label="", width = 1 )
py.text( label="Column 2" enable = False, width = 250 )
py.text( label="", width = 1 )

py.text( label="" )
py.rowColumnLayout( numberOfColumns = 4 )
py.text( label="   Input data:", align="left" )
py.text( label="" )
py.text( label="" )
py.text( label="" )
py.textField( text = "Text here" )
py.text( label="" )
py.text( label="" )
py.text( label="" )
py.setParent( ".." )

py.text( label="" )
py.rowColumnLayout( numberOfColumns = 4 )
py.rowColumnLayout( numberOfColumns = 5 )
py.radioButton( label = "Read file from path", width = 100 )
py.text( label="" )
py.button( label = "Browse" )
py.text( label="" )
py.button( label = "Validate" )
py.setParent( ".." )
py.text( label="" )
py.text( label="" )
py.text( label="" )
py.setParent( ".." )
py.setParent( ".." )

但是,这就是缩进的样子

py.rowColumnLayout( numberOfColumns = 5 )
    py.text( label="", width = 1 )
    py.text( label="Column 1", enable = False, width = 250 )
    py.text( label="", width = 1 )
    py.text( label="Column 2" enable = False, width = 250 )
    py.text( label="", width = 1 )

    py.text( label="" )
    py.rowColumnLayout( numberOfColumns = 4 )
        py.text( label="   Input data:", align="left" )
        py.text( label="" )
        py.text( label="" )
        py.text( label="" )
        py.textField( text = "Text here" )
        py.text( label="" )
        py.text( label="" )
        py.text( label="" )
        py.setParent( ".." )

    py.text( label="" )
    py.rowColumnLayout( numberOfColumns = 4 )
        py.rowColumnLayout( numberOfColumns = 5 )
            py.radioButton( label = "Read file from path", width = 100 )
            py.text( label="" )
            py.button( label = "Browse" )
            py.text( label="" )
            py.button( label = "Validate" )
            py.setParent( ".." )
        py.text( label="" )
        py.text( label="" )
        py.text( label="" )
        py.setParent( ".." )
    py.setParent( ".." )

有什么办法我可以使用缩进来编写它,但在执行时使其完全忽略它们?我看到了一个问题,问是否可以编写不带缩进的python,但我有点相反。

注意:某些py.*功能的输出值也需要分配给变量,只是尚未分配,因为需要首先对布局进行排序。

kartikg3

这是一个很好的用例,像我们这样的技术艺术家在Maya中构建UI时每天都要面对。

对于基于PyMEL的UI:

这是内置在PyMEL中的。您不必创建上下文管理器。布局命令本身是上下文管理器。您只需with在每个布局命令调用之前添加一个关键字,如下所示:

# Do this when using PyMEL for your UI code
import pymel.core as pm

# ...

with pm.rowColumnLayout( numberOfColumns = 5 ):
    pm.text( label="", width = 1 )
    pm.text( label="Column 1", enable = False, width = 250 )
    pm.text( label="", width = 1 )
    pm.text( label="Column 2", enable = False, width = 250 )
    pm.text( label="", width = 1 )

    pm.text( label="" )

    with pm.rowColumnLayout( numberOfColumns = 4 ):
        pm.text( label="   Input data:", align="left" )
        pm.text( label="" )
        pm.text( label="" )
        pm.text( label="" )
        pm.textField( text = "Text here" )
        pm.text( label="" )
        pm.text( label="" )
        pm.text( label="" )        

    pm.text( label="" )
    with pm.rowColumnLayout( numberOfColumns = 4 ):
        with pm.rowColumnLayout( numberOfColumns = 5 ):
            pm.radioButton( label = "Read file from path", width = 100 )
            pm.text( label="" )
            pm.button( label = "Browse" )
            pm.text( label="" )
            pm.button( label = "Validate" )

        pm.text( label="" )
        pm.text( label="" )
        pm.text( label="" )

对于基于maya.cmds的UI:

一种快速的解决方案是制作一个虚拟的上下文管理器。你可以做这样的事情

# Do this when using Maya's cmds for your UI code
import maya.cmds as cmds

# ...

from contextlib import contextmanager
@contextmanager
def neat_indent():
    # OPTIONAL: This is also an opportunity to do something before the block of code runs!
    try:
        # During this is where your indented block will execute
        # Leave it empty
        yield
    finally:
        # OPTIONAL: This is where you can write code that executes AFTER your indented block executes.
        pass

这样,您的代码不必更改太多。只需with在每个预期缩进的开头添加您的上下文管理器功能和关键字!

cmds.rowColumnLayout( numberOfColumns = 5 )
with neat_indent():
    cmds.text( label="", width = 1 )
    cmds.text( label="Column 1", enable = False, width = 250 )
    cmds.text( label="", width = 1 )
    cmds.text( label="Column 2", enable = False, width = 250 )
    cmds.text( label="", width = 1 )

    cmds.text( label="" )

    cmds.rowColumnLayout( numberOfColumns = 4 )
    with neat_indent():
        cmds.text( label="   Input data:", align="left" )
        cmds.text( label="" )
        cmds.text( label="" )
        cmds.text( label="" )
        cmds.textField( text = "Text here" )
        cmds.text( label="" )
        cmds.text( label="" )
        cmds.text( label="" )
        cmds.setParent( ".." )

    cmds.text( label="" )
    cmds.rowColumnLayout( numberOfColumns = 4 )
    with neat_indent():
        cmds.rowColumnLayout( numberOfColumns = 5 )
        with neat_indent():
            cmds.radioButton( label = "Read file from path", width = 100 )
            cmds.text( label="" )
            cmds.button( label = "Browse" )
            cmds.text( label="" )
            cmds.button( label = "Validate" )
            cmds.setParent( ".." )
        cmds.text( label="" )
        cmds.text( label="" )
        cmds.text( label="" )
        cmds.setParent( ".." )
    cmds.setParent( ".." )

我们创建的上下文管理器neat_indent(),还使您有机会编写包装缩进块的代码。这里的一个实际例子是,在每个缩进的结尾处,您都会发现自己在写作py.setParent("..")您可以将其放入finally上下文管理器部分:

from contextlib import contextmanager
@contextmanager
def neat_indent(parent=None):
    # OPTIONAL: This is also an opportunity to do something before the block of code runs!
    try:
        # During this is where your indented block will execute
        # Leave it empty
        yield
    finally:
        # OPTIONAL: This is where you can write code that executes AFTER your indented block executes.
        if parent:
            cmds.setParent(parent)

您的代码现在更有意义了:

cmds.rowColumnLayout( numberOfColumns = 5 )
with neat_indent(".."):
    cmds.text( label="", width = 1 )
    cmds.text( label="Column 1", enable = False, width = 250 )
    cmds.text( label="", width = 1 )
    cmds.text( label="Column 2", enable = False, width = 250 )
    cmds.text( label="", width = 1 )

    cmds.text( label="" )

    cmds.rowColumnLayout( numberOfColumns = 4 )
    with neat_indent(".."):
        cmds.text( label="   Input data:", align="left" )
        cmds.text( label="" )
        cmds.text( label="" )
        cmds.text( label="" )
        cmds.textField( text = "Text here" )
        cmds.text( label="" )
        cmds.text( label="" )
        cmds.text( label="" )        

    cmds.text( label="" )
    cmds.rowColumnLayout( numberOfColumns = 4 )
    with neat_indent(".."):
        cmds.rowColumnLayout( numberOfColumns = 5 )
        with neat_indent(".."):
            cmds.radioButton( label = "Read file from path", width = 100 )
            cmds.text( label="" )
            cmds.button( label = "Browse" )
            cmds.text( label="" )
            cmds.button( label = "Validate" )

        cmds.text( label="" )
        cmds.text( label="" )
        cmds.text( label="" )

上下文管理器功能强大。在这篇文章中,我使用了标准库模块中contextmanager装饰器contextlib您可以在此处阅读有关此技术的信息关于这里with一般

同样,出于这个目的(目的之一),使Maya中的UI开发更加清洁,并且使用Pythonic @theodox创建了mGui模块。一探究竟。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

有没有一种方法可以直接“装饰” Python代码块?

来自分类Dev

有没有一种方法可以直接“装饰” Python代码块?

来自分类Dev

有没有一种方法可以取消从Internet下载的html文档的影响

来自分类Dev

Python中有没有一种方法可以创建一个for循环,而该循环不会等待内部代码完成再进行迭代?

来自分类Dev

有没有一种方法会影响img的位置,但不会影响锚元素中的源?

来自分类Dev

有没有一种方法可以使代码分析忽略“ InternalsVisibleTo”?

来自分类Dev

有没有一种方法可以在原始代码中插入原始javascript?

来自分类Dev

有没有一种方法可以从C ++代码添加包含目录?

来自分类Dev

有没有一种方法可以优化Spark sql代码?

来自分类Dev

有没有一种方法可以重用mongodb / mongose聚合代码?

来自分类Dev

有没有一种方法可以防止特定代码行合并?

来自分类Dev

有没有一种方法可以真正测试Haskell代码的性能?

来自分类Dev

有没有一种方法可以缩短我的SQL Server代码

来自分类Dev

有没有一种方法可以使代码分析忽略“ InternalsVisibleTo”?

来自分类Dev

在AFNetworking 2.0中,有没有一种方法可以简化这样的代码?

来自分类Dev

有没有一种方法可以更实用地编写此代码?

来自分类Dev

触发Pry时,有没有一种方法可以自动执行代码?

来自分类Dev

有没有一种方法可以从代码中调用GoogleSpreadSheet插件?

来自分类Dev

Postgres:有没有一种方法可以在INSERT语句之后执行代码?

来自分类Dev

有没有一种方法可以在php中优化此mysql代码?

来自分类Dev

Python-有没有一种方法可以简化网络抓取代码?

来自分类Dev

有没有一种方法可以更改SSID密码而不影响所有连接的设备?

来自分类Dev

Python模板,有没有一种方法可以从多个html文件继承?

来自分类Dev

有没有一种方法可以在Python中禁用数组边界检查?

来自分类Dev

有没有一种方法可以在Python中使用if语句生成“ Not a number”?

来自分类Dev

Python:有没有一种方法可以漂亮地打印列表?

来自分类Dev

有没有一种方法可以检查函数是否在python中是递归的?

来自分类Dev

有没有一种方法可以在Windows上的python中运行命令?

来自分类Dev

有没有一种方法可以比较python中枚举的内容?

Related 相关文章

  1. 1

    有没有一种方法可以直接“装饰” Python代码块?

  2. 2

    有没有一种方法可以直接“装饰” Python代码块?

  3. 3

    有没有一种方法可以取消从Internet下载的html文档的影响

  4. 4

    Python中有没有一种方法可以创建一个for循环,而该循环不会等待内部代码完成再进行迭代?

  5. 5

    有没有一种方法会影响img的位置,但不会影响锚元素中的源?

  6. 6

    有没有一种方法可以使代码分析忽略“ InternalsVisibleTo”?

  7. 7

    有没有一种方法可以在原始代码中插入原始javascript?

  8. 8

    有没有一种方法可以从C ++代码添加包含目录?

  9. 9

    有没有一种方法可以优化Spark sql代码?

  10. 10

    有没有一种方法可以重用mongodb / mongose聚合代码?

  11. 11

    有没有一种方法可以防止特定代码行合并?

  12. 12

    有没有一种方法可以真正测试Haskell代码的性能?

  13. 13

    有没有一种方法可以缩短我的SQL Server代码

  14. 14

    有没有一种方法可以使代码分析忽略“ InternalsVisibleTo”?

  15. 15

    在AFNetworking 2.0中,有没有一种方法可以简化这样的代码?

  16. 16

    有没有一种方法可以更实用地编写此代码?

  17. 17

    触发Pry时,有没有一种方法可以自动执行代码?

  18. 18

    有没有一种方法可以从代码中调用GoogleSpreadSheet插件?

  19. 19

    Postgres:有没有一种方法可以在INSERT语句之后执行代码?

  20. 20

    有没有一种方法可以在php中优化此mysql代码?

  21. 21

    Python-有没有一种方法可以简化网络抓取代码?

  22. 22

    有没有一种方法可以更改SSID密码而不影响所有连接的设备?

  23. 23

    Python模板,有没有一种方法可以从多个html文件继承?

  24. 24

    有没有一种方法可以在Python中禁用数组边界检查?

  25. 25

    有没有一种方法可以在Python中使用if语句生成“ Not a number”?

  26. 26

    Python:有没有一种方法可以漂亮地打印列表?

  27. 27

    有没有一种方法可以检查函数是否在python中是递归的?

  28. 28

    有没有一种方法可以在Windows上的python中运行命令?

  29. 29

    有没有一种方法可以比较python中枚举的内容?

热门标签

归档