have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one 3.5
authorAurelien Campeas
Wed, 09 Sep 2009 17:46:30 +0200
branch3.5
changeset 3146 cfc4344023f2
parent 3145 9b28545de60d
child 3147 83ad6ff13e3f
have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
test/unittest_utils.py
utils.py
--- a/test/unittest_utils.py	Wed Sep 09 17:45:34 2009 +0200
+++ b/test/unittest_utils.py	Wed Sep 09 17:46:30 2009 +0200
@@ -17,12 +17,12 @@
         self.assertNotEquals(make_uid('xyz'), make_uid('xyz'))
 
     def test_2(self):
-        d = {}
+        d = set()
         while len(d)<10000:
             uid = make_uid('xyz')
-            if d.has_key(uid):
+            if uid in d:
                 self.fail(len(d))
-            d[uid] = 1
+            d.add(uid)
 
 
 class UStringIOTC(TestCase):
--- a/utils.py	Wed Sep 09 17:45:34 2009 +0200
+++ b/utils.py	Wed Sep 09 17:46:30 2009 +0200
@@ -11,6 +11,7 @@
 
 import locale
 from md5 import md5
+import sys
 from datetime import datetime, timedelta, date
 from time import time, mktime
 from random import randint, seed
@@ -103,11 +104,17 @@
     encoding = locale.getpreferredencoding(do_setlocale=False) or 'UTF-8'
     return unicode(date.strftime(str(fmt)), encoding)
 
-def make_uid(key):
-    """forge a unique identifier"""
-    msg = str(key) + "%.10f" % time() + str(randint(0, 1000000))
-    return md5(msg).hexdigest()
 
+if sys.version_info[:2] < (2, 5):
+    def make_uid(key):
+        """forge a unique identifier
+        not that unique on win32"""
+        msg = str(key) + "%.10f" % time() + str(randint(0, 1000000))
+        return md5(msg).hexdigest()
+else:
+    from uuid import uuid4
+    def make_uid(key):
+        return key + str(uuid4())
 
 def dump_class(cls, clsname):
     """create copy of a class by creating an empty class inheriting