Skip to content
This repository has been archived by the owner on Jun 30, 2019. It is now read-only.

Commit

Permalink
改进 netease BaseModel 的 __getattribute__ 实现
Browse files Browse the repository at this point in the history
区分 None 与空列表/空字符串的区别:None 代表该字段没有
被初始化,空列表或者空字符串表示初始化了,但是数据内容为空。
  • Loading branch information
cosven committed Jul 29, 2018
1 parent 71cb443 commit b34219b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
12 changes: 12 additions & 0 deletions fuocore/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# -*- coding: utf-8 -*-

"""
fuocore.model
~~~~~~~~~~~~~
这个模块对音乐相关模型进行了定义,声明了各模型的属性。
"""

from enum import Enum


Expand Down Expand Up @@ -130,6 +137,11 @@ def __eq__(self, other):

@classmethod
def get(cls, identifier):
"""获取 Model 详细信息
NOTE: 字段值如果是 None 的话,说明之前这个字段没有被初始化过。
所以在调用 get 接口之后,需要将每个字段初始化为非 None。
"""
raise NotImplementedError

@classmethod
Expand Down
5 changes: 3 additions & 2 deletions fuocore/netease/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class Meta:
def __getattribute__(self, name):
cls = type(self)
value = object.__getattribute__(self, name)
if name in cls._detail_fields and not value:
if name in cls._detail_fields and value is None:
logger.debug('Field %s value is None, get model detail first.')
obj = cls.get(self.identifier)
for field in cls._detail_fields:
setattr(self, field, getattr(obj, field))
Expand Down Expand Up @@ -163,7 +164,7 @@ class NArtistModel(ArtistModel, NBaseModel):
def get(cls, identifier):
artist_data = cls._api.artist_infos(identifier)
artist = artist_data['artist']
artist['songs'] = artist_data['hotSongs']
artist['songs'] = artist_data['hotSongs'] or []
artist, _ = NeteaseArtistSchema(strict=True).load(artist)
return artist

Expand Down
7 changes: 7 additions & 0 deletions fuocore/provider.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# -*- coding: utf-8 -*-

"""
fuocore.provider
~~~~~~~~~~~~~~~~
provider 意为音乐提供方
"""

from abc import ABC, abstractmethod
from fuocore.models import (
SongModel,
Expand Down

0 comments on commit b34219b

Please sign in to comment.