22import dataclasses as dc
33import datetime as dt
44import heapq
5- import itertools as it
65import operator as op
76import tzlocal
87from typing import Any , ClassVar , Dict , Generic , Iterator , Iterable , Optional , Type , TypeVar , Tuple
98
9+ SENTINEL = object ()
10+
1011
1112def unique (iterable : Iterable [Any ]) -> Iterator [Any ]:
1213 """
@@ -18,7 +19,7 @@ def unique(iterable: Iterable[Any]) -> Iterator[Any]:
1819
1920 it = iter (iterable )
2021
21- prev = None
22+ prev = SENTINEL
2223 for val in it :
2324 if val == prev :
2425 continue
@@ -296,7 +297,7 @@ def __init__(self, monthday_field: Field[MonthdayRange], weekday_field: Field[W
296297 def __iter__ (self ) -> Iterator [int ]:
297298 return self .iter ()
298299
299- def iter (self , year : Optional [int ] = None , month : Optional [int ] = None , start_from : Optional [ int ] = None ) -> Iterator [int ]:
300+ def iter (self , year : Optional [int ] = None , month : Optional [int ] = None , start_from : int = 1 ) -> Iterator [int ]:
300301 """
301302 Returns iterator over month days and week days values of a particular month and year starting from `start_from`.
302303
@@ -311,21 +312,18 @@ def iter(self, year: Optional[int] = None, month: Optional[int] = None, start_fr
311312 month = now .month if month is None else month
312313
313314 if self ._weekday_field .is_default :
314- day_iter = self ._monthday_iter (year , month )
315+ day_iter = self ._monthday_iter (year , month , start_from )
315316 elif self ._monthday_field .is_default :
316- day_iter = self ._weekday_iter (year , month )
317+ day_iter = self ._weekday_iter (year , month , start_from )
317318 else :
318- day_iter = heapq .merge (self ._monthday_iter (year , month ), self ._weekday_iter (year , month ))
319-
320- if start_from is not None :
321- day_iter = it .dropwhile (lambda value : value < start_from , day_iter )
319+ day_iter = heapq .merge (self ._monthday_iter (year , month , start_from ), self ._weekday_iter (year , month , start_from ))
322320
323321 return unique (day_iter )
324322
325- def _monthday_iter (self , year : int , month : int ) -> Iterator [int ]:
326- for day in self ._monthday_field :
323+ def _monthday_iter (self , year : int , month : int , start_from : int = 1 ) -> Iterator [int ]:
324+ for day in self ._monthday_field . iter ( start_from = start_from ) :
327325 if day > calendar .monthrange (year , month )[1 ]:
328- continue
326+ break
329327
330328 yield day
331329
@@ -334,7 +332,8 @@ def _weekday_iter(self, year: int, month: int, start_day: int = 1) -> Iterator[i
334332 curr_weekday = calendar .weekday (year , month , curr_day ) + 1
335333 weekday_iter = self ._weekday_field .iter (start_from = curr_weekday )
336334
337- for _ in range (6 ):
335+ max_weeks_in_month = 6
336+ for _ in range (max_weeks_in_month ):
338337 for weekday in weekday_iter :
339338 curr_day += (weekday - curr_weekday )
340339 curr_weekday += (weekday - curr_weekday )
0 commit comments