Skip to content

Commit

Permalink
made Session a Context Manager for use in 'with' statement, fixes #1244
Browse files Browse the repository at this point in the history
… (#1245)
  • Loading branch information
jmao-denver authored Sep 10, 2021
1 parent 2f48837 commit 5c85fb0
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pyclient/pydeephaven/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class Session:
methods for asking the server to create tables, import Arrow data into tables, merge tables, run Python scripts, and
execute queries.
Session objects can be used in Python with statement so that whatever happens in the with statement block, they
are guaranteed to be closed upon exit.
Attributes:
tables (list[str]): names of the tables available in the server after running scripts
is_alive (bool): check if the session is still alive (may refresh the session)
Expand Down Expand Up @@ -71,6 +74,14 @@ def __init__(self, host: str = None, port: int = None, never_timeout: bool = Tru

self._connect()

def __enter__(self):
if not self.is_connected:
self._connect()
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.close()

@property
def tables(self):
return [t for t in self._tables if self._tables[t][0] == 'Table']
Expand Down

0 comments on commit 5c85fb0

Please sign in to comment.