与Python的流畅界面 [英] Fluent interface with Python

查看:90
本文介绍了与Python的流畅界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python函数 send_message,该函数带有三个参数:

I have a Python function "send_message" which takes three arguments:

send_message("i like windmills", to="INBOX", from="OUTBOX")

我正在考虑将流利的界面放在它。理想情况下,我想编写以下代码:

I am thinking about putting a fluent interface on top of it. Ideally I'd like to write the following:

send_message("i like windmills").to("INBOX").from("OUTBOX")

send_message("i like windmills").from("OUTBOX").to("INBOX")

更新:
to()信息是强制性的,但 from()不是(与真实字母一样)。因此,这也将是一个有效的调用:

Update: The to() information is mandatory but the from() is not (as with real letters). So this one would also be a valid call:

send_message("i like windmills").to("INBOX")

任何想法如何实现这一目标或类似目标?

Any ideas how to accomplish this or something similar?

让我理解具有使对象的方法返回自身的方法的一般方法,但根据我的理解,这将导致以下情况:

The general approach of having methods of an object returning "self" is understood by me but in my understanding this would lead to something like this:

message = Message("i like windmills")
message.to("INBOX").from("OUTBOX").send()

但是这个不如前面的示例好,所以我实际上更喜欢原始版本

But this one is not as nice as the previous example and I then would actually prefer the original version with the named arguments.

不胜感激。

推荐答案

可以通过这种方式完成,我不确定是否有更好的方法,因为这是我的第一次尝试。

It can be accomplished this way, I am unsure if there is a better way because this is my first attempt. Good luck!

DEFAULT_SENDER = 'my_address'
#Because the sender object is optional I assume you have a default sender

class Send_message(object):
    def __init__(self, message):
        self.message = message
        self.sender = None
        self.receiver = None
        self.method = None

    def to(self, receiver):
        self.receiver = receiver
        self.method = self.send()
        return self

    def _from(self, sender):
        self.sender = sender
        self.method = self.send()
        return self

    def __call__(self):
        if self.method:
            return self.method()
        return None

    def send(self):
        if self.receiver:
            if not self.sender:
                self.sender = DEFAULT_SENDER

            return lambda:actual_message_code(self.message, self.sender, self.receiver)


def actual_message_code(message, sender, receiver):
    print "Sent '{}' from: {} to {}.".format(message, sender, receiver)



Send_message("Hello")._from('TheLazyScripter').to('samba2')()
Send_message("Hello").to('samba2')._from('TheLazyScripter')()
Send_message("Hello").to('samba2')()

#Only change in actual calling is the trailing ()

通过实现 __ call __ 方法,我们可以知道何时到达调用链的末尾。当然,这会添加尾随()调用。并要求您更改指向实际消息传递方法的指针和默认的sender变量,但是我认为这是实现目标的最简单方法,而无需实际知道链何时结束。

By implementing the __call__ method we can tell the when we are at the end of the call chain. This of course adds the trailing () call. and requires you to change the pointer to your actual messaging method and default sender variable but I feel that this would be the simplest way to accomplish your goals without actually knowing when the chain ends.

这篇关于与Python的流畅界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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