working with uuids on python
Tags: python uuidPython has a module to generate different UUIDs like defined in RFC 4122. uuids include creation time. This recipe extracts the timestamp information:
def get_posixtime(uuid1):
"""Convert the uuid1 timestamp to a standard posix timestamp
"""
assert uuid1.version == 1, ValueError('only applies to type 1')
t = uuid1.time
t = t - 0x01b21dd213814000
t = t / 1e7
return t
Another task is to sort uuids by creation time. The default cmp function seems not to do this:
...
int = ((time_low << 96L) | (time_mid << 80L) |
(time_hi_version << 64L) | (clock_seq << 48L) | node)
def __cmp__(self, other):
if isinstance(other, UUID):
return cmp(self.int, other.int)
return NotImplemented
But since uuids have time attribute, a list with them can be sorted with
from operator import attrgetter
lst = [..]
lst.sort(key=attrgetter("time"))