am planning to create a unique ID from characters and numbers as well. The ID should look something like this
XXXX-XXXX-XXXX-XXXX
where X is a combination of numbers, small and cap letters. This Id will be stored in a mysql database and will be incremented to maintain consistency of data.
Any advice on how to start?
Use the uuid module;
>>> import uuid
# make a UUID based on the host ID and current time
>>> uuid.uuid1()
UUID('a8098c1a-f86e-11da-bd1a-00112444be1e')
# make a UUID using an MD5 hash of a namespace UUID and a name
>>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e')
# make a random UUID
>>> uuid.uuid4()
UUID('16fd2706-8baf-433b-82eb-8c7fada847da')
# make a UUID using a SHA-1 hash of a namespace UUID and a name
>>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')
Your 4-character requirement can be emulated with;
>>> "-".join([x[:4].upper() for x in str(uuid.uuid4()).split("-")])
'C984-EE70-4BFD-9963-9BB9'
>>> "-".join([x[:4].upper() for x in str(uuid.uuid4()).split("-")])
'30D7-4356-493E-B887-2671
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With