summaryrefslogtreecommitdiff
path: root/ricky/param/bool.py
blob: 961912e780ef69438c68e1cf77b58af27afb8afa (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
30
31
32
33
34
35
36
from ricky.param import Param
import re
import random


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

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

    def value_set(self, value):
        value = self._bool_correct(value)
        super(Bool, self).value_set(value)

    value = property(value_get, value_set)

    def _bool_correct(self, value):
        value = str(value)
        if any([
            re.match(r'(true|1)', value, re.IGNORECASE),
            value == 1
        ]):
            value = True
        elif any([
            re.match(r'(false|0)', value, re.IGNORECASE),
            value == 0
        ]):
            value = False
        else:
            raise ValueError("Bad Value for Bool %s" % value)
        return value

    def randomize(self):
        self.value_set(random.choice([True, False]))