统一社会信用代码规则:
组织机构代码规则:
class Social(object):
'''
统一社会信用代码 + 组织结构代码校验
'''
def __init__(self):
'''
Constructor
'''
# 统一社会信用代码中不使用I,O,S,V,Z
# ''.join([str(i) for i in range(10)])
# import string
# string.ascii_uppercase # ascii_lowercase | ascii_letters
# dict([i for i in zip(list(self.string), range(len(self.string)))])
# dict(enumerate(self.string))
# list(d.keys())[list(d.values()).index(10)]
# chr(97) --> 'a'
self.string1 = '0123456789ABCDEFGHJKLMNPQRTUWXY'
self.SOCIAL_CREDIT_CHECK_CODE_DICT = {
'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17,
'J': 18, 'K': 19, 'L': 20, 'M': 21, 'N': 22, 'P': 23, 'Q': 24,
'R': 25, 'T': 26, 'U': 27, 'W': 28, 'X': 29, 'Y': 30}
# 第i位置上的加权因子
self.social_credit_weighting_factor = [1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28]
# GB11714-1997全国组织机构代码编制规则中代码字符集
self.string2 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
self.ORGANIZATION_CHECK_CODE_DICT = {
'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17, 'I': 18,
'J': 19, 'K': 20, 'L': 21, 'M': 22, 'N': 23, 'O': 24, 'P': 25, 'Q': 26,
'R': 27, 'S': 28, 'T': 29, 'U': 30, 'V': 31, 'W': 32, 'X': 33, 'Y': 34, 'Z': 35}
# 第i位置上的加权因子
self.organization_weighting_factor = [3, 7, 9, 10, 5, 8, 4, 2]
def gen_check_code(self, weighting_factor, ontology_code, modulus, check_code_dict):
'''
@param weighting_factor: 加权因子
@param ontology_code:本体代码
@param modulus: 模数(求余用)
@param check_code_dict: 字符字典
'''
total = 0
for i in range(len(ontology_code)):
if ontology_code[i].isdigit():
# print(ontology_code[i], weighting_factor[i])
total += int(ontology_code[i]) * weighting_factor[i]
else:
num = check_code_dict.get(ontology_code[i], -1)
if num < 0:
return -1
total += num * weighting_factor[i]
diff = modulus - total % modulus
return diff
def unified_social_credit_code():
"""统一社会信用代码"""
department = "123456789999999999999999" # 登记管理部门代码
agency = "11111111111111112121212345999" # 机构类别
organization_num = str(random.randint(11111111, 99999999))
u = Social()
# 组织机构代码校验位
check_code = u.gen_check_code(u.organization_weighting_factor, organization_num, 11, u.ORGANIZATION_CHECK_CODE_DICT)
organization_code = organization_num + str(check_code) # 组织机构代码
address_code = get_address(True)
# 没有校验码的社会统一代码
un_code = random.choice(department) + random.choice(agency) + str(address_code) + organization_code
# 社会 校验位
social_num = u.gen_check_code(u.social_credit_weighting_factor, un_code, 31, u.SOCIAL_CREDIT_CHECK_CODE_DICT)
if social_num == 31:
social_num = 0
social_dict = {value: key for key, value in u.SOCIAL_CREDIT_CHECK_CODE_DICT.items()}
# 两位证书 转换成 协议里面的值
social_key = social_dict[social_num]
code = un_code + social_key
return code
这样 就生成了 符合 规范的 统一社会信用代码