traits.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:uchroma 作者: cyanogen 项目源码 文件源码
def trait_as_dict(trait: TraitType) -> dict:
    """
    Convert a trait to a dict for sending over D-Bus or the like

    :param trait: the trait to be converted
    :return: dict representing this trait
    """
    cls = trait.__class__
    tdict = {}

    for k, v in vars(trait).items():
        if k.startswith('__') or k == 'this_class':
            continue
        if hasattr(cls, k) and getattr(cls, k) == v:
            continue
        if isinstance(v, Iterable) and len(v) == 0:
            continue

        if k.startswith('_'):
            tdict[k[1:]] = v
        else:
            tdict[k] = v

    if isinstance(trait, UseEnum):
        cls = CaselessStrEnum
        tdict['values'] = tuple(trait.enum_class.__members__.keys())
        if 'enum_class' in tdict:
            del tdict['enum_class']

    for k, v in tdict.items():
        if isinstance(v, TraitType):
            tdict[k] = trait_as_dict(v)
        if isinstance(v, enum.Enum):
            tdict[k] = v.name
        if isinstance(v, type):
            tdict[k] = '%s.%s' % (v.__module__, v.__name__)

    tdict['__class__'] = (cls.__module__, cls.__name__)
    return tdict
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号