压缩pyqtproperties [英] condense pyqtproperties

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

问题描述

我正在用Python编写脚本,有一个问题:

I'm writing a script in Python and have a bit of a problem:

class LightDMUser(QObject):
  def __init__(self, user):
    super(LightDMUser, self).__init__()
    self.user = user

  @pyqtProperty(QVariant)
  def background(self):      return self.user.get_background()

  @pyqtProperty(QVariant)
  def display_name(self):    return self.user.get_display_name()

  @pyqtProperty(QVariant)
  def has_messages(self):    return self.user.get_has_messages()

  @pyqtProperty(QVariant)
  def home_directory(self):  return self.user.get_home_directory()

  @pyqtProperty(QVariant)
  def image(self):           return self.user.get_image()

  @pyqtProperty(QVariant)
  def language(self):        return self.user.get_language()

  @pyqtProperty(QVariant)
  def layout(self):          return self.user.get_layout()

  @pyqtProperty(QVariant)
  def layouts(self):         return self.user.get_layouts()

  @pyqtProperty(QVariant)
  def logged_in(self):       return self.user.get_logged_in()

  @pyqtProperty(QVariant)
  def name(self):            return self.user.get_name()

  @pyqtProperty(QVariant)
  def real_name(self):       return self.user.get_real_name()

  @pyqtProperty(QVariant)
  def session(self):         return self.user.get_session()

如您所见,这段代码非常多余。我试着像这样压缩它:

As you can see, this code is horribly redundant. I tried condensing it like this:

class LightDMUser(QObject):
  attributes = ['background', 'display_name', 'has_messages', 'home_directory', 'image', 'language', 'layout', 'layouts', 'logged_in', 'name', 'real_name', 'session']

  def __init__(self, user):
    super(LightDMUser, self).__init__()
    self.user = user

    for attribute in self.attributes:
      setattr(self, attribute, pyqtProperty(QVariant, getattr(self.user, 'get_' + attribute)))



但是,

PyQt4希望类方法针对类本身而不是实例。将 setattr 代码移出 __ init __ 块也不起作用,因为 self ,所以我真的不知道该怎么做。

PyQt4, however, expects the class methods to be present for the class itself, not an instance. Moving the setattr code out of the __init__ block didn't work either because self wasn't defined for the class, so I don't really know what to do.

有人可以看到浓缩此代码的方法吗?

Can anyone see a way to condense this code?

推荐答案

有很多方法可以做到:类装饰器,元类和Mixin。

There are number of ways to do it: class decorator, metaclass, Mixin.

公共帮助功能:

def set_pyqtproperties(klass, properties, proxy='user'):
    def make_prop(prop):        
        def property_(self):
            return getattr(getattr(self, proxy), 'get_' + prop)
        property_.__name__ = prop
        return property_

    if isinstance(properties, basestring):
       properties = properties.split()
    for prop in properties:
         setattr(klass, prop, pyqtProperty(QVariant, make_prop(prop)))



类修饰器



Class decorator

def set_properties(properties):
    def decorator(klass):
        set_pyqtproperties(klass, properties)
        return klass
    return decorator



用法

Usage

@set_properties("display background")
class LightDMUser(QObject): pass

如果不支持类修饰符,则可以尝试:

if there is no support for class decorators then you could try:

class LightDMUser(QObject): 
    pass
LightDMUser = set_properties("display background")(LightDMUser)



元类



Metaclass

def set_properties_meta(properties):
    def meta(name, bases, attrs):
        cls = type(name, bases, attrs)
        set_pyqtproperties(cls, properties)
        return cls
    return meta



用法

Usage

class LightDMUser(QObject):
    __metaclass__ =  set_properties_meta("display background")

注意:如果将属性列表设置为类属性,则可以重用同一元类:

Note: you could reuse the same metaclass if you set the list of properties as a class attribute:

def MetaClass(name, bases, attrs):
    cls = type(name, bases, attrs)
    set_pyqtproperties(cls, attrs.get('properties', ''))
    return cls

class LightDMUser(QObject):
    properties = "display background"
    __metaclass__ = MetaClass

也可以直接操作 attrs attrs [name] = value ,然后调用 type()而不是 setattr(cls,name,value)

Also you could manipulate attrs directly: attrs[name] = value before calling type() instead of setattr(cls, name, value).

以上假设 QObject .__ class__是类型

def properties_mixin(classname, properties):
    #note: create a new class by whatever means necessary
    # e.g., even using exec() as namedtuple does
    # http://hg.python.org/cpython/file/3.2/Lib/collections.py#l235

    # reuse class decorator here
    return set_properties(properties)(type(classname, (), {}))



用法

Usage

PropertiesMixin = properties_mixin('PropertiesMixin', 'display background')
class LightDMUser(PropertiesMixin, QObject): pass

我还没有尝试过。这里的代码显示了实现该功能可能需要的数量和代码种类。

I haven't tried any of it. The code is here to show the amount and the kind of code it might require to implement the feature.

这篇关于压缩pyqtproperties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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