python生成不重复id

import time

'''
配置一个唯一的ruid
props: None
return: 
'''
class RuidGet(object):
    '''
    配置一个唯一的ruid
    props: None
    return:
    '''
    @classmethod
    def get_str_ruid(cls):
        '''
        获取16进制字符串唯一id
        :return:
        '''
        base_time = round(time.mktime(time.strptime('1970-01-02 00:00:00', '%Y-%m-%d %H:%M:%S'))*10**3)

        ruid = round(time.time()*10**3) - base_time
        time.sleep(0.001)
        return str(hex(ruid)).replace('0x', '')

    @classmethod
    def get_int_ruid(cls):
        '''
        获取10进制整数唯一id
        :return:
        '''
        base_time = round(time.mktime(time.strptime('1970-01-02 00:00:00', '%Y-%m-%d %H:%M:%S')) * 10 ** 3)

        ruid = round(time.time() * 10 ** 3) - base_time
        time.sleep(0.001)
        return str(ruid)


if __name__ == '__main__':
    id16 = RuidGet.get_str_ruid()
    print(id16)    # 179a32942da

    id10 = RuidGet.get_int_ruid()
    print(id10)    # 1621940060893


你可能感兴趣的:(Python,python日常开发工具包,python)