Skip to content
Permalink
Browse files

Use pydash with nested dictionaries

  • Loading branch information...
autophagy committed Apr 18, 2019
1 parent a516e63 commit 16af00f1ebed9054bf300e8c5410e418bb4131a1
@@ -1,6 +1,7 @@
from collections import namedtuple
from typing import Optional

from pydash import get
from scieldas.api import API
from scieldas.services import ServiceAPI

@@ -18,7 +19,7 @@ def build(
if branch:
api.add("branch", branch)
details = api.get()
return details.get("build", {}).get("status")
return get(details, "build.status")

@ServiceAPI.call
def tests(
@@ -28,7 +29,7 @@ def tests(
api.add(user, repo)
if branch:
api.add("branch", branch)
jobs = api.get().get("build", {}).get("jobs")
jobs = get(api.get(), "build.jobs")
for job in jobs:
total += job.get("testsCount")
passed += job.get("passedTestsCount")
@@ -1,5 +1,6 @@
from typing import Optional

from pydash import get
from scieldas.api import API
from scieldas.services import ServiceAPI

@@ -14,5 +15,4 @@ def coverage(
api.add(vcs, user, repo)
if branch:
api.add("branches", branch)
details = api.get()
return float(details.get("commit", {}).get("totals", {}).get("c"))
return float(get(api.get(), "commit.totals.c"))
@@ -1,5 +1,6 @@
from typing import Optional

from pydash import get
from scieldas.api import API
from scieldas.services import ServiceAPI

@@ -11,12 +12,12 @@ class Crates(ServiceAPI):
def downloads(self, crate: str, api: API, version: str = None) -> Optional[int]:
if version:
crate_info = api.add(crate, version).get()
return crate_info.get("version", {}).get("downloads")
return get(crate_info, "version.downloads")
else:
crate_info = api.add(crate).get()
return crate_info.get("crate", {}).get("downloads")
return get(crate_info, "crate.downloads")

@ServiceAPI.call
def version(self, crate: str, api: API) -> Optional[str]:
crate_info = api.add(crate).get()
return crate_info.get("crate", {}).get("max_version")
return get(crate_info, "crate.max_version")
@@ -1,5 +1,6 @@
from typing import Dict, Optional

from pydash import get
from scieldas.api import API
from scieldas.services import ServiceAPI

@@ -35,7 +36,7 @@ def license(self, owner: str, repo: str, token: str, api: API) -> Optional[str]:
"Authorization": f"token {token}",
}
)
return repo_info.get("license", {}).get("name")
return get(repo_info, "license.name")

@ServiceAPI.call
def stars(self, owner: str, repo: str, token: str, api: API) -> Optional[int]:
@@ -1,5 +1,6 @@
from typing import Optional

from pydash import get
from scieldas.api import API
from scieldas.services import ServiceAPI

@@ -11,16 +12,16 @@ class Mastodon(ServiceAPI):
def users(self, instance: str, api: API) -> Optional[int]:
api.base_url = self.base_url.format(instance)
instance_info = api.add("instance").get()
return instance_info.get("stats", {}).get("user_count")
return get(instance_info, "stats.user_count")

@ServiceAPI.call
def statuses(self, instance: str, api: API) -> Optional[int]:
api.base_url = self.base_url.format(instance)
instance_info = api.add("instance").get()
return instance_info.get("stats", {}).get("status_count")
return get(instance_info, "stats.status_count")

@ServiceAPI.call
def domains(self, instance: str, api: API) -> Optional[int]:
api.base_url = self.base_url.format(instance)
instance_info = api.add("instance").get()
return instance_info.get("stats", {}).get("domain_count")
return get(instance_info, "stats.domain_count")
@@ -1,5 +1,6 @@
from typing import Optional

from pydash import get
from scieldas.api import API
from scieldas.services import ServiceAPI

@@ -20,9 +21,9 @@ def users(self, addon: str, api: API) -> Optional[str]:
@ServiceAPI.call
def version(self, addon: str, api: API) -> Optional[str]:
addon_info = api.add("addons", "addon", addon).get()
return addon_info.get("current_version", {}).get("version")
return get(addon_info, "current_version.version")

@ServiceAPI.call
def rating(self, addon: str, api: API) -> float:
addon_info = api.add("addons", "addon", addon).get()
return addon_info.get("ratings", {}).get("average")
return get(addon_info, "ratings.average")
@@ -1,6 +1,7 @@
import re
from typing import List, Optional

from pydash import get
from scieldas.api import API
from scieldas.services import ServiceAPI

@@ -11,7 +12,7 @@ class PyPi(ServiceAPI):
@ServiceAPI.call
def version(self, project: str, api: API) -> Optional[str]:
pypi_json = api.add(project, "json").get()
return pypi_json.get("info", {}).get("version")
return get(pypi_json, "info.version")

@staticmethod
def _format_pyversions(classifiers: List[str]) -> str:
@@ -103,7 +104,7 @@ def _format_license(classifiers: List[str]) -> Optional[str]:
@ServiceAPI.call
def license(self, project: str, api: API) -> Optional[str]:
pypi_json = api.add(project, "json").get()
return self._format_license(pypi_json.get("info", {}).get("classifiers"))
return self._format_license(get(pypi_json, "info.classifiers"))


class PyPiStats(ServiceAPI):
@@ -113,4 +114,4 @@ class PyPiStats(ServiceAPI):
def downloads(self, period: str, project: str, api: API) -> Optional[int]:
periods = {"day": "last_day", "week": "last_week", "month": "last_month"}
recent_stats = api.add("packages", project, "recent").get()
return recent_stats.get("data", {}).get(periods.get(period, period))
return get(recent_stats, f"data.{periods.get(period, period)}")
@@ -1,5 +1,6 @@
from typing import Optional

from pydash import get
from scieldas.api import API
from scieldas.services import ServiceAPI

@@ -16,5 +17,4 @@ def build_status(
travis_project = api.add("repos", user, project, "branches", branch).get(
headers={"Authorization": "token {}".format(token)}
)
state = travis_project.get("branch", {}).get("state")
return state_map.get(state)
return state_map.get(get(travis_project, "branch.state"))
@@ -28,6 +28,7 @@
"SQLAlchemy==1.3.1",
"Flask-SQLAlchemy==2.3.2",
"psycopg2==2.8.1",
"pydash==4.7.4",
],
extras_require={
"testing": ["black==18.9b0", "flake8==3.7.7", "mypy==0.670", "isort==4.3.15"]

0 comments on commit 16af00f

Please sign in to comment.
You can’t perform that action at this time.