您如何@rpc _返回spyne中的多态类型? [英] How do you @rpc _returns polymorphic types in spyne?

查看:623
本文介绍了您如何@rpc _返回spyne中的多态类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改

示例

class A(ComplexModel):
    Id = Unicode

class B(ComplexModel):
    __extends__ = A
    Name = Unicode

@rpc(String, _returns=A)
def hello(self, name):
    ret = B()
    B.Id = '123'
    B.Name = name
    return ret

您如何处理此行为,使其不返回A对象?

How do you handle this behavior so it doesn't return an object of A?

如何编写spyne装饰器以正确返回多个类型?例如,如果_returns设置为ZObj,则返回XAccount(如代码中一样)不会执行任何操作.

How would I write the spyne decorators to correctly return more than one type? If, for example, _returns is set to ZObj then returning an XAccount (like in the code) doesn't do anything.

我可以编写XAccount对象,使其扩展为ZObj,并且是有效的返回类型吗?

Can I write the XAccount object so that it extends ZObj and is a valid return type?

@rpc(String, _returns=(ZObj, XAccount))
def hello(self, name):
    acc = XAccount(
        user_name = name.upper(),
        first_name = name,
        last_name = 'last ' + name
    )
    return acc

类示例....

class ZObj(ComplexModel):
    id = Unicode(pattern='[a-zA-Z0-9]{32}|\d+')

class Account(DeclarativeBase):
    __tablename__ = 'account'

    id = Column(Integer, primary_key=True)
    user_name = Column(String(256))
    first_name = Column(String(256))
    last_name = Column(String(256))


class XAccount(TableModel):
    __table__ = Account.__table__

推荐答案

删除我以前的答案,因为您显然需要多态性,而不是多个返回类型.

Deleting my previous answer as you apparently need polymorphism, not multiple return types.

因此,在Spyne中有两种实现多态的方法:Python方法和Spyne方法.

So, There are two ways of doing polymorphism in Spyne: The Python way and the Spyne way.

让我们

class A(ComplexModel):
    i = Integer

class B(A):
    s = Unicode

class C(A):
    d = DateTime

Python方式使用鸭子输入来返回值.

The Python way uses duck typing to return values.

让我们定义一个通用类:

Let's define a generic class:

class GenericA(ComplexModel):
    i = Integer
    s = Unicode
    d = DateTime

并将其用作示例服务的返回值:

and use it as return value of our sample service:

class SomeService(ServiceBase):
    @rpc(Unicode(values=['A', 'B', 'C']), _returns=GenericA)
    def get_some_a(self, type_name):
        # (...)

通过这种方式,您可以获取数据,但会将其标记为GenericA对象.如果您对此不关心,则可以创建一个具有所有对象的所有类型的类(假设具有相同名称的属性具有相同的类型),然后就可以完成操作.这很容易,稳定并且可以在今天使用.

This way, you get your data, but it's tagged as a GenericA object. If you don't care about this, you can create a class that has all types from all objects (assuming attributes with same names have the same type) and just be done with it. This is easy, stable and works today.

如果这还不足以满足您的需求,则必须以Spyne方式进行多态.为此,首先将您的返回类型设置为基类:

If that's not enough for your needs, you have to do the polymorphism the Spyne way. To do that, first set your return type to the base class:

class SomeService(ServiceBase):
    @rpc(Unicode(values=['A', 'B', 'C']), _returns=A)
    def get_some_a(self, type_name):
        # (...)

并将您的输出协议标记为多态的:

and tag your output protocol to be polymorphic:

application = Application([SomeService], 'tns',
    in_protocol=Soap11(validator='lxml'),
    out_protocol=Soap11(polymorphic=True)
)

这至少需要Spyne-2.12.

This requires at least Spyne-2.12.

工作示例: https://github.com/arskom/spyne/blob/a1b3593f3754a9c8a6787c29ff50f591db89fd49/examples/xml/polymorphism.py

这篇关于您如何@rpc _返回spyne中的多态类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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