Skip to content

Commit

Permalink
style: resolve ruff errors
Browse files Browse the repository at this point in the history
  • Loading branch information
korikuzma committed Apr 1, 2024
1 parent 4dd41f7 commit f477b0a
Show file tree
Hide file tree
Showing 22 changed files with 352 additions and 352 deletions.
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ wags-tails = ">=0.1.1"
psycopg = {version = "*", extras=["binary"]}
pytest = "*"
pre-commit = "*"
ruff = ">=0.1.2"
ruff = "==0.2.0"
pytest-cov = "*"
httpx = "*"
mock = "*"
Expand Down
6 changes: 3 additions & 3 deletions docs/scripts/generate_normalize_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ def create_gjgf(result: UnmergedNormalizationService) -> Dict:
}
}

for i, (source, matches) in enumerate(result.source_matches.items()):
for i, (_, matches) in enumerate(result.source_matches.items()):
for match in matches.records:
graph["graph"]["nodes"][match.concept_id] = {
"metadata": {
"color": COLORS[i],
"hover": f"{match.concept_id}\n{match.symbol}\n<i>{match.label}</i>", # noqa: E501
"click": f"<p color='black'>{json.dumps(match.model_dump(), indent=2)}</p>", # noqa: E501
"hover": f"{match.concept_id}\n{match.symbol}\n<i>{match.label}</i>",
"click": f"<p color='black'>{json.dumps(match.model_dump(), indent=2)}</p>",
}
}
for xref in match.xrefs:
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def linkcode_resolve(domain, info):
if not info["module"]:
return None
filename = info["module"].replace(".", "/")
return f"https://github.com/cancervariants/gene-normalization/blob/main/{filename}.py" # noqa: E501
return f"https://github.com/cancervariants/gene-normalization/blob/main/{filename}.py"


# -- code block style --------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions src/gene/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from os import environ
from pathlib import Path

from .version import __version__ # noqa: F401
from .version import __version__

APP_ROOT = Path(__file__).resolve().parent

Expand Down Expand Up @@ -53,7 +53,7 @@ class DownloadException(Exception): # noqa: N818
PREFIX_LOOKUP = {
v.value: SourceName[k].value
for k, v in NamespacePrefix.__members__.items()
if k in SourceName.__members__.keys()
if k in SourceName.__members__
}

# use to generate namespace prefix from source ID value
Expand Down
23 changes: 13 additions & 10 deletions src/gene/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Provides a CLI util to make updates to normalizer database."""
import logging
import os
from ast import literal_eval
from pathlib import Path
from timeit import default_timer as timer
from typing import Collection, List, Optional, Set
Expand Down Expand Up @@ -69,10 +70,10 @@ def update_from_remote(data_url: Optional[str], db_url: str) -> None:
except NotImplementedError:
click.echo(
f"Error: Fetching remote data dump not supported for {db.__class__.__name__}"
) # noqa: E501
)
click.get_current_context().exit(1)
except DatabaseException as e:
click.echo(f"Encountered exception during update: {str(e)}")
click.echo(f"Encountered exception during update: {e!s}")
click.get_current_context().exit(1)


Expand All @@ -92,18 +93,18 @@ def dump_database(output_directory: Path, db_url: str) -> None:
:param db_url: URL to normalizer database
""" # noqa: D301
if not output_directory:
output_directory = Path(".")
output_directory = Path()

db = create_db(db_url, False)
try:
db.export_db(output_directory)
except NotImplementedError:
click.echo(
f"Error: Dumping data to file not supported for {db.__class__.__name__}"
) # noqa: E501
)
click.get_current_context().exit(1)
except DatabaseException as e:
click.echo(f"Encountered exception during update: {str(e)}")
click.echo(f"Encountered exception during update: {e!s}")
click.get_current_context().exit(1)


