source: QX/RPlain/randomname.py @ 954

Revision 954, 1.6 KB checked in by fhoffmann, 2 years ago (diff)

new registerImage (copy of elntest)

Line 
1#
2#  Copyright (c) 2007 Helmut Merz helmutm@cy55.de
3#
4#  This program is free software; you can redistribute it and/or modify
5#  it under the terms of the GNU General Public License as published by
6#  the Free Software Foundation; either version 2 of the License, or
7#  (at your option) any later version.
8#
9#  This program is distributed in the hope that it will be useful,
10#  but WITHOUT ANY WARRANTY; without even the implied warranty of
11#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12#  GNU General Public License for more details.
13#
14#  You should have received a copy of the GNU General Public License
15#  along with this program; if not, write to the Free Software
16#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17#
18
19"""
20Generate strange (not guessable) UIDs.
21
22$Id$
23"""
24
25import random
26
27
28charList = ([chr(i) for i in range(48, 58)]
29          + [chr(i) for i in range(97, 123)]
30          + [chr(i) for i in range(65, 91)]
31          + ['-', '_']
32)
33
34
35def generateName(check=None, lowerCaseOnly=False, bits=128, base=62, seed=None):
36    """ Generates an unguessable random name.
37    """
38    if base > 64:
39        raise ValueError('The base argument may not exceed 64, but is %i.' % base)
40    random.seed(seed)
41    OK = False
42    base = lowerCaseOnly and min(base, 36) or base
43    while not OK:
44        data = strBase(random.getrandbits(bits), base)
45        OK = check is None or check(data)
46    return data
47
48
49def strBase(n, base):
50    result = []
51    while n > 0:
52        n, r = divmod(n, base)
53        result.append(r)
54    return ''.join(reversed([charList[n] for n in (result or [0])]))
Note: See TracBrowser for help on using the repository browser.