Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/sqlalchemy_utils/primitives/weekdays.py: 58%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

33 statements  

1from ..utils import str_coercible 

2from .weekday import WeekDay 

3 

4 

5@str_coercible 

6class WeekDays: 

7 def __init__(self, bit_string_or_week_days): 

8 if isinstance(bit_string_or_week_days, str): 

9 self._days = set() 

10 

11 if len(bit_string_or_week_days) != WeekDay.NUM_WEEK_DAYS: 

12 raise ValueError( 

13 'Bit string must be {} characters long.'.format( 

14 WeekDay.NUM_WEEK_DAYS 

15 ) 

16 ) 

17 

18 for index, bit in enumerate(bit_string_or_week_days): 

19 if bit not in '01': 

20 raise ValueError('Bit string may only contain zeroes and ones.') 

21 if bit == '1': 

22 self._days.add(WeekDay(index)) 

23 elif isinstance(bit_string_or_week_days, WeekDays): 

24 self._days = bit_string_or_week_days._days 

25 else: 

26 self._days = set(bit_string_or_week_days) 

27 

28 def __eq__(self, other): 

29 if isinstance(other, WeekDays): 

30 return self._days == other._days 

31 elif isinstance(other, str): 

32 return self.as_bit_string() == other 

33 else: 

34 return NotImplemented 

35 

36 def __iter__(self): 

37 yield from sorted(self._days) 

38 

39 def __contains__(self, value): 

40 return value in self._days 

41 

42 def __repr__(self): 

43 return f'{self.__class__.__name__}({self.as_bit_string()!r})' 

44 

45 def __unicode__(self): 

46 return ', '.join(str(day) for day in self) 

47 

48 def as_bit_string(self): 

49 return ''.join( 

50 '1' if WeekDay(index) in self._days else '0' 

51 for index in range(WeekDay.NUM_WEEK_DAYS) 

52 )