Skip to content

Commit b43383b

Browse files
authored
feat(pkg-py): Add .cleanup() method (#121)
* Close #106. Add cleanup method to Python * Address feedback * More docstring polish
1 parent 0c278e7 commit b43383b

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

pkg-py/src/querychat/_datasource.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ def get_data(self) -> pd.DataFrame:
8383
"""
8484
...
8585

86+
@abstractmethod
87+
def cleanup(self) -> None:
88+
"""
89+
Clean up resources associated with the data source.
90+
91+
This method should clean up any connections or resources used by the
92+
data source.
93+
94+
Returns
95+
-------
96+
None
97+
98+
"""
99+
86100

87101
class DataFrameSource(DataSource):
88102
"""A DataSource implementation that wraps a pandas DataFrame using DuckDB."""
@@ -212,6 +226,18 @@ def get_data(self) -> pd.DataFrame:
212226
# TODO(@gadenbuie): This should just return `self._df` and not a pandas DataFrame
213227
return self._df.lazy().collect().to_pandas()
214228

229+
def cleanup(self) -> None:
230+
"""
231+
Close the DuckDB connection.
232+
233+
Returns
234+
-------
235+
None
236+
237+
"""
238+
if self._conn:
239+
self._conn.close()
240+
215241

216242
class SQLAlchemySource(DataSource):
217243
"""
@@ -440,3 +466,15 @@ def _get_sql_type_name(self, type_: sqltypes.TypeEngine) -> str: # noqa: PLR091
440466
def _get_connection(self) -> Connection:
441467
"""Get a connection to use for queries."""
442468
return self._engine.connect()
469+
470+
def cleanup(self) -> None:
471+
"""
472+
Dispose of the SQLAlchemy engine.
473+
474+
Returns
475+
-------
476+
None
477+
478+
"""
479+
if self._engine:
480+
self._engine.dispose()

pkg-py/src/querychat/_querychat.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,20 @@ def data_source(self):
273273
"""
274274
return self._data_source
275275

276+
def cleanup(self) -> None:
277+
"""
278+
Clean up resources associated with the data source.
279+
280+
Call this method when you are done using the QueryChat object to close
281+
database connections and avoid resource leaks.
282+
283+
Returns
284+
-------
285+
None
286+
287+
"""
288+
self._data_source.cleanup()
289+
276290

277291
class QueryChat(QueryChatBase):
278292
"""

0 commit comments

Comments
 (0)