Source code for cadrumo.domain.calculations.registry.period_selector_overlap
"""Whether two period selectors could both be live at once.The primitive every revision-overlap decision keys on. It reads selectorsonly, so the modelo schema's own validators and the revision-ordering helpersbuilt over typed revisions share one definition of overlap."""from__future__importannotationsfrom.schema_referencesimportPeriodSelector__all__=("period_selectors_overlap",)def_period_selector_year_bounds(selector:PeriodSelector)->tuple[int,int|None]:ifselector.years:returnmin(selector.years),max(selector.years)ifselector.year_fromisNone:return0,Nonereturnselector.year_from,selector.year_todef_overridden_years(left:PeriodSelector,right:PeriodSelector)->tuple[int,...]:returntuple(sorted({override.yearforselectorin(left,right)foroverrideinselector.period_overrides}))
[docs]defperiod_selectors_overlap(left:PeriodSelector,right:PeriodSelector)->bool:"""Whether the two selectors share a filing year and at least one period code in it. Overrides make the shared period surface year-dependent, so the shared years are decided in two bands rather than one. Each overridden year either side names is tested on its own surface, and the remaining shared years -- where both selectors serve their flat tuple -- are tested once, which keeps the decision finite for an open-ended ``year_from`` range. """left_start,left_end=_period_selector_year_bounds(left)right_start,right_end=_period_selector_year_bounds(right)ifleft_endisnotNoneandleft_end<right_start:returnFalseifright_endisnotNoneandright_end<left_start:returnFalseoverridden=_overridden_years(left,right)ifany(left.includes_year(year)andright.includes_year(year)andset(left.periods_for_year(year)).intersection(right.periods_for_year(year))foryearinoverridden):returnTrueifnotoverridden:returnbool(set(left.periods).intersection(right.periods))# The shared span still holds a year neither selector overrides whenever it# is wider than the overridden set, and there both serve their flat tuples.shared_start=max(left_start,right_start)declared_ends=[endforendin(left_end,right_end)ifendisnotNone]shared_end=min(declared_ends)ifdeclared_endselseNoneifshared_endisnotNone:shared_years={yearforyearinrange(shared_start,shared_end+1)ifleft.includes_year(year)andright.includes_year(year)}ifshared_years.issubset(overridden):returnFalsereturnbool(set(left.periods).intersection(right.periods))