Tags:
Forums: 

จากส่วนหนึ่งของ ไลบรารี่ pymsn ครับ


class Contact(gobject.GObject):
"""Contact related information
@undocumented: gsignals, gproperties, do_get_property"""

__gsignals__ =  {
        "infos-changed": (gobject.SIGNAL_RUN_FIRST,
            gobject.TYPE_NONE,
            (object,)),
        }

__gproperties__ = {
        "memberships": (gobject.TYPE_UINT,
            "Memberships",
            "Membership relation with the contact.",
            0, 15, 0, gobject.PARAM_READABLE),

        "display-name": (gobject.TYPE_STRING,
            "Friendly name",
            "A nickname that the user chooses to display to others",
            "",
            gobject.PARAM_READWRITE),

        "personal-message": (gobject.TYPE_STRING,
            "Personal message",
            "The personal message that the user wants to display",
            "",
            gobject.PARAM_READABLE),

        "current-media": (gobject.TYPE_PYOBJECT,
            "Current media",
            "The current media that the user wants to display",
            gobject.PARAM_READABLE),

        "presence": (gobject.TYPE_STRING,
            "Presence",
            "The presence to show to others",
            Presence.OFFLINE,
            gobject.PARAM_READABLE),

         "groups": (gobject.TYPE_PYOBJECT,
             "Groups",
             "The groups the contact belongs to",
             gobject.PARAM_READABLE),

        "infos": (gobject.TYPE_PYOBJECT,
            "Informations",
            "The contact informations",
            gobject.PARAM_READABLE),

        "contact-type": (gobject.TYPE_PYOBJECT,
            "Contact type",
            "The contact automatic update status flag",
             gobject.PARAM_READABLE),

        "client-capabilities": (gobject.TYPE_UINT64,
            "Client capabilities",
            "The client capabilities of the contact 's client",
            0, 0xFFFFFFFF, 0,
            gobject.PARAM_READABLE),

        "msn-object": (gobject.TYPE_STRING,
            "MSN Object",
            "MSN Object attached to the contact, this generally represent "
            "its display picture",
            "",
            gobject.PARAM_READABLE),
        }

def __init__(self, id, network_id, account, display_name, cid=None,
        memberships=Membership.NONE, contact_type=ContactType.REGULAR):
    """Initializer"""
    gobject.GObject.__init__(self)
    self._id = id
    self._cid = cid or "00000000-0000-0000-0000-000000000000"
    self._network_id = network_id
    self._account = account

    self._display_name = display_name
    self._presence = Presence.OFFLINE
    self._personal_message = ""
    self._current_media = None
    self._groups = set()

    self._memberships = memberships
    self._contact_type = contact_type
    self._client_capabilities = ClientCapabilities()
    self._msn_object = None
    self._infos = {}
    self._attributes = {'icon_url' : None}

def __repr__(self):
    def memberships_str():
        m = []
        memberships = self._memberships
        if memberships & Membership.FORWARD:
            m.append('FORWARD')
        if memberships & Membership.ALLOW:
            m.append('ALLOW')
        if memberships & Membership.BLOCK:
            m.append('BLOCK')
        if memberships & Membership.REVERSE:
            m.append('REVERSE')
        if memberships & Membership.PENDING:
            m.append('PENDING')
        return " | ".join(m)
    template = "<pymsn.Contact id='%s' network='%u' account='%s' memberships='%s'>"
    return template % (self._id, self._network_id, self._account, memberships_str())

@property
def id(self):
    """Contact identifier in a GUID form
        @type: GUID string"""
    return self._id

@property
def attributes(self):
    """Contact attributes
        @type: {key: string => value: string}"""
    return self._attributes.copy()

@property
def cid(self):
    """Contact ID
        @type: GUID string"""
    return self._cid

@property
def network_id(self):
    """Contact network ID
        @type: L{NetworkID<pymsn.profile.NetworkID>}"""
    return self._network_id

@property
def account(self):
    """Contact account
        @type: utf-8 encoded string"""
    return self._account

@property
def presence(self):
    """Contact presence
        @type: L{Presence<pymsn.profile.Presence>}"""
    return self._presence

@property
def display_name(self):
    """Contact display name
        @type: utf-8 encoded string"""
    return self._display_name

@property
def personal_message(self):
    """Contact personal message
        @type: utf-8 encoded string"""
    return self._personal_message

@property
def current_media(self):
    """Contact current media
        @type: (artist: string, track: string)"""
    return self._current_media

@property
def groups(self):
    """Contact list of groups
        @type: set(L{Group<pymsn.profile.Group>}...)"""
    return self._groups

