time_uuid is a lightweight Python library for sensibly dealing with UUIDv1 (or TimeUUIDs as we like to sometimes call them). It allows you to create UUIDv1s in a variety of different ways. Take a look at the docs for the interface.
A class that makes dealing with time and V1 UUIDs much easiser. Offers accurate comparison (compares accurately with other TimeUUIDs and UUIDv1), a way to get TimeUUIDs with UTC-timestamps (instead of the default localtime), an easy way to get the UTC datetime object from it, and default encoding and decoding methods.
Try to convert a number of different types to a TimeUUID. Works for datetimes, ints (which it treats as timestamps) and UUIDs.
See with_timestamp() for descriptions of the randomize and lowest_val arguments.
Parameters: | value – An instance of UUID, TimeUUID, datetime or timestamp int or float |
---|---|
Returns: | A TimeUUID object |
Return type: | TimeUUID |
Create a TimeUUID with any timestamp. Be sure that the timestamp is created with the correct offset. If you are using time.time() be sure to know that in python, it may be offset by your machine’s time zone!
Parameters: |
|
---|---|
Returns: | A TimeUUID object |
Return type: | TimeUUID |
Create a TimeUUID with any datetime in UTC. Only use this if you are sure you are creating your datetime objects properly and can predict how the TimeUUID will sort (if that is at all necessary).
See with_timestamp() for a description of how randomize and lowest_val arguments work.
Parameters: | d (TimeUUID) – A datetime object |
---|---|
Returns: | A TimeUUID object |
Create a TimeUUID with the current datetime in UTC. Every TimeUUID generated this way, on this machine, will supersede the TimeUUID generated before it, by use of a mutex.
Parameters: | randomize – Whether or not to randomize the other bits in the UUID. |
---|
Defaults to True :type randomize: bool
Returns: | A TimeUUID object |
---|---|
Return type: | TimeUUID |
Sorting TimeUUID
TimeUUIDs can be sorted sorted. They will be sorted, first by the date component, then by the random component in a consistent way:
>>> import random, time_uuid
>>> rand_time = lambda: float(random.randrange(0,30))+time_uuid.utctime()
>>> uuids = [time_uuid.TimeUUID.with_timestamp(rand_time()) for i in xrange(3)]
[TimeUUID('2e4ac100-31f1-11e2-9286-14109fcdd33b'),
TimeUUID('2db393a2-31f1-11e2-abda-14109fcdd33b'),
TimeUUID('2987779e-31f1-11e2-a699-14109fcdd33b')]
>>> list(sorted(uuids))
[TimeUUID('2987779e-31f1-11e2-a699-14109fcdd33b'),
TimeUUID('2db393a2-31f1-11e2-abda-14109fcdd33b'),
TimeUUID('2e4ac100-31f1-11e2-9286-14109fcdd33b')]
Creating lower and upper bounds for range searches with TimeUUIDs
If you are using a database in which TimeUUIDs are a first-class data type, you can use TimeUUID to generate upper and lower bounds for range queries:
>>> import time_uuid, datetime
>>> floor_day = lambda d: datetime.datetime(year=d.year, month=d.month, day=d.day)
>>> yesterday = floor_day(datetime.datetime.utcnow() - datetime.datetime.timedelta(days=1))
>>> today = floor_day(datetime.datetime.utcnow())
>>> lower_bound = time_uuid.TimeUUID.with_utc(yesterday, randomize=False, lowest_val=True)
>>> upper_bound = time_uuid.TimeUUID.with_utc(today, randomize=False, lowest_val=False)