<filename>applications/utils/calculator.py

def percent(correct, wrong, no_answer, negative_coefficient=3):
    point = 1.0
    total_question = int(correct) + int(wrong) + int(no_answer)
    point = ((int(negative_coefficient) * int(correct)) - int(wrong)) / (int(negative_coefficient) * total_question) * 100
    return round(point, 2)


SYMBOLS = {
    'customary': ('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'),
    'customary_ext': ('byte', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zetta', 'iotta'),
    'iec': ('Bi', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'),
    'iec_ext': ('byte', 'kibi', 'mebi', 'gibi', 'tebi', 'pebi', 'exbi', 'zebi', 'yobi'),
}


def byte_converter(file_size, output_format='%(value).1f %(symbol)s', symbols='customary'):
    """
    Convert file_size bytes into a human readable string based on output_format.
    symbols can be either "customary", "customary_ext", "iec" or "iec_ext",
    see: http://goo.gl/kTQMs
    """
    file_size = int(file_size)
    if file_size < 0:
        raise ValueError("file_size < 0")
    symbols = SYMBOLS[symbols]
    prefix = {}
    for i, s in enumerate(symbols[1:]):
        prefix[s] = 1 << (i+1)*10
    for symbol in reversed(symbols[1:]):
        if file_size >= prefix[symbol]:
            value = float(file_size) / prefix[symbol]
            return output_format % locals()
    return output_format % dict(symbol=symbols[0], value=file_size)


def human2bytes(s):
    """
    Attempts to guess the string format based on default symbols
    set and return the corresponding bytes as an integer.
    When unable to recognize the format ValueError is raised.

      >>> human2bytes('0 B')
      0
      >>> human2bytes('1 K')
      1024
      >>> human2bytes('1 M')
      1048576
      >>> human2bytes('1 Gi')
      1073741824
      >>> human2bytes('1 tera')
      1099511627776

      >>> human2bytes('0.5kilo')
      512
      >>> human2bytes('0.1  byte')
      0
      >>> human2bytes('1 k')  # k is an alias for K
      1024
      >>> human2bytes('12 foo')
      Traceback (most recent call last):
          ...
      ValueError: can't interpret '12 foo'
    """
    init = s
    num = ""
    while s and s[0:1].isdigit() or s[0:1] == '.':
        num += s[0]
        s = s[1:]
    num = float(num)
    letter = s.strip()
    for name, sset in SYMBOLS.items():
        if letter in sset:
            break
    else:
        if letter == 'k':
            # treat 'k' as an alias for 'K' as per: http://goo.gl/kTQMs
            sset = SYMBOLS['customary']
            letter = letter.upper()
        else:
            raise ValueError("can't interpret %r" % init)
    prefix = {sset[0]:1}
    for i, s in enumerate(sset[1:]):
        prefix[s] = 1 << (i+1)*10
    return int(num * prefix[letter])


<filename>applications/utils/calculator.py

def percent(correct, wrong, no_answer, negative_coefficient=3):
    point = 1.0
    total_question = int(correct) + int(wrong) + int(no_answer)
    point = ((int(negative_coefficient) * int(correct)) - int(wrong)) / (int(negative_coefficient) * total_question) * 100
    return round(point, 2)


SYMBOLS = {
    'customary': ('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'),
    'customary_ext': ('byte', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zetta', 'iotta'),
    'iec': ('Bi', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'),
    'iec_ext': ('byte', 'kibi', 'mebi', 'gibi', 'tebi', 'pebi', 'exbi', 'zebi', 'yobi'),
}


def byte_converter(file_size, output_format='%(value).1f %(symbol)s', symbols='customary'):
    """
    Convert file_size bytes into a human readable string based on output_format.
    symbols can be either "customary", "customary_ext", "iec" or "iec_ext",
    see: http://goo.gl/kTQMs
    """
    file_size = int(file_size)
    if file_size < 0:
        raise ValueError("file_size < 0")
    symbols = SYMBOLS[symbols]
    prefix = {}
    for i, s in enumerate(symbols[1:]):
        prefix[s] = 1 << (i+1)*10
    for symbol in reversed(symbols[1:]):
        if file_size >= prefix[symbol]:
            value = float(file_size) / prefix[symbol]
            return output_format % locals()
    return output_format % dict(symbol=symbols[0], value=file_size)


def human2bytes(s):
    """
    Attempts to guess the string format based on default symbols
    set and return the corresponding bytes as an integer.
    When unable to recognize the format ValueError is raised.

      >>> human2bytes('0 B')
      0
      >>> human2bytes('1 K')
      1024
      >>> human2bytes('1 M')
      1048576
      >>> human2bytes('1 Gi')
      1073741824
      >>> human2bytes('1 tera')
      1099511627776

      >>> human2bytes('0.5kilo')
      512
      >>> human2bytes('0.1  byte')
      0
      >>> human2bytes('1 k')  # k is an alias for K
      1024
      >>> human2bytes('12 foo')
      Traceback (most recent call last):
          ...
      ValueError: can't interpret '12 foo'
    """
    init = s
    num = ""
    while s and s[0:1].isdigit() or s[0:1] == '.':
        num += s[0]
        s = s[1:]
    num = float(num)
    letter = s.strip()
    for name, sset in SYMBOLS.items():
        if letter in sset:
            break
    else:
        if letter == 'k':
            # treat 'k' as an alias for 'K' as per: http://goo.gl/kTQMs
            sset = SYMBOLS['customary']
            letter = letter.upper()
        else:
            raise ValueError("can't interpret %r" % init)
    prefix = {sset[0]:1}
    for i, s in enumerate(sset[1:]):
        prefix[s] = 1 << (i+1)*10
    return int(num * prefix[letter])


Convert file_size bytes into a human readable string based on output_format.     symbols can be either "customary", "customary_ext", "iec" or "iec_ext",     see: http://goo.gl/kTQMs Attempts to guess the string format based on default symbols     set and return the corresponding bytes as an integer.     When unable to recognize the format ValueError is raised.        >>> human2bytes('0 B')       0       >>> human2bytes('1 K')       1024       >>> human2bytes('1 M')       1048576       >>> human2bytes('1 Gi')       1073741824       >>> human2bytes('1 tera')       1099511627776        >>> human2bytes('0.5kilo')       512       >>> human2bytes('0.1  byte')       0       >>> human2bytes('1 k')  # k is an alias for K       1024       >>> human2bytes('12 foo')       Traceback (most recent call last):           ...       ValueError: can't interpret '12 foo' # treat 'k' as an alias for 'K' as per: http://goo.gl/kTQMs