Skip to content

Commit

Permalink
Migrate code style to Black (#1268)
Browse files Browse the repository at this point in the history
  • Loading branch information
manasaV3 committed Sep 22, 2023
1 parent 927f8ae commit a3c4aa2
Show file tree
Hide file tree
Showing 21 changed files with 434 additions and 332 deletions.
2 changes: 1 addition & 1 deletion napari-hub-commons/setup.py
Expand Up @@ -8,7 +8,7 @@

def get_install_requires():
lib_folder = os.path.dirname(os.path.realpath(__file__))
requirement_path = lib_folder + '/requirements.txt'
requirement_path = lib_folder + "/requirements.txt"
install_requires = []
if os.path.isfile(requirement_path):
with open(requirement_path) as f:
Expand Down
2 changes: 1 addition & 1 deletion napari-hub-commons/src/nhcommons/__init__.py
@@ -1 +1 @@
from nhcommons import (utils, models)
from nhcommons import utils, models
20 changes: 9 additions & 11 deletions napari-hub-commons/src/nhcommons/models/activity_helper.py
Expand Up @@ -10,21 +10,21 @@ def _get_first_of_last_month() -> datetime:
return datetime.combine(
datetime.today().replace(day=1) - relativedelta(months=1),
datetime.min.time(),
timezone.utc
timezone.utc,
)


def build_timeline_query_parameters(
plugin: str,
range_key_format: str,
month_delta: int,
range_key_attribute: Attribute,
plugin: str,
range_key_format: str,
month_delta: int,
range_key_attribute: Attribute,
) -> Dict[str, Any]:
start_date = _get_first_of_last_month()
end_date = start_date - relativedelta(months=month_delta - 1)
condition = range_key_attribute.between(
range_key_format.format(timestamp=end_date),
range_key_format.format(timestamp=start_date)
range_key_format.format(timestamp=start_date),
)
return {
"hash_key": plugin.lower(),
Expand All @@ -33,7 +33,7 @@ def build_timeline_query_parameters(


def process_timeline_results(
results: Dict[int, int], month_delta: int, activity_value_key: str
results: Dict[int, int], month_delta: int, activity_value_key: str
) -> List[Dict[str, int]]:
start_datetime = _get_first_of_last_month()
dates = [
Expand All @@ -44,14 +44,12 @@ def process_timeline_results(


def _timestamp_to_entry(
value_by_timestamp: Dict[int, int],
activity_value_key: str,
timestamp: int = None
value_by_timestamp: Dict[int, int], activity_value_key: str, timestamp: int = None
) -> Union[Dict[str, int], partial]:
if timestamp is None:
return partial(_timestamp_to_entry, value_by_timestamp, activity_value_key)

return {
"timestamp": timestamp,
activity_value_key: value_by_timestamp.get(timestamp, 0)
activity_value_key: value_by_timestamp.get(timestamp, 0),
}
5 changes: 3 additions & 2 deletions napari-hub-commons/src/nhcommons/models/github_activity.py
Expand Up @@ -4,7 +4,8 @@
from typing import Dict, Any, List, Optional, Iterator
from pynamodb.attributes import UnicodeAttribute, NumberAttribute
from nhcommons.models.activity_helper import (
build_timeline_query_parameters, process_timeline_results
build_timeline_query_parameters,
process_timeline_results,
)
from nhcommons.models.pynamo_helper import set_ddb_metadata, PynamoWrapper

Expand Down Expand Up @@ -94,7 +95,7 @@ def _query_for_timeline(plugin: str, repo: str, month_delta: int) -> Dict[int, i
plugin,
f"MONTH:{{timestamp:%Y%m}}:{repo}",
month_delta,
_GitHubActivity.type_identifier
_GitHubActivity.type_identifier,
)
query_params["filter_condition"] = _GitHubActivity.repo == repo
return {row.timestamp: row.commit_count for row in _query_table(query_params)}
Expand Down
19 changes: 10 additions & 9 deletions napari-hub-commons/src/nhcommons/models/install_activity.py
Expand Up @@ -3,16 +3,17 @@
import time
from datetime import date
from functools import reduce
from typing import (Dict, Any, List, Iterator)
from typing import Dict, Any, List, Iterator

from dateutil.relativedelta import relativedelta
from pynamodb.attributes import (UnicodeAttribute, NumberAttribute)
from pynamodb.attributes import UnicodeAttribute, NumberAttribute
from pynamodb.indexes import GlobalSecondaryIndex, IncludeProjection

from nhcommons.models.activity_helper import (
build_timeline_query_parameters, process_timeline_results
build_timeline_query_parameters,
process_timeline_results,
)
from nhcommons.models.pynamo_helper import (set_ddb_metadata, PynamoWrapper)
from nhcommons.models.pynamo_helper import set_ddb_metadata, PynamoWrapper

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -97,13 +98,13 @@ def get_recent_installs(plugin: str, day_delta: int) -> int:

query_params = {
"hash_key": plugin.lower(),
"range_key_condition": _InstallActivity.type_timestamp.between(lower, upper)
"range_key_condition": _InstallActivity.type_timestamp.between(lower, upper),
}

return reduce(
lambda acc, count: acc + count,
[row.install_count for row in _query_table(query_params)],
0
0,
)


Expand All @@ -119,7 +120,7 @@ def get_timeline(plugin: str, month_delta: int) -> List[Dict[str, int]]:
plugin,
f"MONTH:{{timestamp:%Y%m}}",
month_delta,
_InstallActivity.type_timestamp
_InstallActivity.type_timestamp,
)
results = {row.timestamp: row.install_count for row in _query_table(query_params)}
return process_timeline_results(results, month_delta, "installs")
Expand All @@ -133,12 +134,12 @@ def get_total_installs_by_plugins() -> Dict[str, int]:
start = time.perf_counter()
try:
iterator = _InstallActivity.total_installs.scan(
attributes_to_get=["plugin_name", "install_count"]
attributes_to_get=["plugin_name", "install_count"]
)
return {item.plugin_name: item.install_count for item in iterator}
finally:
duration = (time.perf_counter() - start) * 1000
logging.info(f'scan duration={duration}ms')
logging.info(f"scan duration={duration}ms")


def _query_table(kwargs: dict) -> Iterator[_InstallActivity]:
Expand Down
52 changes: 35 additions & 17 deletions napari-hub-commons/src/nhcommons/models/plugin.py
Expand Up @@ -48,15 +48,33 @@ class Meta:


_INDEX_SUBSET = {
"authors", "category", "code_repository", "description_content_type",
"description_text", "development_status", "display_name", "error_message",
"first_released", "license", "name", "npe2", "operating_system",
"plugin_types", "python_version", "reader_file_extensions", "release_date",
"summary", "version", "writer_file_extensions", "writer_save_layers"
"authors",
"category",
"code_repository",
"description_content_type",
"description_text",
"development_status",
"display_name",
"error_message",
"first_released",
"license",
"name",
"npe2",
"operating_system",
"plugin_types",
"python_version",
"reader_file_extensions",
"release_date",
"summary",
"version",
"writer_file_extensions",
"writer_save_layers",
}


def get_index(visibility_filter: Optional[Set[PluginVisibility]]) -> List[Dict[str, Any]]:
def get_index(
visibility_filter: Optional[Set[PluginVisibility]],
) -> List[Dict[str, Any]]:
return _scan_latest_plugins_index(
attributes=["name", "version", "data", "visibility"],
mapper=_index_list_mapper(),
Expand All @@ -72,8 +90,7 @@ def get_latest_plugins() -> Dict[str, str]:


def get_latest_plugin(
name: str,
visibilities: Optional[Set[PluginVisibility]]
name: str, visibilities: Optional[Set[PluginVisibility]]
) -> Dict[str, Any]:
plugin = _query_for_latest_plugin(
name, ["data", "release_date"], _to_visibility_condition(visibilities)
Expand All @@ -93,7 +110,7 @@ def get_plugin_name_by_repo() -> Dict[str, str]:


def get_plugin_by_version(
name: str, version: str, visibilities: Optional[Set[PluginVisibility]]
name: str, version: str, visibilities: Optional[Set[PluginVisibility]]
) -> Dict[str, Any]:
kwargs = {
"attributes_to_get": ["data", "release_date"],
Expand Down Expand Up @@ -137,7 +154,8 @@ def _to_dict(item: _Plugin, data: Dict) -> Dict:
def _mapper(plugins: Iterator[_Plugin]) -> List[Dict[str, Any]]:
return [
_to_dict(item, data)
for item in plugins if item.data and (data := item.data.as_dict())
for item in plugins
if item.data and (data := item.data.as_dict())
]

return _mapper
Expand All @@ -150,9 +168,9 @@ def _get_latest(plugins: Iterator[_Plugin]) -> Optional[_Plugin]:


def _query_for_latest_plugin(
name: str,
attributes_to_get: List[str],
filter_condition: Optional[Condition] = None,
name: str,
attributes_to_get: List[str],
filter_condition: Optional[Condition] = None,
) -> Optional[_Plugin]:
kwargs = {
"hash_key": name,
Expand All @@ -163,9 +181,9 @@ def _query_for_latest_plugin(


def _scan_latest_plugins_index(
attributes: List[str],
mapper: Callable[[ResultIterator[_Plugin]], T],
filter_conditions: Optional[Condition] = None
attributes: List[str],
mapper: Callable[[ResultIterator[_Plugin]], T],
filter_conditions: Optional[Condition] = None,
) -> T:
result = {}
start = time.perf_counter()
Expand All @@ -192,7 +210,7 @@ def _to_repo(plugin: _Plugin) -> Optional[str]:


def _to_visibility_condition(
visibilities: Optional[Set[PluginVisibility]]
visibilities: Optional[Set[PluginVisibility]],
) -> Optional[Condition]:
if not visibilities:
return None
Expand Down
10 changes: 5 additions & 5 deletions napari-hub-commons/src/nhcommons/models/plugin_metadata.py
Expand Up @@ -69,17 +69,17 @@ def get_existing_types(plugin: str, version: str) -> Set[PluginMetadataType]:


def query(
plugin: str, version: str = None, version_type: str = None
plugin: str, version: str = None, version_type: str = None
) -> List[Dict[str, Any]]:
results = _query(name=plugin, version=version, version_type=version_type)
return [result.to_dict() for result in results]


def _query(
name: str,
version: Optional[str] = None,
version_type: Optional[str] = None,
projection: Optional[List[str]] = None
name: str,
version: Optional[str] = None,
version_type: Optional[str] = None,
projection: Optional[List[str]] = None,
) -> Union[ResultIterator[_PluginMetadata], List]:
if not name or (not version and not version_type):
return []
Expand Down
9 changes: 5 additions & 4 deletions napari-hub-commons/src/nhcommons/models/plugin_utils.py
Expand Up @@ -2,7 +2,6 @@


class PluginMetadataType(Enum):

DISTRIBUTION = auto()
PYPI = auto()
METADATA = auto()
Expand All @@ -13,12 +12,14 @@ def to_version_type(self, version: str) -> str:

class PluginVisibilityMeta(EnumMeta):
def __contains__(cls, item):
return isinstance(item, cls) or \
isinstance(item, str) and item.upper() in cls._member_names_
return (
isinstance(item, cls)
or isinstance(item, str)
and item.upper() in cls._member_names_
)


class PluginVisibility(Enum, metaclass=PluginVisibilityMeta):

PUBLIC = auto()
HIDDEN = auto()
DISABLED = auto()
Expand Down
11 changes: 4 additions & 7 deletions napari-hub-commons/src/nhcommons/models/pynamo_helper.py
Expand Up @@ -9,19 +9,16 @@


class PynamoWrapper(Model):

last_updated_timestamp = NumberAttribute(
default_for_new=get_current_timestamp
)
last_updated_timestamp = NumberAttribute(default_for_new=get_current_timestamp)


def get_stack_name() -> str:
return os.getenv("STACK_NAME", "local")


def set_ddb_metadata(table_name: str,
dynamo_model_cls: Optional[PynamoWrapper] = None)\
-> Union[PynamoWrapper, partial]:
def set_ddb_metadata(
table_name: str, dynamo_model_cls: Optional[PynamoWrapper] = None
) -> Union[PynamoWrapper, partial]:
"""
Sets up the Meta class of dynamo model with required values
:returns Union[PynamoWrapper, functools.partial]: if all parameters available
Expand Down
17 changes: 9 additions & 8 deletions napari-hub-commons/src/nhcommons/tests/models/conftest.py
Expand Up @@ -40,11 +40,9 @@ def _create_dynamo_table(pynamo_ddb_model: PynamoWrapper, table_name: str):
# reinitializing to override properties with mock environ value
pynamo_ddb_model = set_ddb_metadata(table_name, pynamo_ddb_model)
pynamo_ddb_model.create_table()
return boto3\
.resource("dynamodb",
region_name=AWS_REGION,
endpoint_url=LOCAL_DYNAMO_HOST) \
.Table(f"{STACK_NAME}-{table_name}")
return boto3.resource(
"dynamodb", region_name=AWS_REGION, endpoint_url=LOCAL_DYNAMO_HOST
).Table(f"{STACK_NAME}-{table_name}")

return _create_dynamo_table

Expand Down Expand Up @@ -74,8 +72,10 @@ def is_match(expected) -> bool:
if len(diff_items) != 1:
continue
diff = diff_items.pop()
if diff[0] == "last_updated_timestamp" and \
start_time <= diff[1] <= end_time:
if (
diff[0] == "last_updated_timestamp"
and start_time <= diff[1] <= end_time
):
return True
return False

Expand All @@ -98,8 +98,9 @@ def _generate(
ts = first_of_month - relativedelta(months=i)
entry = {
"timestamp": int(ts.timestamp()) * 1000,
value_key: values_by_ts.get(i, 0)
value_key: values_by_ts.get(i, 0),
}
timeline.append(entry)
return timeline

return _generate

0 comments on commit a3c4aa2

Please sign in to comment.