访问 Python 类的函数子集

发布于 2024-08-20 15:40:32 字数 675 浏览 7 评论 0原文

使用具有 xmlrpc 代理作为其对象属性之一的类,

def __init__(self):
    self.proxy = ServerProxy(...)
    # ...

我试图简化某些代理功能的使用。应该只使用代理函数的子集,因此我想到为它们创建一组微小的包装函数,例如

def sample(self):
    """ A nice docstring for a wrapper function. """
    self.proxy.sample()

是否有一个好的方法来获取所有包装函数的列表?我正在考虑类似 dir() 的东西,但随后我需要过滤对象的包装函数。 xmlrpc 内省 (http://xmlrpc-c.sourceforge.net/introspection.html )也没有多大帮助,因为我不想使用/提供服务器的所有功能。

也许在包装器上设置一个属性和 @staticmethod get_wrappers() 可以解决问题。有 _wrapper 后缀不适合我的用例。类中跟踪可用的静态列表太容易出错。所以我正在寻找关于如何最好地获取包装函数列表的好主意?

Using a class that has an xmlrpc proxy as one of it's object's properties

def __init__(self):
    self.proxy = ServerProxy(...)
    # ...

I'm trying to ease the use of some of the proxy's functions. Only a subset of the proxy functions are supposed to be used and I thus thought of creating a set of tiny wrapper functions for them like

def sample(self):
    """ A nice docstring for a wrapper function. """
    self.proxy.sample()

Is there a good way of getting a list of all the wrapper functions? I'm thinking about something like dir(), but then I would need to filter for the object's wrapper functions. xmlrpc introspection (http://xmlrpc-c.sourceforge.net/introspection.html) doesn't help much either since I don't want to use/ provide all the server's functions.

Maybe setting an attribute on the wrappers together with a @staticmethod get_wrappers() would do the trick. Having a _wrapper suffix is not appropriate for my use case. A static list in the class that keeps track of the available is too error prone. So I'm looking for good ideas on how to best getting a list of the wrapper functions?

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

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

发布评论

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

评论(2

℡Ms空城旧梦 2024-08-27 15:40:32

我不确定这是否是您想要的,但它有效:

def proxy_wrapper(name, docstring):
    def wrapper(self, *args, **kwargs):
        return self.proxy.__getattribute__(name)(*args, **kwargs)
    wrapper.__doc__ = docstring
    wrapper._is_wrapper = True
    return wrapper

class Something(object):
    def __init__(self):
        self.proxy = {}

    @classmethod
    def get_proxy_wrappers(cls):
        return [m for m in dir(cls) if hasattr(getattr(cls, m), "_is_wrapper")]

    update = proxy_wrapper("update", "wraps the proxy's update() method")
    proxy_keys = proxy_wrapper("keys", "wraps the proxy's keys() method")    

然后

>>> a = Something()
>>> print a.proxy
{}
>>> a.update({1: 42})
>>> print a.proxy
{1: 42}
>>> a.update({"foo": "bar"})
>>> print a.proxy_keys()
[1, 'foo']
>>> print a.get_proxy_wrappers()
['proxy_keys', 'update']

I'm not 100% sure if this is what you want, but it works:

def proxy_wrapper(name, docstring):
    def wrapper(self, *args, **kwargs):
        return self.proxy.__getattribute__(name)(*args, **kwargs)
    wrapper.__doc__ = docstring
    wrapper._is_wrapper = True
    return wrapper

class Something(object):
    def __init__(self):
        self.proxy = {}

    @classmethod
    def get_proxy_wrappers(cls):
        return [m for m in dir(cls) if hasattr(getattr(cls, m), "_is_wrapper")]

    update = proxy_wrapper("update", "wraps the proxy's update() method")
    proxy_keys = proxy_wrapper("keys", "wraps the proxy's keys() method")    

Then

>>> a = Something()
>>> print a.proxy
{}
>>> a.update({1: 42})
>>> print a.proxy
{1: 42}
>>> a.update({"foo": "bar"})
>>> print a.proxy_keys()
[1, 'foo']
>>> print a.get_proxy_wrappers()
['proxy_keys', 'update']
许久 2024-08-27 15:40:32

使用 xml-rpc 自省来获取服务器列表并将其与对象的属性相交。像这样的东西:

loc = dir(self)
rem = proxy.listMethods() # However introspection gets a method list
wrapped = [x for x in rem if x in loc]

Use xml-rpc introspection to get the server list and intersect it with your object's properties. Something like:

loc = dir(self)
rem = proxy.listMethods() # However introspection gets a method list
wrapped = [x for x in rem if x in loc]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文