Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-backend / pithos / backends / random_word.py @ 1ec05716

History | View | Annotate | Download (2.2 kB)

1 56f3c759 Sofia Papagiannaki
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 56f3c759 Sofia Papagiannaki
#
3 56f3c759 Sofia Papagiannaki
# Redistribution and use in source and binary forms, with or
4 56f3c759 Sofia Papagiannaki
# without modification, are permitted provided that the following
5 56f3c759 Sofia Papagiannaki
# conditions are met:
6 56f3c759 Sofia Papagiannaki
#
7 56f3c759 Sofia Papagiannaki
#   1. Redistributions of source code must retain the above
8 56f3c759 Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
9 56f3c759 Sofia Papagiannaki
#      disclaimer.
10 56f3c759 Sofia Papagiannaki
#
11 56f3c759 Sofia Papagiannaki
#   2. Redistributions in binary form must reproduce the above
12 56f3c759 Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
13 56f3c759 Sofia Papagiannaki
#      disclaimer in the documentation and/or other materials
14 56f3c759 Sofia Papagiannaki
#      provided with the distribution.
15 56f3c759 Sofia Papagiannaki
#
16 56f3c759 Sofia Papagiannaki
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 56f3c759 Sofia Papagiannaki
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 56f3c759 Sofia Papagiannaki
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 56f3c759 Sofia Papagiannaki
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 56f3c759 Sofia Papagiannaki
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 56f3c759 Sofia Papagiannaki
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 56f3c759 Sofia Papagiannaki
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 56f3c759 Sofia Papagiannaki
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 56f3c759 Sofia Papagiannaki
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 56f3c759 Sofia Papagiannaki
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 56f3c759 Sofia Papagiannaki
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 56f3c759 Sofia Papagiannaki
# POSSIBILITY OF SUCH DAMAGE.
28 56f3c759 Sofia Papagiannaki
#
29 56f3c759 Sofia Papagiannaki
# The views and conclusions contained in the software and
30 56f3c759 Sofia Papagiannaki
# documentation are those of the authors and should not be
31 56f3c759 Sofia Papagiannaki
# interpreted as representing official policies, either expressed
32 56f3c759 Sofia Papagiannaki
# or implied, of GRNET S.A.
33 56f3c759 Sofia Papagiannaki
34 4a105ce2 Sofia Papagiannaki
import random
35 56f3c759 Sofia Papagiannaki
36 4a105ce2 Sofia Papagiannaki
getrandbits = random.SystemRandom().getrandbits
37 56f3c759 Sofia Papagiannaki
38 4a105ce2 Sofia Papagiannaki
DEFAULT_ALPHABET = ("0123456789"
39 4a105ce2 Sofia Papagiannaki
                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
40 4a105ce2 Sofia Papagiannaki
                    "abcdefghijklmnopqrstuvwxyz")
41 56f3c759 Sofia Papagiannaki
42 29148653 Sofia Papagiannaki
43 4a105ce2 Sofia Papagiannaki
def get_random_word(length, alphabet=DEFAULT_ALPHABET):
44 4a105ce2 Sofia Papagiannaki
    remainder = getrandbits(length * 8)
45 4a105ce2 Sofia Papagiannaki
    return encode_word(remainder, alphabet=alphabet)
46 4a105ce2 Sofia Papagiannaki
47 29148653 Sofia Papagiannaki
48 4a105ce2 Sofia Papagiannaki
def encode_word(number, alphabet=DEFAULT_ALPHABET):
49 56f3c759 Sofia Papagiannaki
    base = len(alphabet)
50 56f3c759 Sofia Papagiannaki
    digits = []
51 56f3c759 Sofia Papagiannaki
    append = digits.append
52 4a105ce2 Sofia Papagiannaki
    quotient = number
53 4a105ce2 Sofia Papagiannaki
    while True:
54 56f3c759 Sofia Papagiannaki
        quotient, remainder = divmod(quotient, base)
55 56f3c759 Sofia Papagiannaki
        append(alphabet[remainder])
56 4a105ce2 Sofia Papagiannaki
        if quotient <= 0:
57 4a105ce2 Sofia Papagiannaki
            break
58 56f3c759 Sofia Papagiannaki
59 4a105ce2 Sofia Papagiannaki
    return ''.join(digits)