@property
def infos(self):
    """Contact informations
        @type: {key: string => value: string}"""
    return self._infos

@property
def memberships(self):
    """Contact membership value
        @type: bitmask of L{Membership<pymsn.profile.Membership>}s"""
    return self._memberships

@property
def contact_type(self):
    """Contact automatic update status flag
        @type: L{ContactType<pymsn.profile.ContactType>}"""
    return self._contact_type

@property
def client_capabilities(self):
    """Contact client capabilities
        @type: L{ClientCapabilities}"""
    return self._client_capabilities

@property
def msn_object(self):
    """Contact MSN Object
        @type: L{MSNObject<pymsn.p2p.MSNObject>}"""
    return self._msn_object

@property
def domain(self):
    """Contact domain, which is basically the part after @ in the account
        @type: utf-8 encoded string"""
    result = self._account.split('@', 1)
    if len(result) > 1:
        return result[1]
    else:
        return ""

@property
def profile_url(self):
    """Contact profile url
        @type: string"""
    account = self._account
    return "http://members.msn.com/default.msnw?mem=%s&pgmarket=" % account

### membership management
def is_member(self, memberships):
    """Determines if this contact belongs to the specified memberships
        @type memberships: bitmask of L{Membership<pymsn.profile.Membership>}s"""
    return (self.memberships & memberships) == memberships

def _set_memberships(self, memberships):
    self._memberships = memberships
    self.notify("memberships")

def _add_membership(self, membership):
    self._memberships |= membership
    self.notify("memberships")

def _remove_membership(self, membership):
    """removes the given membership from the contact

        @param membership: the membership to remove
        @type membership: int L{Membership}"""
    self._memberships ^= membership
    self.notify("memberships")

def _server_property_changed(self, name, value): #FIXME, should not be used for memberships
    if name == "client-capabilities":
        value = ClientCapabilities(client_id=value)
    attr_name = "_" + name.lower().replace("-", "_")
    old_value = getattr(self, attr_name)
    if value != old_value:
        setattr(self, attr_name, value)
        self.notify(name)

def _server_attribute_changed(self, name, value):
    self._attributes[name] = value

def _server_infos_changed(self, updated_infos):
    self._infos.update(updated_infos)
    self.emit("infos-changed", updated_infos)
    self.notify("infos")

### group management
def _add_group_ownership(self, group):
    self._groups.add(group)

def _delete_group_ownership(self, group):
    self._groups.discard(group)

def do_get_property(self, pspec):
    name = pspec.name.lower().replace("-", "_")
    return getattr(self, name)

gobject.type_register(Contact)

class Group(gobject.GObject):
"""Group
@undocumented: gsignals, gproperties, do_get_property"""

__gproperties__ = {
    "name": (gobject.TYPE_STRING,
             "Group name",
             "Name that the user chooses for the group",
             "",
             gobject.PARAM_READABLE)
    }

def __init__(self, id, name):
    """Initializer"""
    gobject.GObject.__init__(self)
    self._id = id
    self._name = name

@property
def id(self):
    """Group identifier in a GUID form
        @type: GUID string"""
    return self._id

@property
def name(self):
    """Group name
        @type: utf-8 encoded string"""
    return self._name

def _server_property_changed(self, name, value):
    attr_name = "_" + name.lower().replace("-", "_")
    old_value = getattr(self, attr_name)
    if value != old_value:
        setattr(self, attr_name, value)
        self.notify(name)

def do_get_property(self, pspec):
    name = pspec.name.lower().replace("-", "_")
    return getattr(self, name)

gobject.type_register(Group)

คำถามแรกนะครับคือ @property คืออะไรหน่ะครับ
คำถามที่สองอ่ะครับ คือผมเรียกใช้ คลาส contact เรียกใช้ contact.domain / contact.contact_tpye / contact.client_capabilities/ contact.msn_object/ เรียกใช้ได้ปกติ แต่ ทำไมผมเรียกใช้ contact.profile_url ไม่ได้ ไม่ทราบว่าผมต้องเข้าใจอะไรตรงไหนก่อนไหมครับ ขึ้น error ว่า
"print contact.profile_url
AttributeError: 'Contact' object has no attribute 'profile_url'" เจอปัญหานี่ เล่นเอา อึ้งเลยครับ

ขอบคุณครับ

Get latest news from Blognone
By: KnightBaron
ContributoriPhoneAndroidRed Hat
on 11 February 2010 - 21:45 #156543
KnightBaron's picture
  1. เรียกว่า decorator ครับ ลอง Google รายละเอียดดูนะครับ
  2. profile_url มันเป็น method นี่ครับ ก็ต้องใช้ "print contact.profile_url()"