friends.py

来自「pyLJclient是一个跨平台的livejournal客户端」· Python 代码 · 共 43 行

PY
43
字号
import string
import types
import friend

class Friends:
    """ the Friends class takes a properly formatted response string from lj, and
    tries to extract friends information from it.  Each friend is stored in a
    list called friends.  Currently, the proper format for the response string
    from lj is that the first line is a key, and the next line is the value.
    thus, key value pairs can be extracted and mapped."""
    
    def __init__(self, lj_response_string=''):
        self.friends = []
        if lj_response_string:
            self._hash_response(lj_response_string)
            self._get_friends()

    def _hash_response(self, response_string):
        split_response = string.split(response_string, '\n')
        temp_hash={}
        for i in range(0, len(split_response), 2):
            if split_response[i] == '':
                break
            temp_hash[split_response[i]]=split_response[i+1]
        #the basic_hash puts key value pairs from lj response string into
        #a dictionary (hash)
        self.basic_hash = temp_hash

    def _get_friends(self):
        self.friends_hash ={}
        for key in self.basic_hash.keys():
            key_tokens = string.split(key, '_')
            if (len(key_tokens) == 3) and (type(int(key_tokens[1])) ==
                                           types.IntType):
                (kind, count, name) = key_tokens
                #process friends
                if not self.friends_hash.has_key(count):
                    self.friends_hash[count] = []
                self.friends_hash[count].append((name, self.basic_hash[key]))
        for a_friend in self.friends_hash.keys():
            self.friends.append(friend.Friend(self.friends_hash[a_friend]))
            

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?