diff --git a/src/biolookup/app/backends.py b/src/biolookup/app/backends.py index 9d22bd0..9891838 100644 --- a/src/biolookup/app/backends.py +++ b/src/biolookup/app/backends.py @@ -203,51 +203,71 @@ def __init__( self._summarize_alts = summarize_alts self._summarize_definitions = summarize_definitions - def has_prefix(self, prefix: str) -> bool: # noqa:D102 + def has_prefix(self, prefix: str) -> bool: + """Check for the prefix using the id/name getter.""" return self.get_id_name_mapping(prefix) is not None - def get_primary_id(self, prefix: str, identifier: str) -> str: # noqa:D102 + def get_primary_id(self, prefix: str, identifier: str) -> str: + """Get the canonical identifier with the alts/id getter.""" alts_to_id = self.get_alts_to_id(prefix) or {} return alts_to_id.get(identifier, identifier) - def get_name(self, prefix: str, identifier: str) -> Optional[str]: # noqa:D102 + def get_name(self, prefix: str, identifier: str) -> Optional[str]: + """Get the name with the id/name getter.""" id_name_mapping = self.get_id_name_mapping(prefix) or {} return id_name_mapping.get(identifier) - def get_definition(self, prefix: str, identifier: str) -> Optional[str]: # noqa:D102 + def get_definition(self, prefix: str, identifier: str) -> Optional[str]: + """Get the name with the id/definition getter, if available.""" if self.get_id_definition_mapping is None: return None id_definition_mapping = self.get_id_definition_mapping(prefix) or {} return id_definition_mapping.get(identifier) - def summarize_names(self) -> Mapping[str, Any]: # noqa:D102 + def summarize_names(self) -> Mapping[str, Any]: + """Summarize the names with the internal name summary function, if available.""" if self._summarize_names is None: return {} return self._summarize_names() - def count_prefixes(self) -> int: # noqa:D102 + def count_prefixes(self) -> int: + """Count prefixes using the name summary.""" return len(self.summarize_names().keys()) - def count_names(self) -> int: # noqa:D102 + def count_names(self) -> int: + """Count names using the name summary.""" return sum(self.summarize_names().values()) - def summarize_alts(self) -> Mapping[str, Any]: # noqa:D102 + def summarize_alts(self) -> Mapping[str, Any]: + """Summarize the alts with the internal alt summary function, if available.""" if self._summarize_alts is None: return {} return self._summarize_alts() - def count_alts(self) -> int: # noqa:D102 + def count_alts(self) -> int: + """Count alts using the alt summary.""" return sum(self.summarize_alts().values()) - def summarize_definitions(self) -> Mapping[str, Any]: # noqa:D102 + def summarize_definitions(self) -> Mapping[str, Any]: + """Summarize the definitions with the internal definition summary function, if available.""" if self._summarize_definitions is None: return {} return self._summarize_definitions() - def count_definitions(self) -> int: # noqa:D102 + def count_definitions(self) -> int: + """Count definitions using the definition summary.""" return sum(self.summarize_definitions().values()) +def _ensure_engine(engine: Union[None, str, Engine]) -> Engine: + if engine is None: + return create_engine(get_sqlalchemy_uri()) + elif isinstance(engine, str): + return create_engine(engine) + else: + return engine + + class RawSQLBackend(Backend): """A backend that communicates with low-level SQL statements.""" @@ -269,26 +289,22 @@ def __init__( :param defs_table: A name for the defs (prefix-id-def) table. Defaults to 'obo_def' :param engine: An engine, connection string, or None if you want the default. """ - if engine is None: - self.engine = create_engine(get_sqlalchemy_uri()) - elif isinstance(engine, str): - self.engine = create_engine(engine) - else: - self.engine = engine + self.engine = _ensure_engine(engine) self.refs_table = refs_table or REFS_TABLE_NAME self.alts_table = alts_table or ALTS_TABLE_NAME self.defs_table = defs_table or DEFS_TABLE_NAME @lru_cache(maxsize=1) - def count_names(self) -> int: # noqa:D102 + def count_names(self) -> int: """Get the number of names.""" return self._get_one( f"SELECT SUM(identifier_count) FROM {self.refs_table}_summary;" # noqa:S608 ) @lru_cache(maxsize=1) - def count_prefixes(self) -> int: # noqa:D102 + def count_prefixes(self) -> int: + """Count prefixes using a SQL query to the references summary table.""" logger.info("counting prefixes") start = time.time() rv = self._get_one( @@ -298,14 +314,15 @@ def count_prefixes(self) -> int: # noqa:D102 return rv @lru_cache(maxsize=1) - def count_definitions(self) -> int: # noqa:D102 - """Get the number of definitions.""" + def count_definitions(self) -> int: + """Count definitions using a SQL query to the definitions summary table.""" return self._get_one( f"SELECT SUM(identifier_count) FROM {self.defs_table}_summary;" # noqa:S608 ) @lru_cache(maxsize=1) - def count_alts(self) -> Optional[int]: # noqa:D102 + def count_alts(self) -> Optional[int]: + """Count alts using a SQL query to the alts summary table.""" logger.info("counting alts") return self._get_one(f"SELECT COUNT(*) FROM {self.alts_table};") # noqa:S608 @@ -314,13 +331,16 @@ def _get_one(self, sql: str): result = connection.execute(sql).fetchone() return result[0] - def summarize_names(self) -> Counter: # noqa:D102 + def summarize_names(self) -> Counter: + """Return the results of a SQL query that dumps the name summary table.""" return self._get_summary(self.refs_table) - def summarize_alts(self) -> Counter: # noqa:D102 + def summarize_alts(self) -> Counter: + """Return the results of a SQL query that dumps the alts summary table.""" return self._get_summary(self.alts_table) - def summarize_definitions(self) -> Counter: # noqa:D102 + def summarize_definitions(self) -> Counter: + """Return the results of a SQL query that dumps the definitions summary table.""" return self._get_summary(self.defs_table) @lru_cache() @@ -330,7 +350,8 @@ def _get_summary(self, table) -> Counter: return Counter(dict(connection.execute(sql).fetchall())) @lru_cache() - def has_prefix(self, prefix: str) -> bool: # noqa:D102 + def has_prefix(self, prefix: str) -> bool: + """Check for the prefix with a SQL query.""" sql = text( f"SELECT EXISTS(SELECT 1 from {self.refs_table} WHERE prefix = :prefix);" # noqa:S608 ) @@ -339,7 +360,8 @@ def has_prefix(self, prefix: str) -> bool: # noqa:D102 return bool(result) @lru_cache(maxsize=100_000) - def get_primary_id(self, prefix: str, identifier: str) -> str: # noqa:D102 + def get_primary_id(self, prefix: str, identifier: str) -> str: + """Get the canonical identifier with a SQL query to the alts table.""" sql = text( f""" SELECT identifier @@ -351,16 +373,16 @@ def get_primary_id(self, prefix: str, identifier: str) -> str: # noqa:D102 result = connection.execute(sql, prefix=prefix, alt=identifier).fetchone() return result[0] if result else identifier - def get_name(self, prefix: str, identifier: str) -> Optional[str]: # noqa:D102 + def get_name(self, prefix: str, identifier: str) -> Optional[str]: + """Get the name with a SQL query to the names table.""" return self._help_one(self.refs_table, "name", prefix, identifier) - def get_definition(self, prefix: str, identifier: str) -> Optional[str]: # noqa:D102 + def get_definition(self, prefix: str, identifier: str) -> Optional[str]: + """Get the definition with a SQL query to the definitions table.""" return self._help_one(self.defs_table, "definition", prefix, identifier) @lru_cache(maxsize=100_000) - def _help_one( - self, table: str, column: str, prefix: str, identifier: str - ) -> Optional[str]: # noqa:D102 + def _help_one(self, table: str, column: str, prefix: str, identifier: str) -> Optional[str]: sql = text( f""" SELECT {column} diff --git a/src/biolookup/app/wsgi.py b/src/biolookup/app/wsgi.py index 2084e01..3648d46 100644 --- a/src/biolookup/app/wsgi.py +++ b/src/biolookup/app/wsgi.py @@ -104,7 +104,7 @@ def get_app( app.register_blueprint(biolookup_blueprint) @app.before_first_request - def before_first_request(): + def _before_first_request(): logger.info("before_first_request") backend.count_all()