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
This commit is contained in:
		
							parent
							
								
									30ab26608b
								
							
						
					
					
						commit
						ace7e7dc02
					
				
					 2 changed files with 14 additions and 1 deletions
				
			
		| 
						 | 
					@ -31,6 +31,8 @@ bits = 128
 | 
				
			||||||
def generateUid(check=None, lowerCaseOnly=False, bits=bits, seed=None, base=62):
 | 
					def generateUid(check=None, lowerCaseOnly=False, bits=bits, seed=None, base=62):
 | 
				
			||||||
    """ Generates a unique ID.
 | 
					    """ Generates a unique ID.
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
 | 
					    if base > 64:
 | 
				
			||||||
 | 
					        raise ValueError('The base argument may not exceed 64, but is %i.' % base)
 | 
				
			||||||
    random.seed(seed)
 | 
					    random.seed(seed)
 | 
				
			||||||
    OK = False
 | 
					    OK = False
 | 
				
			||||||
    base = lowerCaseOnly and min(base, 36) or base
 | 
					    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])]))
 | 
					    return ''.join(reversed([toChar(n) for n in (result or [0])]))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def toChar(n):
 | 
					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])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -24,3 +24,11 @@ $Id$
 | 
				
			||||||
  >>> generateUid()
 | 
					  >>> 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.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue