src/hdalab/utils.py
author ymh <ymh.work@gmail.com>
Mon, 26 Oct 2015 19:20:54 +0100
changeset 660 04255afd160e
parent 492 19220d52bce7
permissions -rw-r--r--
display errors in registration form. Add check for email unicity. slightly change registration message, upgrade django-registration-redux
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
'''
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
Created on Mar 13, 2012
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
290
fb86765b4c54 node placer classes
cavaliet
parents: 266
diff changeset
     5
@author: ymh and tc
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
'''
492
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents: 462
diff changeset
     7
import hashlib
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents: 462
diff changeset
     8
import logging
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents: 462
diff changeset
     9
import re
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents: 462
diff changeset
    10
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
from django.core.cache import cache
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
from django.utils.encoding import smart_str
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
492
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents: 462
diff changeset
    14
290
fb86765b4c54 node placer classes
cavaliet
parents: 266
diff changeset
    15
logger = logging.getLogger(__name__)
fb86765b4c54 node placer classes
cavaliet
parents: 266
diff changeset
    16
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
def fix_cache_key(key):
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
    cache_key = re.sub(r'\s+', '-', key)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
    cache_key = smart_str(cache_key)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
    if len(cache_key) > (250-(2+len(cache.key_prefix)+len(str(cache.version)))-33):
345
7bc38c7d6cf9 update libs + add wsgi interface
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    22
        cache_key = cache_key[:(250-(2+len(cache.key_prefix)+len(str(cache.version)))-33)] + '-' + hashlib.md5(cache_key).hexdigest()
290
fb86765b4c54 node placer classes
cavaliet
parents: 266
diff changeset
    23
    return cache_key
fb86765b4c54 node placer classes
cavaliet
parents: 266
diff changeset
    24
        
462
df8b3b49e81c correct bug and add email sending
ymh <ymh.work@gmail.com>
parents: 345
diff changeset
    25
#
df8b3b49e81c correct bug and add email sending
ymh <ymh.work@gmail.com>
parents: 345
diff changeset
    26
# from : http://cs.stackexchange.com/a/10321
df8b3b49e81c correct bug and add email sending
ymh <ymh.work@gmail.com>
parents: 345
diff changeset
    27
#
df8b3b49e81c correct bug and add email sending
ymh <ymh.work@gmail.com>
parents: 345
diff changeset
    28
def toDigits(n, b):
df8b3b49e81c correct bug and add email sending
ymh <ymh.work@gmail.com>
parents: 345
diff changeset
    29
    """Convert a positive number n to its digit representation in base b."""
df8b3b49e81c correct bug and add email sending
ymh <ymh.work@gmail.com>
parents: 345
diff changeset
    30
    digits = []
df8b3b49e81c correct bug and add email sending
ymh <ymh.work@gmail.com>
parents: 345
diff changeset
    31
    while n > 0:
df8b3b49e81c correct bug and add email sending
ymh <ymh.work@gmail.com>
parents: 345
diff changeset
    32
        digits.insert(0, n % b)
df8b3b49e81c correct bug and add email sending
ymh <ymh.work@gmail.com>
parents: 345
diff changeset
    33
        n  = n // b
492
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents: 462
diff changeset
    34
    return digits
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents: 462
diff changeset
    35
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents: 462
diff changeset
    36
    
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents: 462
diff changeset
    37