Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing custom prefix using uuid

Tags:

uuid

I went through the documentation for python UUID module.

>>> uuid.uuid4()
UUID('82fe5629-6680-4b13-a4e3-7a082f10e038')
>>> uuid.uuid4()
UUID('b2721a42-5645-4deb-bbbd-6ba1a55820d8')
>>> uuid.uuid4()
UUID('632736f8-a935-4335-a56d-56cd8ebc7dbf')
>>> uuid.uuid4()
UUID('a3519262-72d6-40ce-8e49-f65e8637ec07')

Every time it generates a random value. But my question is, what if I need to fix the first value and rest of them would be random. Is it possible?

e.g.,

>>> uuid.somefn("a3519262")
UUID('a3519262-72d6-40ce-8e49-f65e8637ec07')
>>> uuid.somefn("a3519262")
UUID('a3519262-a935-4335-a56d-56cd8ebc7dbf')

Reason I was looking for this solution: I have multiple systems talking to a centralized system, So based on the uuid I want to segregate the source/load on the system for metrics purpose. Any alternative solution is also welcome.

Thanks in advance.

like image 635
Suhadri Avatar asked Jan 25 '26 15:01

Suhadri


1 Answers

Been finding this solution for quite sometimes but just to make your work done! I think you would do a very hacky way to implement this.

>>>custom_uuid = str(uuid.uuid4())
>>>custom_uuid
'a3519262-72d6-40ce-8e49-f65e8637ec07'

So you can manipulate the string before storing it into a database or something like that.

>>>custom_uuid
'a3519262-72d6-40ce-8e49-f65e8637ec07'
>>>"prefix_" + custom_uuid
>>>'prefix_a3519262-72d6-40ce-8e49-f65e8637ec07'

>>>'a3519262-72d6-40ce-8e49-f65e8637ec07' + "_suffix"
>>>'a3519262-72d6-40ce-8e49-f65e8637ec07_suffix'
like image 63
Shedrack Avatar answered Jan 29 '26 18:01

Shedrack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!