简体   繁体   中英

How to generate UUID in python withing given range

I need to generate UUID for MYSQL int and BigInt which needs to be in a given range and I need 10 digits integer UUID for which I have written a code

def getUniqueID():
    unique_id = uuid4().int & (0 << 32) - 1
    return unique_id

for example, SQL int should not exceed the limit of 2147483647.(unique_id < 2147483647).

def getUniqueID(min,max):

    unique_id = uuid4().int & (0 << 32) - 1
    return unique_id

SQL int Limitation found here,

https://dev.mysql.com/doc/refman/8.0/en/integer-types.html

First you can install shortuuid with:

python -m pip install shortuuid

then you can use it like this:

import shortuuid
print(shortuuid.ShortUUID().random(length=10))

the project is in github: https://github.com/skorokithakis/shortuuid

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM