uuid-utils
uuid-utils copied to clipboard
Generate UUID7 from string.
It would be handy to be able to create a UUID7 from a string, my naive approach of passing one in didn't work + I've seen you need the timestamp itself.
My use case is roundtrips to config (+ checking this).
It's easier to see the difference in some UUIDs themselves than some timestamps (accross a couple of config files, as I have).
I think I also need this to have a UUID7 field in a django app, since that wants to roundtrip to and from string, e.g. in to_python.
Cheers, got there in the end with help from a chat bot -
For anyone needing this in future, I needed to do:
import uuid_utils as uuid
value = uuid.UUID(value, version=7)
Should probably add this to the README
In the end I'm not using this library, as I can't work out how to save these into postgres in django in a field.
>>> import uuid
>>> from uuid_utils import uuid7
>>> uuid.UUID(str(uuid7()))
UUID('0196d5e1-fa88-7993-a921-80946d50987f')
For Python 3.14+, you can probably just do this:
uuid.UUID(uuid7(), version=7)
For Django specifically you need to use uuid_utils.compat, e.g. this:
from uuid_utils.compat import uuid7
Yeah, in addition to what @opentyler mentioned I'm not sure how the documentation can be improved. Feel free to create PRs though