src/hdalab/utils.py
author ymh <ymh.work@gmail.com>
Tue, 05 Apr 2016 19:52:17 +0200
changeset 680 541cd2de37d8
parent 492 19220d52bce7
permissions -rw-r--r--
Added tag V03.02 for changeset 1911af6da33f

# -*- coding: utf-8 -*-
'''
Created on Mar 13, 2012

@author: ymh and tc
'''
import hashlib
import logging
import re

from django.core.cache import cache
from django.utils.encoding import smart_str


logger = logging.getLogger(__name__)


def fix_cache_key(key):
    cache_key = re.sub(r'\s+', '-', key)
    cache_key = smart_str(cache_key)
    if len(cache_key) > (250-(2+len(cache.key_prefix)+len(str(cache.version)))-33):
        cache_key = cache_key[:(250-(2+len(cache.key_prefix)+len(str(cache.version)))-33)] + '-' + hashlib.md5(cache_key).hexdigest()
    return cache_key
        
#
# from : http://cs.stackexchange.com/a/10321
#
def toDigits(n, b):
    """Convert a positive number n to its digit representation in base b."""
    digits = []
    while n > 0:
        digits.insert(0, n % b)
        n  = n // b
    return digits