Skip to content

Implement decorator to create read-only lazy attributes #28

Open

Description

@daniel-zullo-frequenz

What's needed?

Replace the pattern in _data_pipeline to create objects only once and that lives forever by creating a decorator (or selecting an existent one) to lazily create attributes and cache them for the lifetime of the _data_pipeline.

Proposed solution

Assess the different suggestions in these comments

We tried @cached_property and @lazy, but @cached_property is really complex and uses a lock (although it is removed in Python 3.12) and both allow writing to the property, and we need them to be read-only.

Use cases

Simplify code and improve code readability when there are many lazy attributes like in _data_pipeline

Alternatives and workarounds

Write ad-hoc code to do the lazy initialization.

Additional context

Originally posted in frequenz-floss/frequenz-sdk-python#799 (comment)

Activity

  1. added
    part:❓We need to figure out which part is affected
    priority:❓We need to figure out how soon this should be addressed
    type:enhancementNew feature or enhancement visitble to users
    priority:lowThis should be addressed only if there is nothing else on the table
    and removed
    priority:❓We need to figure out how soon this should be addressed
    part:❓We need to figure out which part is affected
    on Jan 30, 2024
  2. changed the title [-]Implement decorator to create lazy attributes[/-] [+]Implement decorator to create read-only lazy attributes[/+] on Feb 1, 2024
  3. added this to the Untriaged milestone on Jul 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority:lowThis should be addressed only if there is nothing else on the tabletype:enhancementNew feature or enhancement visitble to users

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions