这个疯狂的循环和 if 序列是怎么回事?有改进这段代码的想法吗?

发布于 2024-10-02 18:21:38 字数 640 浏览 0 评论 0原文

这段代码目前正在运行,但看起来很糟糕 - 并且可能在性能方面可以大大提高。

有什么建议吗?

def OnClick():
    global Selection, touch, set_elsb, set_vreg, set_els, BAR_Items
    A = viz.pick(0, viz.WORLD, all=False)
    if touch != A: return
    for i in BAR_Items:
        if not set_els: break
        elif BAR_Items[i] == A or SHAPES[i+"_SHP"] == A:
            if i in Selection:
                Selection.remove(i)
                BAR_Items[i].clearActions()
                VFrame.SetStatusText(frame, i + " has been deselected")
                viz.director( do_chart )
            else:
                Selection.append(i)

非常感谢!

This code is currently working, but it looks terrible - and probably can be much improved in terms of performance.

Any Suggestions?

def OnClick():
    global Selection, touch, set_elsb, set_vreg, set_els, BAR_Items
    A = viz.pick(0, viz.WORLD, all=False)
    if touch != A: return
    for i in BAR_Items:
        if not set_els: break
        elif BAR_Items[i] == A or SHAPES[i+"_SHP"] == A:
            if i in Selection:
                Selection.remove(i)
                BAR_Items[i].clearActions()
                VFrame.SetStatusText(frame, i + " has been deselected")
                viz.director( do_chart )
            else:
                Selection.append(i)

Thank you very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

挽袖吟 2024-10-09 18:21:38

虽然多了几行代码,但我认为它更清晰了。

def OnClick():
     if not set_els: return

     # swap this with the line above if viz.pick has side effects that should occur 
     A = viz.pick(0, viz.WORLD, all=False) 
     if touch != A: return


     keys = (key for key in BAR_Items
             if BAR_Items[key] == A or SHAPES[key+"_SHP"] == A)

     for key in keys:
         if key in Selection:
             Selection.remove(key)
             BAR_Items[key].clearActions()
             VFrame.SetStatusText(frame, key + " has been deselected")
             viz.director(do_chart)
         else:
             Selection.append(key)

整个 global 语句没有任何作用,因为您没有分配给它们中的任何一个。调用属性和设置键不需要 global 关键字。

It's a few more lines of code but I think it's clearer.

def OnClick():
     if not set_els: return

     # swap this with the line above if viz.pick has side effects that should occur 
     A = viz.pick(0, viz.WORLD, all=False) 
     if touch != A: return


     keys = (key for key in BAR_Items
             if BAR_Items[key] == A or SHAPES[key+"_SHP"] == A)

     for key in keys:
         if key in Selection:
             Selection.remove(key)
             BAR_Items[key].clearActions()
             VFrame.SetStatusText(frame, key + " has been deselected")
             viz.director(do_chart)
         else:
             Selection.append(key)

That entire global statement served no purpose as you weren't assigning to any of them. Calling attributes and setting keys don't require the global keyword.

强辩 2024-10-09 18:21:38

我对此的正常方法是将其中一些重构为小方法。这通常使其更易于测试且更易于阅读。

My normal approach to this would be to re-factor some of it out into small methods. This generally makes it more testable and easier to read.

送舟行 2024-10-09 18:21:38

如果在此代码执行期间 set_els 未在外部更改,则:

def OnClick():
    global Selection, touch, set_elsb, set_vreg, set_els, BAR_Items
    if set_els: return
    A = viz.pick(0, viz.WORLD, all=False)
    if touch != A: return
    for i in BAR_Items:
        if not (BAR_Items[i] == A or SHAPES[i+"_SHP"] == A): continue
        if i in Selection:
            Selection.remove(i)
            BAR_Items[i].clearActions()
            VFrame.SetStatusText(frame, i + " has been deselected")
            viz.director( do_chart )
        else:
            Selection.append(i)

无论如何,当我的坏代码检测器看到这样的代码时,它会闪烁红灯,尤其是对于如此大量的全局变量。

If set_els is not changed outside during this code execution then:

def OnClick():
    global Selection, touch, set_elsb, set_vreg, set_els, BAR_Items
    if set_els: return
    A = viz.pick(0, viz.WORLD, all=False)
    if touch != A: return
    for i in BAR_Items:
        if not (BAR_Items[i] == A or SHAPES[i+"_SHP"] == A): continue
        if i in Selection:
            Selection.remove(i)
            BAR_Items[i].clearActions()
            VFrame.SetStatusText(frame, i + " has been deselected")
            viz.director( do_chart )
        else:
            Selection.append(i)

Anyway, my bad code detector flashes with red light when it sees such a code, especially with such amount of globals.

尐偏执 2024-10-09 18:21:38

人们普遍认为,如果代码丑陋、令人困惑或难以理解,那么它一定是低效的。

许多人还认为,如果想让代码运行得更快,就必须对其进行丑化。

我见过一些丑陋、令人困惑的代码,其中一些运行得非常快,而另一些则存在巨大的性能问题。

我也看到过干净、清晰、漂亮的代码,同样可以这么说。

我的经验——速度和美是独立的。

It is very common to assume that if code is ugly, confusing, or hard to follow, it must therefore be inefficient.

Many people also think that if you want to make code go faster, you have to uglify it.

I've seen way ugly confusing code, some of which ran very fast, and other of which had massive performance problems.

I've also seen clean, clear, beautiful code of which the same could be said.

My experience - speed and beauty are independent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文