zimm0140 / AI-Playground

AI PC starter app for doing AI image creation, image stylizing, and chatbot on a PC powered by an Intel® Arc™ GPU.
MIT License
0 stars 0 forks source link

Refactor Classes to Use Dataclasses for Improved Readability and Efficiency #4

Open SZim92 opened 2 months ago

SZim92 commented 2 months ago

Description:

To enhance code readability, reduce boilerplate code, and improve maintainability, classes will be refactored to use Python's dataclasses module where appropriate. Dataclasses provide a decorator and functions for automatically adding special methods such as __init__(), __repr__(), and __eq__() to user-defined classes.

Objectives:

  1. Identify key classes that can be refactored to use dataclasses.
  2. Refactor identified classes to use the dataclass decorator.
  3. Ensure all unit tests pass after the refactoring.

Tasks:

  1. Identify Key Classes:

    • Review the codebase to identify classes that can benefit from being refactored to dataclasses.
  2. Refactor to Dataclasses:

    • Add the @dataclass decorator to identified classes.
    • Remove boilerplate code that is automatically handled by dataclasses.
    • Ensure all class attributes are properly annotated with types.
  3. Verify Changes:

    • Run all existing unit tests to ensure they pass.
    • Verify that the refactored classes behave as expected.

Implementation Plan:

  1. Identify Classes to Refactor:

    • Review the codebase manually or use tools to identify classes that can be refactored to dataclasses.
  2. Example Refactoring:

    • Current Code:

      class ModelDownloaderApi:
       def __init__(self):
           self.file_queue = list()
           self.fs = HfFileSystem()
           self.total_size = 0
      
       def get_info(self, repo_id: str, is_sd: bool = False) -> None:
           self.repo_id = repo_id
           self.repo_folder = repo_id.replace('/', '---')
           self.file_queue.clear()
           self.total_size = 0
           self.enum_file_list(repo_id, is_sd, True)
           print(dumps({"total_size": self.total_size, "file_list": self.file_queue}))
    • Refactored Code Using Dataclasses:

      from dataclasses import dataclass, field
      from typing import List
      
      @dataclass
      class ModelDownloaderApi:
       repo_id: str = ""
       file_queue: List[dict] = field(default_factory=list)
       total_size: int = 0
       fs: HfFileSystem = field(default_factory=HfFileSystem)
       repo_folder: str = ""
      
       def get_info(self, repo_id: str, is_sd: bool = False) -> None:
           self.repo_id = repo_id
           self.repo_folder = repo_id.replace('/', '---')
           self.file_queue.clear()
           self.total_size = 0
           self.enum_file_list(repo_id, is_sd, True)
           print(dumps({"total_size": self.total_size, "file_list": self.file_queue}))
  3. Verify with Unit Tests:

    • Run pytest to execute all unit tests and ensure that the refactored code behaves correctly.

Steps to Reproduce:

  1. Clone the repository:
    git clone <repository-url>
  2. Navigate to the directory containing the code to be refactored.
  3. Identify classes that can be converted to dataclasses.
  4. Refactor the classes by adding the @dataclass decorator and removing boilerplate code.
  5. Run the tests to verify the changes:
    pytest

Expected Behavior:

Current Behavior:

Additional Information:

Progress Log: