Skip to content

OpenVariant New Feature | Replace classes import in process and builder #53

@dmartmillan

Description

@dmartmillan

Describe the feature

Replace the current dynamic class import for a Lazy Class Registry pattern with a load control.

The idea is similar to the current approach but introduces a Class Registry (two separate dictionaries that keep track of the available classes). These classes will be imported dynamically when needed. However, we can implement a load control method ("cache") to check whether a class has already been imported.

This will allow us to import only the classes that are needed and avoid repeatedly importing the same class, which is currently happening. It combines the efficiency of lazy loading with the benefits of caching, in a clean and maintainable interface.

Note: Python already caches the import of any module or class by itself. But our code has some problems: we importing (cached, but it produce a minimal overhead), getting the class (uses getattr to search module's namespace) and creating a new instance, every single time that we use a class (as shown in the code below). In the end, we are adding unnecessary overhead that could be avoided.

def import_class_from_module(module_name, class_name):
    """Import annotation class"""
    try:
        module = importlib.import_module(module_name)
        class_ = getattr(module, class_name)
        return class_        
    except (ModuleNotFoundError, AttributeError) as e:
        print(f"Error: {e}")
        return None


class_name = ann_type
module_name = "openvariant.annotation.process"
ClassAnnotation = import_class_from_module(module_name, class_name)
instance = ClassAnnotation()

header_schema.update({field: instance(ann, original_header, file_path, header_schema)})

code from: openvariant/variant/variant.py

If the approach works well, we can check a "similar" approach for the plugin system. 😈

Why it should be implemented?

Not expecting a significant improvement but it should make advantages on:

  • Avoid overhead from repeated imports (expected a better performace).
  • Keep code flow more stable, maintainable and type-safe.

Additionally, the changes will be minimal.

The best way to implement the new feature (optional)

No response

Contact details (optional)

No response

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions