Skip to content

Commit

Permalink
🐬 Pass flake8/mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt committed Aug 29, 2021
1 parent a21fd29 commit 5cefdef
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 43 deletions.
9 changes: 6 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,17 @@ keywords =

[options]
install_requires =
# Missing itertools from the standard library you didn't know you needed
more_itertools
# Use progress bars excessively
tqdm
# Command line tools
click
more_click
# TODO your requirements go here
# Web requirements
flask
bootstrap-flask
flasgger
psutil
psycopg2-binary

# Random options
zip_safe = false
Expand Down
76 changes: 46 additions & 30 deletions src/biolookup/app/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
from collections import Counter, defaultdict
from functools import lru_cache
from pathlib import Path
from typing import Any, List, Mapping, Optional, Union

import pandas as pd
from sqlalchemy import create_engine, text
from sqlalchemy.engine import Engine
from tqdm import tqdm
from typing import Any, Callable, DefaultDict, Dict, List, Mapping, Optional, Union

import bioregistry
import pandas as pd
import pyobo
from pyobo import normalize_curie
from pyobo.constants import ALTS_TABLE_NAME, DEFS_TABLE_NAME, REFS_TABLE_NAME, get_sqlalchemy_uri
from pyobo.resource_utils import ensure_alts, ensure_definitions, ensure_ooh_na_na
from sqlalchemy import create_engine, text
from sqlalchemy.engine import Engine
from tqdm import tqdm


__all__ = [
"Backend",
Expand Down Expand Up @@ -182,23 +182,26 @@ def __init__(
self,
get_id_name_mapping,
get_alts_to_id,
summarize_names,
summarize_alts=None,
summarize_definitions=None,
summarize_names: Optional[Callable[[], Mapping[str, Any]]],
summarize_alts: Optional[Callable[[], Mapping[str, Any]]] = None,
summarize_definitions: Optional[Callable[[], Mapping[str, Any]]] = None,
get_id_definition_mapping=None,
) -> None:
"""Initialize the in-memory backend.
:param get_id_name_mapping: A function for getting id-name mappings
:param get_alts_to_id: A function for getting alts-id mappings
:param summarize_names: A function for summarizing
:param summarize_names: A function for summarizing references
:param summarize_alts: A function for summarizing alts
:param summarize_definitions: A function for summarizing definitions
:param get_id_definition_mapping: A function for getting id-def mappings
"""
self.get_id_name_mapping = get_id_name_mapping
self.get_alts_to_id = get_alts_to_id
self.get_id_definition_mapping = get_id_definition_mapping
self.summarize_names = summarize_names
self.summarize_alts = summarize_alts
self.summarize_definitions = summarize_definitions
self._summarize_names = summarize_names
self._summarize_alts = summarize_alts
self._summarize_definitions = summarize_definitions

def has_prefix(self, prefix: str) -> bool: # noqa:D102
return self.get_id_name_mapping(prefix) is not None
Expand All @@ -213,25 +216,36 @@ def get_name(self, prefix: str, identifier: str) -> Optional[str]: # noqa:D102

def get_definition(self, prefix: str, identifier: str) -> Optional[str]: # noqa:D102
if self.get_id_definition_mapping is None:
return
return None
id_definition_mapping = self.get_id_definition_mapping(prefix) or {}
return id_definition_mapping.get(identifier)

def count_prefixes(self) -> Optional[int]: # noqa:D102
if self.summarize_names:
return len(self.summarize_names().keys())
def summarize_names(self) -> Mapping[str, Any]: # noqa:D102
if self._summarize_names is None:
return {}
return self._summarize_names()

def count_names(self) -> Optional[int]: # noqa:D102
if self.summarize_names:
return sum(self.summarize_names().values())
def count_prefixes(self) -> int: # noqa:D102
return len(self.summarize_names().keys())

def count_alts(self) -> Optional[int]: # noqa:D102
if self.summarize_alts:
return sum(self.summarize_alts().values())
def count_names(self) -> int: # noqa:D102
return sum(self.summarize_names().values())

def count_definitions(self) -> Optional[int]: # noqa:D102
if self.summarize_definitions:
return sum(self.summarize_definitions().values())
def summarize_alts(self) -> Mapping[str, Any]: # noqa:D102
if self._summarize_alts is None:
return {}
return self._summarize_alts()

def count_alts(self) -> int: # noqa:D102
return sum(self.summarize_alts().values())

def summarize_definitions(self) -> Mapping[str, Any]: # noqa:D102
if self._summarize_definitions is None:
return {}
return self._summarize_definitions()

def count_definitions(self) -> int: # noqa:D102
return sum(self.summarize_definitions().values())


class RawSQLBackend(Backend):
Expand All @@ -251,8 +265,9 @@ def __init__(
"""Initialize the raw SQL backend.
:param refs_table: A name for the references (prefix-id-name) table. Defaults to 'obo_reference'
:param alts_table: A name for the alts (prefix-alt-id) table. Defaults to 'obo_alt'
:param engine:
:param alts_table: A name for the alts (prefix-id-alt) table. Defaults to 'obo_alt'
: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())
Expand Down Expand Up @@ -357,6 +372,7 @@ def _help_one(
result = connection.execute(sql, prefix=prefix, identifier=identifier).fetchone()
if result:
return result[0]
return None


def get_backend(
Expand Down Expand Up @@ -425,7 +441,7 @@ def get_backend(
def _get_lookup_from_df(
df: pd.DataFrame, desc: Optional[str] = None
) -> Mapping[str, Mapping[str, str]]:
lookup = defaultdict(dict)
lookup: DefaultDict[str, Dict[str, str]] = defaultdict(dict)
if desc is None:
desc = "processing mappings from df"
it = tqdm(df.values, total=len(df.index), desc=desc, unit_scale=True)
Expand All @@ -437,7 +453,7 @@ def _get_lookup_from_df(
def _get_lookup_from_path(
path: Union[str, Path], desc: Optional[str] = None
) -> Mapping[str, Mapping[str, str]]:
lookup = defaultdict(dict)
lookup: DefaultDict[str, Dict[str, str]] = defaultdict(dict)
if desc is None:
desc = "loading mappings"
with gzip.open(path, "rt") as file:
Expand Down
4 changes: 2 additions & 2 deletions src/biolookup/app/blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def resolve(curie: str):
required: true
type: string
example: doid:14330
"""
""" # noqa:DAR101,DAR201
logger.debug("resolving %s", curie)
start = time.time()
rv = backend.resolve(curie)
Expand All @@ -57,7 +57,7 @@ def size():
"""Return how much memory we're taking.
Doesn't work if you're running with Gunicorn because it makes child processes.
"""
""" # noqa:DAR201
try:
import psutil
except ImportError:
Expand Down
6 changes: 3 additions & 3 deletions src/biolookup/app/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def web(
],
columns=["prefix", "identifier", "name"],
)
click.echo(f"prepared {len(name_data):,} test names from {prefixes}")
click.echo(f"prepared {len(name_data.index):,} test names from {prefixes}") # type:ignore
alts_data = pd.DataFrame(
[
(prefix, alt, identifier)
Expand All @@ -86,7 +86,7 @@ def web(
],
columns=["prefix", "alt", "identifier"],
)
click.echo(f"prepared {len(alts_data):,} test alts from {prefixes}")
click.echo(f"prepared {len(alts_data.index):,} test alts from {prefixes}") # type:ignore
defs_data = pd.DataFrame(
[
(prefix, identifier, definition)
Expand All @@ -95,7 +95,7 @@ def web(
],
columns=["prefix", "identifier", "definition"],
)
click.echo(f"prepared {len(defs_data):,} test defs from {prefixes}")
click.echo(f"prepared {len(defs_data.index):,} test defs from {prefixes}") # type:ignore

app = get_app(
name_data=name_data,
Expand Down
18 changes: 13 additions & 5 deletions src/biolookup/app/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,21 @@ def get_app(
"""Build a flask app.
:param name_data: If none, uses the internal PyOBO loader. If a string, assumes is a gzip and reads a
dataframe from there. If a dataframe, uses it directly. Assumes data frame has 3 columns - prefix,
identifier, and name and is a TSV.
dataframe from there. If a dataframe, uses it directly. Assumes data frame has 3 columns - prefix,
identifier, and name and is a TSV.
:param alts_data: If none, uses the internal PyOBO loader. If a string, assumes is a gzip and reads a
dataframe from there. If a dataframe, uses it directly. Assumes data frame has 3 columns - prefix,
alt identifier, and identifier and is a TSV.
dataframe from there. If a dataframe, uses it directly. Assumes data frame has 3 columns - prefix,
alt identifier, and identifier and is a TSV.
:param defs_data: If none, uses the internal PyOBO loader. If a string, assumes is a gzip and reads a
dataframe from there. If a dataframe, uses it directly. Assumes data frame has 3 columns - prefix,
identifier identifier, and definition and is a TSV.
:param lazy: don't load the full cache into memory to run
:param sql_table: Use SQL-based backend
:param sql: use a remote SQL database
:param uri: If using a remote SQL database, specify a non-default connection string
:param refs_table: Name of the reference table in the SQL database
:param alts_table: Name of the alternative identifiers table in the SQL database
:param defs_table: Name of the definitions table in the SQL database
:return: A pre-built flask app.
"""
app = Flask(__name__)
Swagger(
Expand Down

0 comments on commit 5cefdef

Please sign in to comment.