devika icon indicating copy to clipboard operation
devika copied to clipboard

Update project.py

Open ImSuvodeep opened this issue 11 months ago • 3 comments

Optimized database transactions and streamlined message stack handling for improved efficiency and readability

ImSuvodeep avatar Mar 29 '24 16:03 ImSuvodeep

I think it would be easier to use the contextmanager from https://docs.python.org/3/library/contextlib.html instead of 'manually' changing sessions. But don't pin me on that.

from contextlib import contextmanager

class ProjectManager:
        
    @contextmanager
    def session_scope(self):
        session = Session(self.engine)
        try:
            yield session
            session.commit()
        except Exception:
            session.rollback()
            raise
        finally:
            session.close()
       # e.g
       def create_project(self, project: str):
         with self.session_scope() as session:
             self._add_project_state(session, project, [])

juvi21 avatar Mar 29 '24 18:03 juvi21

The context manager from the contextlib module provides a cleaner and more efficient way to handle file operations. By using the @contextmanager decorator, we can define a generator function that yields the file object within a with statement block. This ensures that the file is properly closed after its use, even if an exception occurs.

In the Feature class, I replaced the manual file handling with the open_file context manager. This simplifies the code and improves its readability. Additionally, I made minor enhancements to variable names and added logging to improve debugging capabilities.

ImSuvodeep avatar Mar 29 '24 18:03 ImSuvodeep

The conversation shows duplicate messages from Devika.

darrassi1 avatar Mar 29 '24 21:03 darrassi1

The conversation shows duplicate messages from Devika.

This modified version ensures that only unique messages are included in the message_stack by checking against a set of seen messages (seen_messages) and filtering out duplicates

ImSuvodeep avatar Mar 30 '24 04:03 ImSuvodeep

This what cause duplication `` def _get_latest_message(self, project_state, from_devika: bool): message_stack = self._get_message_stack(project_state) for message in reversed(message_stack): if message["from_devika"] == from_devika: return message

I tried this and the duplication of message from Devika disappear

def _get_latest_message(self, project_state, from_devika: bool):
        message_stack = self._get_message_stack(project_state)
        if projet_state:
           for message in reversed(message_stack):
              if message["from_devika"] == from_devika:
                  return message

darrassi1 avatar Mar 30 '24 04:03 darrassi1