Skip to content

Commit d73fb24

Browse files
for loop --> comprehension
1 parent 8f44797 commit d73fb24

1 file changed

Lines changed: 55 additions & 38 deletions

File tree

mpcontribs-client/mpcontribs/client/__init__.py

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -963,11 +963,16 @@ def _is_valid_payload(self, model: str, data: dict) -> None:
963963

964964
def _is_serializable_dict(self, dct: dict) -> None:
965965
"""Raise an error if an input dict is not JSON serializable."""
966-
for k, v in flatten(dct, reducer="dot").items():
967-
if v is not None and not isinstance(v, (str, int, float)):
968-
raise MPContribsClientError(
966+
try:
967+
raise MPContribsClientError(
968+
next(
969969
f"Value {v} of {type(v)} for key {k} not supported."
970+
for k, v in flatten(dct, reducer="dot").items()
971+
if v is not None and not isinstance(v, (str, int, float))
970972
)
973+
)
974+
except StopIteration:
975+
pass
971976

972977
def _get_per_page_default_max(
973978
self, op: str = "query", resource: str = "contributions"
@@ -1035,14 +1040,10 @@ def _split_query(
10351040

10361041
for q in queries:
10371042
# copy over missing parameters
1038-
for k, v in query.items():
1039-
if k not in q:
1040-
q[k] = v
1043+
q.update({k: v for k, v in query.items() if k not in q})
10411044

10421045
# comma-separated lists
1043-
for k, v in q.items():
1044-
if isinstance(v, list):
1045-
q[k] = ",".join(v)
1046+
q.update({k: ",".join(v) for k, v in q.items() if isinstance(v, list)})
10461047

10471048
return queries
10481049

@@ -1160,9 +1161,13 @@ def search_future(search_term):
11601161
if total_pages < 2:
11611162
return ret["data"]
11621163

1163-
for field in ["name__in", "_fields"]:
1164-
if field in query:
1165-
query[field] = ",".join(query[field])
1164+
query.update(
1165+
{
1166+
field: ",".join(query[field])
1167+
for field in ["name__in", "_fields"]
1168+
if field in query
1169+
}
1170+
)
11661171

11671172
queries = []
11681173

@@ -1175,8 +1180,7 @@ def search_future(search_term):
11751180
]
11761181
responses = _run_futures(futures, total=total_count, timeout=timeout)
11771182

1178-
for resp in responses.values():
1179-
ret["data"] += resp["result"]["data"]
1183+
ret["data"].extend([resp["result"]["data"] for resp in responses.values()])
11801184

11811185
return ret["data"]
11821186

@@ -1639,15 +1643,15 @@ def get_totals(
16391643
query = {k: v for k, v in query.items() if k not in skip_keys}
16401644
query["_fields"] = [] # only need totals -> explicitly request no fields
16411645
queries = self._split_query(query, resource=resource, op=op) # don't paginate
1642-
result = {"total_count": 0, "total_pages": 0}
16431646
futures = [
16441647
self._get_future(i, q, rel_url=resource) for i, q in enumerate(queries)
16451648
]
16461649
responses = _run_futures(futures, timeout=timeout, desc="Totals")
16471650

1648-
for resp in responses.values():
1649-
for k in result:
1650-
result[k] += resp.get("result", {}).get(k, 0)
1651+
result = {
1652+
k: sum(resp.get("result", {}).get(k, 0) for resp in responses.values())
1653+
for k in ("total_count", "total_pages")
1654+
}
16511655

16521656
return result["total_count"], result["total_pages"]
16531657

@@ -1743,9 +1747,13 @@ def get_all_ids(
17431747

17441748
unique_identifiers = self.get_unique_identifiers_flags()
17451749
data_id_fields = data_id_fields or {}
1746-
for k, v in data_id_fields.items():
1747-
if k in unique_identifiers and isinstance(v, str):
1748-
data_id_fields[k] = v
1750+
data_id_fields.update(
1751+
{
1752+
k: v
1753+
for k, v in data_id_fields.items()
1754+
if k in unique_identifiers and isinstance(v, str)
1755+
}
1756+
)
17491757

17501758
ret = {}
17511759
query = query or {}
@@ -1808,26 +1816,32 @@ def get_all_ids(
18081816
if data_id_field and data_id_field_val:
18091817
ret[project][identifier][data_id_field] = data_id_field_val
18101818

1811-
for component in components:
1812-
if component in contrib:
1813-
ret[project][identifier][component] = {
1819+
ret[project][identifier].update(
1820+
{
1821+
component: {
18141822
d["name"]: {"id": d["id"], "md5": d["md5"]}
18151823
for d in contrib[component]
18161824
}
1825+
for component in components
1826+
if component in contrib
1827+
}
1828+
)
18171829

18181830
elif data_id_field and data_id_field_val:
18191831
ret[project][identifier] = {
18201832
data_id_field_val: {"id": contrib["id"]}
18211833
}
18221834

1823-
for component in components:
1824-
if component in contrib:
1825-
ret[project][identifier][data_id_field_val][
1826-
component
1827-
] = {
1835+
ret[project][identifier][data_id_field_val].update(
1836+
{
1837+
component: {
18281838
d["name"]: {"id": d["id"], "md5": d["md5"]}
18291839
for d in contrib[component]
18301840
}
1841+
for component in components
1842+
if component in contrib
1843+
}
1844+
)
18311845

18321846
return ret
18331847

@@ -1859,12 +1873,11 @@ def query_contributions(
18591873
query["project"] = self.project
18601874

18611875
if paginate:
1862-
cids = []
1863-
1864-
for v in self.get_all_ids(query).values():
1865-
cids_project = v.get("ids")
1866-
if cids_project:
1867-
cids.extend(cids_project)
1876+
cids = [
1877+
idx
1878+
for v in self.get_all_ids(query).values()
1879+
for idx in (v.get("ids") or [])
1880+
]
18681881

18691882
if not cids:
18701883
raise MPContribsClientError("No contributions match the query.")
@@ -2118,9 +2131,13 @@ def submit_contributions(
21182131
resp = self.get_all_ids(dict(id__in=collect_ids), timeout=timeout)
21192132
project_names |= set(resp.keys())
21202133

2121-
for project_name, values in resp.items():
2122-
for cid in values["ids"]:
2123-
id2project[cid] = project_name
2134+
id2project.update(
2135+
{
2136+
cid: project_name
2137+
for project_name, values in resp.items()
2138+
for cid in values["ids"]
2139+
}
2140+
)
21242141

21252142
existing = defaultdict(dict)
21262143
unique_identifiers = defaultdict(dict)

0 commit comments

Comments
 (0)