📄 friends.py
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -