devika
devika copied to clipboard
Update project.py
Optimized database transactions and streamlined message stack handling for improved efficiency and readability
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, [])
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.
The conversation shows duplicate messages from Devika.
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
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