Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

made Session a Context Manager for use in 'with' statement, fixes #1244 #1245

Merged
merged 1 commit into from
Sep 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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