summaryrefslogtreecommitdiff
path: root/ricky/param/multiselect.py
blob: 68df161c1449a9d64f65957ddbac66e7e219b3fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import random
from ricky.param import Param


class MultiSelect(Param):
    def __init__(self, **kwargs):
        super(MultiSelect, self).__init__(**kwargs)

    def value_get(self):
        return super(MultiSelect, self).value_get()

    def value_set(self, value):
        if not any([value == i['value'] for i in self._options]) and \
            value is not None:
            raise ValueError
        super(MultiSelect, self).value_set(value)

    value = property(value_get, value_set)

    def randomize(self):
        weights_total = sum(map(lambda x: x["weight"], self.options()))
        choice = random.randint(0, weights_total)
        position = 0
        for elem in self.options():
            position += elem["weight"]
            if position >= choice:
                self.value = elem["value"]
                break