使用适配器模式和__slots__优化代码小记

今天看了一篇关于设计模式方面的资料,再加上前几天看的 __slots__ 的用法,想起项目中的更新用户资料相关代码可以用上边的知识(适配器模式, __slots____setattr__)优化一下:

修改前:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class UserModel(object):
def __init__(self, user_id=None):
self.user_id = user_id
self.query = Query(_User)
...
def update_profile(self, avatar=None, nickname=None, birth=None,
mind=None, height=None, weight=None, sexuality=None, emotion=None,
haunt=None, been=None, company=None, position=None, graduated=None,
industry=None, salary=None, hometown=None):
u = self.get_by_id()
user_profile = UserProflieModel.get_or_create_user_profile(u)
if avatar is not None:
avatar_file = Query(_File).get(avatar)
u.set('avatar', avatar_file)
if nickname is not None:
u.set('nickname', nickname)
if birth is not None:
birthday = datetime.fromtimestamp(birth)
u.set('birthday', birthday)
if mind is not None:
if len(mind) > 300:
raise LeanCloudError(20504, errmsg.ERRMSG[20504])
u.set('mind', mind)
if height is not None:
user_profile.set('height', height)
if weight is not None:
user_profile.set('weight', weight)
if sexuality is not None:
user_profile.set('sexuality', sexuality)
if emotion is not None:
user_profile.set('emotion', emotion)
if haunt is not None:
user_profile.set('haunt', haunt)
if been is not None:
user_profile.set('been', been)
if company is not None:
user_profile.set('company', company)
if position is not None:
user_profile.set('position', position)
if graduated is not None:
user_profile.set('graduated', graduated)
if industry is not None:
user_profile.set('industry', industry)
if salary is not None:
user_profile.set('salary', salary)
if hometown is not None:
region = Query(Region).equal_to('adcode', hometown).first()
user_profile.set('hometown', region)
try:
u.save()
user_profile.save()
except LeanCloudError as e:
logging.error(e)
return False
return True

修改后:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Account(object):
__slots__ = ('user_id', 'user', 'user_profile', 'account_money')
def __init__(self, user_id):
self.user_id = user_id
self.user = Query(_User).include('avatar.url', 'password').get(user_id)
self.user_profile = UserProflieModel.get_or_create_user_profile(self.user)
self.account_money = None
def __setattr__(self, key, value):
if key in self.__slots__:
object.__setattr__(self, key, value)
elif key in ('height', 'weight', 'sexuality', 'emotion', 'haunt', 'been', 'company',
'position', 'graduated', 'industry', 'salary', 'hometown') and value is not None:
if key == 'hometown':
value = Query(Region).equal_to('adcode', value).first()
self.user_profile.set(key, value)
elif key in ('avatar', 'nickname', 'birth', 'mind') and value is not None:
if key == 'avatar':
value = Query(_File).get(value)
elif key == 'birth':
value = datetime.fromtimestamp(value/1000)
elif key == 'mind':
if len(value) > 300:
raise LeanCloudError(20504, errmsg.ERRMSG[20504])
self.user.set(key, value)
def save(self):
self.user.save()
self.user_profile.save()
# self.account_money.save()
class UserModel(object):
def __init__(self, user_id=None):
self.user_id = user_id
self.query = Query(_User)
...
def update_profile(self, avatar=None, nickname=None, birth=None,
mind=None, height=None, weight=None, sexuality=None, emotion=None,
haunt=None, been=None, company=None, position=None, graduated=None,
industry=None, salary=None, hometown=None):
account = Account(self.user_id)
account.avatar = avatar
account.nickname = nickname
account.birth = birth
account.mind = mind
account.height = height
account.weight = weight
account.sexuality = sexuality
account.emotion = emotion
account.haunt = haunt
account.been = been
account.company = company
account.position = position
account.graduated = graduated
account.industry = industry
account.salary = salary
account.hometown = hometown
try:
account.save()
except LeanCloudError as e:
logging.error(e)
return False
return True

感觉自己萌萌哒 (。◕∀◕。)