Base types#
Core types exported from the lactuca top-level namespace.
TableKey#
- class lactuca.TableKey(table_name: str, sex: str, cohort: int | None = None, duration: TableDurationKey | None = None, unisex_blend: float | None = None)#
Bases:
NamedTupleImmutable key identifying a single actuarial table instance.
Used as a dictionary key when
__new__()is called withreturn_dict=True. The five fields together uniquely identify any table that Lactuca can construct.- Parameters:
table_name (str) – Repository name of the actuarial table (e.g.
'PASEM2020_Dec_1o').sex (str) – Biological sex code:
'm'(male),'f'(female), or'u'(unisex blend).cohort (int, optional) – Birth-year for generational (projected) tables.
Nonefor static period tables. Default isNone.duration (int or
"ult", optional) – Select-table duration index, or"ult"for the ultimate segment.Nonefor tables without a select period. Default isNone. Validated by__new__(), not byTableKeyitself — see Notes.unisex_blend (float, optional) – Male weight in \([0, 1]\) used to build the unisex blend.
Nonewhensex != 'u'. Default isNone.
Notes
TableKeyis aNamedTupleand therefore hashable. It can be used as a dictionary key or set element without any extra configuration.Equality and hashing are field-wise: two
TableKeyobjects are equal when all five fields compare equal.Passing
unisex_blend=Noneexplicitly is identical to omitting the argument — both produce the same key. This preserves backward compatibility when upgrading code that does not use unisex blending.Float precision caveat:
unisex_blendis stored as a Pythonfloat. Two blends that differ only in floating-point representation noise will produce different keys. Always pass the same literal (e.g.0.5) to guarantee key consistency.Manual construction:
TableKeyperforms no validation onduration,cohort, orunisex_blend. Values such as an invaliddurationstring are stored as given and may fail to match keys from__new__()withreturn_dict=True, which validates those fields before building keys. Preferreturn_dict=Trueor mirror its validation rules when constructing keys by hand.
Examples
Construct a minimal key (table name and sex only):
>>> from lactuca import TableKey >>> k = TableKey('PASEM2020_Dec_1o', 'm') >>> k.table_name, k.sex, k.cohort, k.duration, k.unisex_blend ('PASEM2020_Dec_1o', 'm', None, None, None)
Use as a dictionary key to retrieve table instances:
>>> d = {TableKey('T1', 'm'): 'inst_m', TableKey('T1', 'f'): 'inst_f'} >>> d[TableKey('T1', 'm')] 'inst_m'
Keys with different
unisex_blendvalues are not equal:>>> TableKey('T1', 'u', None, None, 0.55) == TableKey('T1', 'u', None, None, 0.45) False
- cohort: int | None#
Alias for field number 2
- duration: int | Literal['ult'] | None#
Alias for field number 3
- sex: str#
Alias for field number 1
- table_name: str#
Alias for field number 0
- unisex_blend: float | None#
Alias for field number 4