Expand All @@ -122,7 +123,7 @@ def _update_normalizer(
:param use_existing: if True, use most recent local version of source data instead of
fetching from remote
"""
processed_ids = list()
processed_ids = []
for n in sources:
delete_time = _delete_source(n, db)
_load_source(n, db, delete_time, processed_ids, use_existing)
Expand Down Expand Up @@ -184,7 +185,7 @@ def _load_source(
f"Encountered ModuleNotFoundError attempting to import {e.name}. {_etl_dependency_help}"
)
click.get_current_context().exit()
SourceClass = eval(n.value) # noqa: N806
SourceClass = literal_eval(n.value) # noqa: N806

source = SourceClass(database=db, silent=False)
try:
Expand Down Expand Up @@ -297,19 +298,21 @@ def update_normalizer_db(
ctx = click.get_current_context()
click.echo(
"Must either enter 1 or more sources, or use `--update_all` parameter"
) # noqa: E501
)
click.echo(ctx.get_help())
ctx.exit()
else:
sources_split = sources.lower().split()

if len(sources_split) == 0:
raise Exception("Must enter 1 or more source names to update")
err_msg = "Must enter 1 or more source names to update"
raise Exception(err_msg)

non_sources = set(sources_split) - set(SOURCES)

if len(non_sources) != 0:
raise Exception(f"Not valid source(s): {non_sources}")
err_msg = f"Not valid source(s): {non_sources}"
raise Exception(err_msg)

parsed_source_names = {SourceName(SOURCES[s]) for s in sources_split}
_update_normalizer(parsed_source_names, db, update_merged, use_existing)
Expand Down
11 changes: 5 additions & 6 deletions src/gene/database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,11 @@ def _check_delete_okay() -> bool:
"""
if environ.get(AWS_ENV_VAR_NAME, "") == AwsEnvName.PRODUCTION:
if environ.get(SKIP_AWS_DB_ENV_NAME, "") == "true":
raise DatabaseWriteException(
f"Must unset {SKIP_AWS_DB_ENV_NAME} env variable to enable drop_db()" # noqa: E501
)
err_msg = f"Must unset {SKIP_AWS_DB_ENV_NAME} env variable to enable drop_db()"
raise DatabaseWriteException(err_msg)
return click.confirm("Are you sure you want to delete existing data?")
else:
return True

return True

@abc.abstractmethod
def drop_db(self) -> None:
Expand Down Expand Up @@ -324,7 +323,7 @@ def create_db(
else:
if db_url:
endpoint_url = db_url
elif "GENE_NORM_DB_URL" in environ.keys():
elif "GENE_NORM_DB_URL" in environ:
endpoint_url = environ["GENE_NORM_DB_URL"]
else:
endpoint_url = "http://localhost:8000"
Expand Down
102 changes: 52 additions & 50 deletions src/gene/database/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,18 @@ def __init__(self, db_url: Optional[str] = None, **db_args) -> None:

if AWS_ENV_VAR_NAME in environ:
if "GENE_TEST" in environ:
raise DatabaseInitializationException(
f"Cannot have both GENE_TEST and {AWS_ENV_VAR_NAME} set."
) # noqa: E501
err_msg = f"Cannot have both GENE_TEST and {AWS_ENV_VAR_NAME} set."
raise DatabaseInitializationException(err_msg)

aws_env = environ[AWS_ENV_VAR_NAME]
if aws_env not in VALID_AWS_ENV_NAMES:
raise DatabaseInitializationException(
f"{AWS_ENV_VAR_NAME} must be one of {VALID_AWS_ENV_NAMES}"
) # noqa: E501
err_msg = f"{AWS_ENV_VAR_NAME} must be one of {VALID_AWS_ENV_NAMES}"
raise DatabaseInitializationException(err_msg)

skip_confirmation = environ.get(SKIP_AWS_DB_ENV_NAME)
if (not skip_confirmation) or (
skip_confirmation and skip_confirmation != "true"
): # noqa: E501
):
confirm_aws_db_use(environ[AWS_ENV_VAR_NAME])

boto_params = {"region_name": region_name}
Expand Down Expand Up @@ -156,7 +154,7 @@ def check_schema_initialized(self) -> bool:
existing_tables = self.list_tables()
exists = self.gene_table in existing_tables
if not exists:
logger.info(f"{self.gene_table} table is missing or unavailable.")
logger.info("%s table is missing or unavailable.", self.gene_table)
return exists

def check_tables_populated(self) -> bool:
Expand Down Expand Up @@ -210,18 +208,17 @@ def get_source_metadata(self, src_name: Union[str, SourceName]) -> Dict:
src_name = src_name.value
if src_name in self._cached_sources:
return self._cached_sources[src_name]
else:
pk = f"{src_name.lower()}##source"
concept_id = f"source:{src_name.lower()}"
metadata = self.genes.get_item(
Key={"label_and_type": pk, "concept_id": concept_id}
).get("Item")
if not metadata:
raise DatabaseReadException(
f"Unable to retrieve data for source {src_name}"
)
self._cached_sources[src_name] = metadata
return metadata

pk = f"{src_name.lower()}##source"
concept_id = f"source:{src_name.lower()}"
metadata = self.genes.get_item(
Key={"label_and_type": pk, "concept_id": concept_id}
).get("Item")
if not metadata:
err_msg = f"Unable to retrieve data for source {src_name}"
raise DatabaseReadException(err_msg)
self._cached_sources[src_name] = metadata
return metadata

def get_record_by_id(
self, concept_id: str, case_sensitive: bool = True, merge: bool = False
Expand All @@ -246,17 +243,17 @@ def get_record_by_id(
Key={"label_and_type": pk, "concept_id": concept_id}
)
return match["Item"]
else:
exp = Key("label_and_type").eq(pk)
response = self.genes.query(KeyConditionExpression=exp)
record = response["Items"][0]
del record["label_and_type"]
return record

exp = Key("label_and_type").eq(pk)
response = self.genes.query(KeyConditionExpression=exp)
record = response["Items"][0]
del record["label_and_type"]
return record
except ClientError as e:
logger.error(
f"boto3 client error on get_records_by_id for "
f"search term {concept_id}: "
f"{e.response['Error']['Message']}"
"boto3 client error on get_records_by_id for search term %s: %s",
concept_id,
e.response["Error"]["Message"],
)
return None
except (KeyError, IndexError): # record doesn't exist
Expand All @@ -277,9 +274,9 @@ def get_refs_by_type(self, search_term: str, ref_type: RefType) -> List[str]:
return [m["concept_id"] for m in matches.get("Items", None)]
except ClientError as e:
logger.error(
f"boto3 client error on get_refs_by_type for "
f"search term {search_term}: "
f"{e.response['Error']['Message']}"
"boto3 client error on get_refs_by_type for search term %s: %s",
search_term,
e.response["Error"]["Message"],
)
return []

Expand Down Expand Up @@ -341,7 +338,7 @@ def get_all_records(self, record_type: RecordType) -> Generator[Dict, None, None
else:
if (
incoming_record_type == RecordType.IDENTITY
and not record.get("merge_ref") # noqa: E501
and not record.get("merge_ref")
) or incoming_record_type == RecordType.MERGER:
yield record
last_evaluated_key = response.get("LastEvaluatedKey")
Expand All @@ -364,7 +361,7 @@ def add_source_metadata(self, src_name: SourceName, metadata: SourceMeta) -> Non
try:
self.genes.put_item(Item=metadata_item)
except ClientError as e:
raise DatabaseWriteException(e)
raise DatabaseWriteException(e) from e

def add_record(self, record: Dict, src_name: SourceName) -> None:
"""Add new record to database.
Expand All @@ -381,8 +378,9 @@ def add_record(self, record: Dict, src_name: SourceName) -> None:
self.batch.put_item(Item=record)
except ClientError as e:
logger.error(
"boto3 client error on add_record for "
f"{concept_id}: {e.response['Error']['Message']}"
"boto3 client error on add_record for %s: %s",
concept_id,
e.response["Error"]["Message"],
)
for attr_type, item_type in ITEM_TYPES.items():
if attr_type in record:
Expand Down Expand Up @@ -413,8 +411,9 @@ def add_merged_record(self, record: Dict) -> None:
self.batch.put_item(Item=record)
except ClientError as e:
logger.error(
"boto3 client error on add_record for "
f"{concept_id}: {e.response['Error']['Message']}"
"boto3 client error on add_record for " "%s: %s",
concept_id,
e.response["Error"]["Message"],
)

def _add_ref_record(
Expand All @@ -439,9 +438,11 @@ def _add_ref_record(
self.batch.put_item(Item=record)
except ClientError as e:
logger.error(
f"boto3 client error adding reference {term} for "
f"{concept_id} with match type {ref_type}: "
f"{e.response['Error']['Message']}"
"boto3 client error adding reference %s for %s with match type %s: %s",
term,
concept_id,
ref_type,
e.response["Error"]["Message"],
)

def update_merge_ref(self, concept_id: str, merge_ref: Any) -> None: # noqa: ANN401
Expand All @@ -466,14 +467,15 @@ def update_merge_ref(self, concept_id: str, merge_ref: Any) -> None: # noqa: AN
except ClientError as e:
code = e.response.get("Error", {}).get("Code")
if code == "ConditionalCheckFailedException":
raise DatabaseWriteException(
err_msg = (
f"No such record exists for keys {label_and_type}, {concept_id}"
)
else:
logger.error(
f"boto3 client error in `database.update_record()`: "
f"{e.response['Error']['Message']}"
)
raise DatabaseWriteException(err_msg) from e

logger.error(
"boto3 client error in `database.update_record()`: %s",
e.response["Error"]["Message"],
)

def delete_normalized_concepts(self) -> None:
"""Remove merged records from the database. Use when performing a new update
Expand All @@ -495,7 +497,7 @@ def delete_normalized_concepts(self) -> None:
),
)
except ClientError as e:
raise DatabaseReadException(e)
raise DatabaseReadException(e) from e
records = response["Items"]
if not records:
break
Expand All @@ -522,7 +524,7 @@ def delete_source(self, src_name: SourceName) -> None:
KeyConditionExpression=Key("src_name").eq(src_name.value),
)
except ClientError as e:
raise DatabaseReadException(e)
raise DatabaseReadException(e) from e
records = response["Items"]
if not records:
break
Expand All @@ -538,7 +540,7 @@ def delete_source(self, src_name: SourceName) -> None:
}
)
except ClientError as e:
raise DatabaseWriteException(e)
raise DatabaseWriteException(e) from e

def complete_write_transaction(self) -> None:
"""Conclude transaction or batch writing if relevant."""
Expand Down
Loading

0 comments on commit f477b0a

Please sign in to comment.