Skip to content

Commit

Permalink
Ensure cursor still around for repeat use of feature matrix extractor…
Browse files Browse the repository at this point in the history
… object.
  • Loading branch information
jimmymathews committed Jul 27, 2023
1 parent 4a12e87 commit d7f73ee
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions spatialprofilingtoolbox/db/feature_matrix_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Convenience provision of a feature matrix for each study, the data retrieved from the SPT database.
"""
import sys
from typing import cast

import pandas as pd
from psycopg2.extensions import cursor as Psycopg2Cursor
Expand All @@ -22,7 +21,7 @@ class FeatureMatrixExtractor:
Pull from the database and create convenience bundle of feature matrices and metadata.
"""

cursor: Psycopg2Cursor
cursor: Psycopg2Cursor | None
database_config_file: str | None

def __init__(self,
Expand All @@ -36,22 +35,24 @@ def __init__(self,
message = 'A cursor and database configuration file were both specified. Using the '\
'cursor.'
logger.warning(message)
self.cursor = cast(Psycopg2Cursor, cursor)
self.cursor = cursor
self.database_config_file = database_config_file

def extract(self,
specimen: str | None=None,
study: str | None=None,
continuous_also: bool=False,
):
extraction = None
if self.cursor is not None:
return self._extract(specimen=specimen, study=study, continuous_also=continuous_also)
extraction = self._extract(specimen=specimen, study=study, continuous_also=continuous_also)
if self.database_config_file is not None:
with DatabaseConnectionMaker(self.database_config_file) as dcm:
with dcm.get_connection().cursor() as cursor:
self.cursor = cursor
return self._extract(specimen=specimen, study=study, continuous_also=continuous_also)
return None
extraction = self._extract(specimen=specimen, study=study, continuous_also=continuous_also)
self.cursor = None
return extraction

def _extract(self,
specimen: str | None=None,
Expand Down

0 comments on commit d7f73ee

Please sign in to comment.