File: weekdays.py

package info (click to toggle)
python-sqlalchemy-utils 0.41.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,252 kB
  • sloc: python: 13,566; makefile: 141
file content (57 lines) | stat: -rw-r--r-- 1,764 bytes parent folder | download | duplicates (2)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from ..utils import str_coercible
from .weekday import WeekDay


@str_coercible
class WeekDays:
    def __init__(self, bit_string_or_week_days):
        if isinstance(bit_string_or_week_days, str):
            self._days = set()

            if len(bit_string_or_week_days) != WeekDay.NUM_WEEK_DAYS:
                raise ValueError(
                    'Bit string must be {} characters long.'.format(
                        WeekDay.NUM_WEEK_DAYS
                    )
                )

            for index, bit in enumerate(bit_string_or_week_days):
                if bit not in '01':
                    raise ValueError(
                        'Bit string may only contain zeroes and ones.'
                    )
                if bit == '1':
                    self._days.add(WeekDay(index))
        elif isinstance(bit_string_or_week_days, WeekDays):
            self._days = bit_string_or_week_days._days
        else:
            self._days = set(bit_string_or_week_days)

    def __eq__(self, other):
        if isinstance(other, WeekDays):
            return self._days == other._days
        elif isinstance(other, str):
            return self.as_bit_string() == other
        else:
            return NotImplemented

    def __iter__(self):
        yield from sorted(self._days)

    def __contains__(self, value):
        return value in self._days

    def __repr__(self):
        return '{}({!r})'.format(
            self.__class__.__name__,
            self.as_bit_string()
        )

    def __unicode__(self):
        return ', '.join(str(day) for day in self)

    def as_bit_string(self):
        return ''.join(
            '1' if WeekDay(index) in self._days else '0'
            for index in range(WeekDay.NUM_WEEK_DAYS)
        )