From ace7e7dc02776eecf8497e5c7794bcebdc761793 Mon Sep 17 00:00:00 2001 From: helmutm Date: Wed, 8 Aug 2007 08:49:26 +0000 Subject: [PATCH] added util.uid for generating random ids base62-encoded git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@1886 fd906abe-77d9-0310-91a1-e0d9ade77398 --- util/uid.py | 7 ++++++- util/uid.txt | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/util/uid.py b/util/uid.py index c63b361..a273f54 100644 --- a/util/uid.py +++ b/util/uid.py @@ -31,6 +31,8 @@ bits = 128 def generateUid(check=None, lowerCaseOnly=False, bits=bits, seed=None, base=62): """ Generates a unique ID. """ + if base > 64: + raise ValueError('The base argument may not exceed 64, but is %i.' % base) random.seed(seed) OK = False base = lowerCaseOnly and min(base, 36) or base @@ -48,5 +50,8 @@ def strBase(n, base): return ''.join(reversed([toChar(n) for n in (result or [0])])) def toChar(n): - return n < 10 and chr(48+n) or n < 36 and chr(87+n) or chr(29+n) + return (n < 10 and chr(48+n) + or n < 36 and chr(87+n) + or n < 62 and chr(29+n) + or ('-', '_')[n-62]) diff --git a/util/uid.txt b/util/uid.txt index 8e90e47..8d7d7fd 100644 --- a/util/uid.txt +++ b/util/uid.txt @@ -24,3 +24,11 @@ $Id$ >>> generateUid() '...' + >>> generateUid(base=64, seed=42) + '2ZRA3X1CsqQhO0cn-zInCt' + + >>> generateUid(base=99, seed=42) + Traceback (most recent call last): + ... + ValueError: The base argument may not exceed 64, but is 99. +