loncapa_check.py 981 Bytes
Newer Older
1 2 3 4 5 6 7
#!/usr/bin/python
#
# File:  mitx/lib/loncapa/loncapa_check.py
#
# Python functions which duplicate the standard comparison functions available to LON-CAPA problems.
# Used in translating LON-CAPA problems to i4x problem specification language.

8
from __future__ import division
9
import random
10
import math
11

12 13

def lc_random(lower, upper, stepsize):
14 15 16
    '''
    like random.randrange but lower and upper can be non-integer
    '''
17 18
    nstep = int((upper - lower) / (1.0 * stepsize))
    choices = [lower + x * stepsize for x in range(nstep)]
19 20
    return random.choice(choices)

21 22

def lc_choose(index, *args):
23 24 25 26
    '''
    return args[index]
    '''
    try:
27 28
        return args[int(index) - 1]
    except Exception, err:
29 30 31
        pass
    if len(args):
        return args[0]
32 33 34 35 36 37
    raise Exception(
        "loncapa_check.lc_choose error, index={index}, args={args}".format(
            index=index,
            args=args,
        )
    )
ichuang committed
38

39 40
deg2rad = math.pi / 180.0
rad2deg = 180.0 / math.pi