src/hdalab/utils.py
author ymh <ymh.work@gmail.com>
Sat, 28 Feb 2015 06:40:48 +0100
changeset 462 df8b3b49e81c
parent 345 7bc38c7d6cf9
child 492 19220d52bce7
permissions -rw-r--r--
correct bug and add email sending
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
'''
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
from django.core.cache import cache
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
from django.utils.encoding import smart_str
345
7bc38c7d6cf9 update libs + add wsgi interface
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
     9
import hashlib
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
import re
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
290
fb86765b4c54 node placer classes
cavaliet
parents: 266
diff changeset
    12
import logging
fb86765b4c54 node placer classes
cavaliet
parents: 266
diff changeset
    13
logger = logging.getLogger(__name__)
fb86765b4c54 node placer classes
cavaliet
parents: 266
diff changeset
    14
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
def fix_cache_key(key):
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
    cache_key = re.sub(r'\s+', '-', key)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
    cache_key = smart_str(cache_key)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
    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
    20
        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
    21
    return cache_key
fb86765b4c54 node placer classes
cavaliet
parents: 266
diff changeset
    22
        
462
df8b3b49e81c correct bug and add email sending
ymh <ymh.work@gmail.com>
parents: 345
diff changeset
    23
#
df8b3b49e81c correct bug and add email sending
ymh <ymh.work@gmail.com>
parents: 345
diff changeset
    24
# from : http://cs.stackexchange.com/a/10321
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
def toDigits(n, b):
df8b3b49e81c correct bug and add email sending
ymh <ymh.work@gmail.com>
parents: 345
diff changeset
    27
    """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
    28
    digits = []
df8b3b49e81c correct bug and add email sending
ymh <ymh.work@gmail.com>
parents: 345
diff changeset
    29
    while n > 0:
df8b3b49e81c correct bug and add email sending
ymh <ymh.work@gmail.com>
parents: 345
diff changeset
    30
        digits.insert(0, n % b)
df8b3b49e81c correct bug and add email sending
ymh <ymh.work@gmail.com>
parents: 345
diff changeset
    31
        n  = n // b
df8b3b49e81c correct bug and add email sending
ymh <ymh.work@gmail.com>
parents: 345
diff changeset
    32
    return digits