调度字典,而是传递不同的参数给函数 [英] Dispatch dictionary but pass different parameters to functions

查看:145
本文介绍了调度字典,而是传递不同的参数给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用派遣模式在这样的蟒蛇....

I'm using the dispatch pattern in python like this....

    ok = parse_qsl(urlparse(u).query, keep_blank_values=True)

    eventType = (ok[9][1])
    nName = (ok[10][1])
    hName = (ok[11][1])
    cName = (ok[12][1])

    def newChannel(cName):
        queue = j.queue(cName)
        r = queue.add_subscribers(*[subscriberCreateChanTable, subscriberSortScenes])

    def newNetwork(hName):
        queue = j.queue(hName)
        r = queue.add_subscribers(*[subscriber1a])

    def loginError(nName):
        pass

    action = {'newChannel': newChannel,
             'newNetwork': newNetwork , 'loginError': loginError}

    handler = action.get(eventType)
    handler(cname)

我如何通过不同的功能参数映射功能
所以,如果EVENTTYPE =newChannel,然后newChannel函数将调用与CNAME,
但如果EVENTTYPE =newNetwork,我怎么跟hname变量调用它来代替。

How do I pass different function parameters to the mapped function so if eventType = "newChannel", then newChannel function will get called with cname, but if eventType = "newNetwork", how do i call it with hname variable instead.

只是调用处理器(hname)?

just call handler(hname)?

    handler(hname)
    handler(cname)
    handler(nName)

推荐答案

您可以使用lambda或部分或只是存储在字典中PARAMS太:

You can either use a lambda or partial or just store the params in the dict too:

action = {'newChannel': (newChannel, hname),
             'newNetwork': (newNetwork, cname) , 'loginError': (loginError, nName)}

handler, param = action.get(eventType)
handler(param)

现在它仍然意味着你必须建立动作对每个请求。应避免此另一种方案是写的PARAMS和商店消气(处理器,吸气)对:

Now it still means you have to build action on each request. Another solution that avoids this is to write "getter" for the params and store (handler, getter) pairs:

def newChannel(cName):
    queue = j.queue(cName)
    r = queue.add_subscribers(*[subscriberCreateChanTable, subscriberSortScenes])

def newNetwork(hName):
    queue = j.queue(hName)
    r = queue.add_subscribers(*[subscriber1a])

def loginError(nName):
    pass

def hName(ok):
    return ok[11][1]

def cName(ok):
    return ok[12][1]

def nName(ok):
    return ok[10][1]

def eventType(ok):
    return ok[9][1]


action = {
    'newChannel': (newChannel, cName),
    'newNetwork': (newNetwork, hName),
    'loginError': (loginError, nName)
     }


ok = parse_qsl(urlparse(u).query, keep_blank_values=True)
handler, getter = action.get(eventType(ok))
handler(getter(ok))

lambda表达式使用同样的例子:

The same example using lambdas:

action = {
    'newChannel': lambda ok: newChannel(cName(ok)),
    'newNetwork': lambda ok: newNetwork(hName(ok)),
    'loginError': lambda ok: loginError(nName(ok))
     }

ok = parse_qsl(urlparse(u).query, keep_blank_values=True)

handler = action.get(eventType(ok))
handler(ok)

在这种情况下,它只是使少明确code和无用开销恕我直言,但有时一个基于拉姆达的解决方案,让你捕捉一些额外的背景下,这不是可在您定义等功能点。

In this case it just makes for less explicit code and useless overhead IMHO, but sometimes a lambda-based solution let you capture some additional context that's not available at the point you define your other functions.

或者你可以做的lambda表达式的参数解析自己,即:

Or you could have done the param parsing in the lambdas themselves, ie :

action = {
    'newChannel': lambda ok: newChannel(ok[12][1]),
    'newNetwork': lambda ok: newNetwork(ok[11][1]),
    'loginError': lambda ok: loginError(ok[10][1])
     }

但仍比使用普通的功能不明确(少可测试)。

but that's still less explicit (and less testable) than using plain functions.

这篇关于调度字典,而是传递不同的参数给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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