python验证企业统一信用码

_credit_id_weight = (
    1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28)


def _is_credit_id_valid(oid: str) -> bool:
    if not isinstance(oid, str):
        return False
    try:
        s = sum(_credit_id_dict[d] * _credit_id_weight[i]
                for i, d in enumerate(oid[:-1]))
    except KeyError:
        return False
    result = 31 - s % 31
    if result == 30:
        return oid[-1] == 'Y'
    elif result == 31:
        return oid[-1] == '0'
    else:
        return _credit_id_dict.get(oid[-1]) == result

 

posted on 2020-04-10 15:46  不要挡着我晒太阳  阅读(289)  评论(0编辑  收藏  举报

导航