1
2
3
4
5
6
7
8
9
10
11
12
13 __doc__="""guid
14
15 Generate a globally unique id that is used for events.
16 This is a wrapper around the library that is used in Python 2.5
17 and higher.
18 See http://zestyping.livejournal.com/157957.html for more info and
19 the code is available from http://zesty.ca/python/
20 """
21
22 from uuid.uuid import uuid1, uuid3, uuid4, uuid5
23
24
25 known_uuid_types= {
26 1:uuid1,
27 3:uuid3,
28 4:uuid4,
29 5:uuid5,
30 }
31
32 -def generate( uuid_type=4, *args, **kwargs ):
33 """
34 Generate an Universally Unique ID (UUID), according to RFC 4122.
35 If an unknown uuid_type is provided, uses the UUID4 algorithm.
36
37 >>> guids = [ generate() for x in range(100000) ]
38 >>> guid_set = set( guids )
39 >>> len(guids) == len(guid_set)
40 True
41 >>> len( str( generate() ) ) == 36
42 True
43
44 @param uuid_type: the type of UUID to generate
45 @type uuid_type: range from 0 - 5
46 @return: UUID
47 @type: string
48 """
49
50 uuid_func= known_uuid_types.get( uuid_type, uuid4 )
51 return str( uuid_func(*args, **kwargs) )
52