vidjil_utils.py 1.16 KB
Newer Older
1
import math
2
from gluon import current
3

4
def format_size(n, unit='B'):
5 6
    '''
    Takes an integer n, representing a filesize and returns a string
7 8
    where the size is formatted with the correct SI prefix and
    with a constant number of significant digits.
9 10

    Example:
11 12 13 14 15 16
    >>> format_size(42)
    '42 B'
    >>> format_size(123456)
    '123 kB'
    >>> format_size(1000*1000)
    '1.00 MB'
17
    >>> format_size(1024*1024*1024)
18 19 20
    '1.07 GB'
    >>> format_size(42*(2**40))
    '46.2 TB'
21
    '''
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    size = float(n)
    PREFIXES = ['', 'k', 'M', 'G', 'T', 'P']

    for prefix in PREFIXES:
        if size < 1000:
            break
        size /= 1000


    if size > 100 or not prefix:
        fmt = '%.0f'
    elif size > 10:
        fmt = '%.1f'
    else:
        fmt = '%.2f'

    return fmt % size + ' ' + prefix + unit

40 41 42 43 44 45 46 47 48 49 50 51

def anon(patient_id, user_id):
    db = current.db
    auth=current.auth
    
    last_name = db.patient[patient_id].last_name
    first_name = db.patient[patient_id].first_name
    
    if auth.has_permission("anon", "patient", patient_id, user_id):
        return last_name + " " + first_name
    else:
        return last_name[:3] + " " + first_name[:3]