icalendar.cal.lazy module#
Components for lazy parsing of components.
- class icalendar.cal.lazy.InitialSubcomponentsStrategy[source]#
Bases:
objectInitial strategy for the calendar.
No subcomponents.
- set_components(components)[source]#
Set the subcomponents, switching to lazy parsing.
- Parameters:
components (
list[Component]) – The subcomponents to store. Must be empty for an uninitialised calendar.- Raises:
ValueError – If
componentsis not empty. Parse the calendar first or useLazyCalendar.add_component()instead.- Return type:
- class icalendar.cal.lazy.LazyCalendar(*args, **kwargs)[source]#
Bases:
CalendarA calendar that parses subcomponents lazily for memory efficiency.
Subcomponents of this calendar are parsed only when accessed, allowing the calendar to handle large files without consuming too much memory or time. All calendar-level properties are parsed immediately; subcomponents and their properties are deferred.
Examples
By accessing the
eventsof the calendar, onlyEventandTimezoneare immediately parsed.>>> from icalendar import LazyCalendar >>> calendar = LazyCalendar.example("issue_1050_all_components") >>> len(calendar.events) == 1 True
The calendar's subcomponents were not parsed because they were not accessed. The calendar is still lazy.
>>> calendar.is_lazy() True
When you access all
subcomponentsof the calendar, for example by getting their count, the entire calendar is parsed and becomes not lazy.>>> len(calendar.subcomponents) 5 >>> calendar.is_lazy() False
See also
Initialize the calendar.
- add_component(component)[source]#
Add a component to this calendar.
This adds a subcomponent without parsing the entire calendar. Use this, instead of appending to
subcomponentswhich forces all subcomponents to be parsed first.- Parameters:
component (
Component) – The component to add as a subcomponent.
See also
- Return type:
- is_lazy()[source]#
Whether the subcomponents are still deferred and not yet parsed.
Returns
Trueif subcomponents have not been accessed yet. ReturnsFalseonce all subcomponents have been parsed, for example, by accessingsubcomponents.Note
If you believe the calendar parses more subcomponents than it should, please open an issue.
- Return type:
- Returns:
Trueif subcomponent parsing is deferred.Falseif all subcomponents have been parsed.
- property subcomponents: list[Component]#
Parse and return all subcomponents of this calendar.
Accessing this property triggers the parsing of all deferred subcomponents. Once accessed, the calendar is no longer lazy.
You can manipulate the returned list or set it to replace all subcomponents. Setting the list does not re-enable lazy parsing.
- Returns:
A list of parsed subcomponents.
See also
- with_uid(uid)[source]#
Return subcomponents matching the given UID without parsing all subcomponents.
This searches lazily, parsing only the minimal subcomponents needed to find matches. If this calendar's own UID matches, it is included as the first element.
- Parameters:
uid (
str) – The UID to search for.- Return type:
- Returns:
A list of components whose UID matches, with the calendar itself first if it matches.
See also
- class icalendar.cal.lazy.LazySubcomponentsStrategy[source]#
Bases:
objectParse subcomponents only when accessed.
- add_component(component)[source]#
Add a component to the calendar without parsing it.
- Parameters:
component (
Component|LazySubcomponent) – The component to add.- Return type:
- Returns:
This strategy with the component added.
- property as_parsed: ParsedSubcomponentsStrategy#
Return a parsed strategy with all subcomponents parsed.
- Returns:
A
ParsedSubcomponentsStrategywith all subcomponents.
- get_all_components()[source]#
Get the subcomponents of the calendar, parsing all of them.
- Return type:
- Returns:
A tuple of a parsed strategy and the list of subcomponents.
- initial_components_to_parse: tuple[str, ...] = ('VTIMEZONE',)#
Parse these subcomponents before any others.
- parse_initial_components()[source]#
Parse the components that are required by other components.
This mainly concerns the timezone components. They are required by other components that have a TZID parameter.
- Return type:
- set_components(components)[source]#
Set the subcomponents of the calendar.
- Parameters:
- Return type:
- Returns:
A
ParsedSubcomponentsStrategyholding the components.
- walk(name)[source]#
Get the subcomponents of the calendar with the given name.
Parse only the minimal number of subcomponents.
- class icalendar.cal.lazy.ParsedSubcomponentsStrategy[source]#
Bases:
objectAll the subcomponents are parsed and available as a list.
- add_component(component)[source]#
Add a component to the calendar, parsing it immediately.
- Parameters:
component (
Component) – The component to add.- Return type:
- Returns:
This strategy with the component added.
- get_all_components()[source]#
Get the parsed subcomponents of the calendar.
- Return type:
- Returns:
A tuple of this strategy and the list of parsed subcomponents.
- is_lazy()[source]#
Return
Falsebecause subcomponents are not lazily parsed.- Return type:
Literal[False]
- set_components(components)[source]#
Set the subcomponents of the calendar.