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. +