词典或If语句,Jython [英] Dictionary or If statements, Jython

查看:98
本文介绍了词典或If语句,Jython的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻我正在编写一个脚本,它将使用dom4j从HTML捕获某些信息.

I am writing a script at the moment that will grab certain information from HTML using dom4j.

由于Python/Jython没有本机的 switch 语句,我决定使用一大堆 if 语句来调用适当的方法,如下所示:

Since Python/Jython does not have a native switch statement I decided to use a whole bunch of if statements that call the appropriate method, like below:

if type == 'extractTitle':
    extractTitle(dom)
if type == 'extractMetaTags':
    extractMetaTags(dom)

我将根据我想从HTML提取的信息添加更多信息,并考虑采用在本网站其他地方找到的字典方法,例如以下示例:

I will be adding more depending on what information I want to extract from the HTML and thought about taking the dictionary approach which I found elsewhere on this site, example below:

{
    'extractTitle':    extractTitle,
    'extractMetaTags': extractMetaTags
}[type](dom)

我知道每次运行脚本都会构建字典,但是同时如果我要使用 if 语句,脚本将必须检查所有脚本,直到找到为止击中正确的.我真正想知道的是,哪种性能更好或更普遍使用呢?

I know that each time I run the script the dictionary will be built, but at the same time if I were to use the if statements the script would have to check through all of them until it hits the correct one. What I am really wondering, which one performs better or is generally better practice to use?

更新:@Brian-感谢您的答复.我有一个问题,是否任何提取方法都需要多个对象,例如

Update: @Brian - Thanks for the great reply. I have a question, if any of the extract methods require more than one object, e.g.

handle_extractTag(self, dom, anotherObject)
# Do something

您如何对 handle 方法进行适当的更改以实现此目的?希望你明白我的意思:)

How would you make the appropriate changes to the handle method to implemented this? Hope you know what I mean :)

欢呼

推荐答案

为避免在dict中指定标签和处理程序,您可以只使用处理程序类,并为其命名以匹配类型的方法.例如

To avoid specifying the tag and handler in the dict, you could just use a handler class with methods named to match the type. Eg

class  MyHandler(object):
    def handle_extractTitle(self, dom):
        # do something

    def handle_extractMetaTags(self, dom):
        # do something

    def handle(self, type, dom):
        func = getattr(self, 'handle_%s' % type, None)
        if func is None:
            raise Exception("No handler for type %r" % type)
        return func(dom)

用法:

 handler = MyHandler()
 handler.handle('extractTitle', dom)

更新:

当您有多个参数时,只需更改handle函数以采用这些参数并将它们传递给该函数.如果您想使其更通用(因此在更改参数签名时不必同时更改处理函数和handle方法),则可以使用* args和** kwargs语法传递所有接收到的参数.然后handle方法变为:

When you have multiple arguments, just change the handle function to take those arguments and pass them through to the function. If you want to make it more generic (so you don't have to change both the handler functions and the handle method when you change the argument signature), you can use the *args and **kwargs syntax to pass through all received arguments. The handle method then becomes:

def handle(self, type, *args, **kwargs):
    func = getattr(self, 'handle_%s' % type, None)
    if func is None:
        raise Exception("No handler for type %r" % type)
    return func(*args, **kwargs)

这篇关于词典或If语句,Jython的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