2012-09-26 73 views
-1
class SignedFileRequest(SignedRequest): 
    def __init__(self, host, path, node_id = None, name=None, \ 
       content_type=None, hash=None, ssl=False, expires=0): 
     super(SignedFileRequest, self).__init__(host, path, ssl, expires) 
     self.name = name 
     self.content_type = content_type 
     self.hash = hash 
     self.node_id = node_id 
    def get_name(self): 
     return self.query_dict.get(NAME_KEY) 
    def set_name(self, value): 
     self.query_dict[NAME_KEY] = value 
    def get_content_type(self): 
     return self.query_dict.get(CONTENT_TYPE_KEY) 
    def set_content_type(self, value): 
     self.query_dict[CONTENT_TYPE_KEY] = value 
    def get_hash(self): 
     return self.query_dict.get(HASH_KEY) 
    def set_hash(self, value): 
     self.query_dict[HASH_KEY] = value 
    def get_node_id(self): 
     return self.query_dict.get(NODE_ID_KEY) 
    def set_node_id(self, value): 
     self.query_dict[NODE_ID_KEY] = value 
    name = property(get_name, set_name) 
    content_type = property(get_content_type, set_content_type) 
    hash = property(get_hash, set_hash) 
    node_id = property(get_node_id, set_node_id) 
+0

該模式會是什麼?例如,請提供更多關於如何將屬性映射到'query_dict'鍵的詳細信息。 –

+0

可能的重複[任何人都可以幫助壓縮這個Python代碼嗎?](http://stackoverflow.com/questions/11921320/can-anyone-help-condense-this-python-code) –

回答

1

您可以使用__getattr____setattr__掛鉤,而不是一個元類:

class SignedFileRequest(SignedRequest): 
    __attr_map = dict(name=NAME_KEY, content_type=CONTENT_TYPE_KEY, 
         hash=HASH_KEY, node_id=NODE_ID_KEY) 

    def __init__(self, host, path, node_id = None, name=None, \ 
       content_type=None, hash=None, ssl=False, expires=0): 
     super(SignedFileRequest, self).__init__(host, path, ssl, expires) 
     self.name = name 
     self.content_type = content_type 
     self.hash = hash 
     self.node_id = node_id 

    def __getattr__(self, attr): 
     if attr in self.__attr_map: 
      return self.query_dict.get(self.__attr_map[attr]) 
     return super(SignedFileRequest, self).__getattr__(attr) 

    def __setattr__(self, attr, value): 
     if attr in self.__attr_map: 
      self.query_dict[self.__attr_map[attr]] = value 
     super(SignedFileRequest, self).__setattr__(attr, value) 

或者,相同的映射關係可用於創建一類裝飾工:

def add_property(klass, name, key): 
    def getter(self): 
     return self.query_dict.get(key) 
    def setter(self, value): 
     self.query_dict[key] = value 
    setattr(klass, name, property(getter, setter)) 

def set_properties(**mapping): 
    def decorator(klass): 
     for name, key in mapping.iteritems(): 
      add_property(klass, name, key) 
     return klass 
    return decorator 

@set_properties(name=NAME_KEY, content_type=CONTENT_TYPE_KEY, 
       hash=HASH_KEY, node_id=NODE_ID_KEY) 
class SignedFileRequest(SignedRequest): 
    def __init__(self, host, path, node_id = None, name=None, \ 
       content_type=None, hash=None, ssl=False, expires=0): 
     super(SignedFileRequest, self).__init__(host, path, ssl, expires) 
     self.name = name 
     self.content_type = content_type 
     self.hash = hash 
     self.node_id = node_id 

或者你可以用元類的方法去反正:

def mapped_properties_meta(**mapping): 
    def mapped_meta(name, bases, attrs): 
     klass = type(name, bases, attrs) 
     for name, key in mapping.iteritems(): 
      add_property(klass, name, key) 
     return klass 
    return mapped_meta 


class SignedFileRequest(SignedRequest): 
    __metaclass__ = mapped_properties_meta(
     name=NAME_KEY, content_type=CONTENT_TYPE_KEY, 
     hash=HASH_KEY, node_id=NODE_ID_KEY) 

    def __init__(self, host, path, node_id = None, name=None, \ 
       content_type=None, hash=None, ssl=False, expires=0): 
     super(SignedFileRequest, self).__init__(host, path, ssl, expires) 
     self.name = name 
     self.content_type = content_type 
     self.hash = hash 
     self.node_id = node_id 

後者重用從裝飾方法的add_property功能。

2

你可以使用一個類裝飾:

def keyprop(keymap): 
    def decorator(cls): 
     for name, key in keymap.items(): 
      def getter(self): 
       return self.query_dict.get(NAME_KEY) 
      def setter(self, value): 
       self.query_dict[NAME_KEY] = value 
      setattr(cls, name, property(getter, setter)) 
    return decorator 

keymap = {'name' : NAME_KEY, 
      'content_type' : CONTENT_TYPE_KEY, 
      'hash' : HASH_KEY, 
      'node_id' : NODE_ID_KEY} 

@keyprop(keymap) 
class SignedFileRequest(SignedRequest): 
    def __init__(self, host, path, node_id = None, name=None, \ 
       content_type=None, hash=None, ssl=False, expires=0): 
     super(SignedFileRequest, self).__init__(host, path, ssl, expires) 
     self.name = name 
     self.content_type = content_type 
     self.hash = hash 
     self.node_id = node_id 
+0

非常好的答案,你先回答。我認爲Martijn的回答有點完整。你介意我給他答案嗎? – Alexandre

+0

當然,請繼續;我會挺過來的 :) – unutbu