|
154
|
1 |
# -*- coding: utf-8 -*- |
|
|
2 |
''' |
|
|
3 |
Created on Mar 13, 2012 |
|
|
4 |
|
|
290
|
5 |
@author: ymh and tc |
|
154
|
6 |
''' |
|
492
|
7 |
import hashlib |
|
|
8 |
import logging |
|
|
9 |
import re |
|
|
10 |
|
|
154
|
11 |
from django.core.cache import cache |
|
|
12 |
from django.utils.encoding import smart_str |
|
|
13 |
|
|
492
|
14 |
|
|
290
|
15 |
logger = logging.getLogger(__name__) |
|
|
16 |
|
|
154
|
17 |
|
|
|
18 |
def fix_cache_key(key): |
|
|
19 |
cache_key = re.sub(r'\s+', '-', key) |
|
|
20 |
cache_key = smart_str(cache_key) |
|
|
21 |
if len(cache_key) > (250-(2+len(cache.key_prefix)+len(str(cache.version)))-33): |
|
345
|
22 |
cache_key = cache_key[:(250-(2+len(cache.key_prefix)+len(str(cache.version)))-33)] + '-' + hashlib.md5(cache_key).hexdigest() |
|
290
|
23 |
return cache_key |
|
|
24 |
|
|
462
|
25 |
# |
|
|
26 |
# from : http://cs.stackexchange.com/a/10321 |
|
|
27 |
# |
|
|
28 |
def toDigits(n, b): |
|
|
29 |
"""Convert a positive number n to its digit representation in base b.""" |
|
|
30 |
digits = [] |
|
|
31 |
while n > 0: |
|
|
32 |
digits.insert(0, n % b) |
|
|
33 |
n = n // b |
|
492
|
34 |
return digits |
|
|
35 |
|
|
|
36 |
|
|
|
37 |
|