Skip to content

Commit

Permalink
add strict to zip + improve TagWriter condition getters
Browse files Browse the repository at this point in the history
  • Loading branch information
geo-martino committed Jun 27, 2024
1 parent 0331c64 commit f3cdf9e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
28 changes: 14 additions & 14 deletions musify/libraries/local/track/_tags/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def write_title(self, source: Track, target: Track, replace: bool = False, dry_r
replace and source.title != target.title
]
if any(conditionals) and self._write_title(track=target, dry_run=dry_run):
return [i for i, c in enumerate(conditionals) if c][0]
return next(i for i, c in enumerate(conditionals) if c)

def _write_title(self, track: Track, dry_run: bool = True) -> bool:
"""
Expand Down Expand Up @@ -216,7 +216,7 @@ def write_artist(self, source: Track, target: Track, replace: bool = False, dry_
replace and source.artist != target.artist
]
if any(conditionals) and self._write_artist(track=target, dry_run=dry_run):
return [i for i, c in enumerate(conditionals) if c][0]
return next(i for i, c in enumerate(conditionals) if c)

def _write_artist(self, track: Track, dry_run: bool = True) -> bool:
"""
Expand Down Expand Up @@ -244,7 +244,7 @@ def write_album(self, source: Track, target: Track, replace: bool = False, dry_r
replace and source.album != target.album
]
if any(conditionals) and self._write_album(track=target, dry_run=dry_run):
return [i for i, c in enumerate(conditionals) if c][0]
return next(i for i, c in enumerate(conditionals) if c)

def _write_album(self, track: Track, dry_run: bool = True) -> bool:
"""
Expand Down Expand Up @@ -274,7 +274,7 @@ def write_album_artist(
replace and source.album_artist != target.album_artist
]
if any(conditionals) and self._write_album_artist(track=target, dry_run=dry_run):
return [i for i, c in enumerate(conditionals) if c][0]
return next(i for i, c in enumerate(conditionals) if c)

def _write_album_artist(self, track: Track, dry_run: bool = True) -> bool:
"""
Expand Down Expand Up @@ -303,7 +303,7 @@ def write_track(self, source: Track, target: Track, replace: bool = False, dry_r
replace and (source.track_number != target.track_number or source.track_total != target.track_total)
]
if any(conditionals) and self._write_track(track=target, dry_run=dry_run):
return [i for i, c in enumerate(conditionals) if c][0]
return next(i for i, c in enumerate(conditionals) if c)

def _write_track(self, track: Track, dry_run: bool = True) -> bool:
"""
Expand Down Expand Up @@ -340,7 +340,7 @@ def write_genres(self, source: Track, target: Track, replace: bool = False, dry_
"""
conditionals = [source.genres is None and bool(target.genres), replace and source.genres != target.genres]
if any(conditionals) and self._write_genres(track=target, dry_run=dry_run):
return [i for i, c in enumerate(conditionals) if c][0]
return next(i for i, c in enumerate(conditionals) if c)

def _write_genres(self, track: Track, dry_run: bool = True) -> bool:
"""
Expand Down Expand Up @@ -378,7 +378,7 @@ def write_date(
date, year, month, day = self._write_date(track=target, dry_run=dry_run)

updated = {}
condition = [i for i, c in enumerate(conditionals) if c][0]
condition = next(i for i, c in enumerate(conditionals) if c)
if date:
updated[Tags.DATE] = condition
if year:
Expand Down Expand Up @@ -433,7 +433,7 @@ def write_bpm(self, source: Track, target: Track, replace: bool = False, dry_run
]

if any(conditionals) and self._write_bpm(track=target, dry_run=dry_run):
return [i for i, c in enumerate(conditionals) if c][0]
return next(i for i, c in enumerate(conditionals) if c)

def _write_bpm(self, track: Track, dry_run: bool = True) -> bool:
"""
Expand All @@ -459,7 +459,7 @@ def write_key(self, source: Track, target: Track, replace: bool = False, dry_run
conditionals = [source.key is None and target.key is not None, replace and source.key != target.key]

if any(conditionals) and self._write_key(track=target, dry_run=dry_run):
return [i for i, c in enumerate(conditionals) if c][0]
return next(i for i, c in enumerate(conditionals) if c)

def _write_key(self, track: Track, dry_run: bool = True) -> bool:
"""
Expand Down Expand Up @@ -488,7 +488,7 @@ def write_disc(self, source: Track, target: Track, replace: bool = False, dry_ru
replace and (source.disc_number != target.disc_number or source.disc_total != target.disc_total)
]
if any(conditionals) and self._write_disc(track=target, dry_run=dry_run):
return [i for i, c in enumerate(conditionals) if c][0]
return next(i for i, c in enumerate(conditionals) if c)

def _write_disc(self, track: Track, dry_run: bool = True) -> bool:
"""
Expand Down Expand Up @@ -531,7 +531,7 @@ def write_compilation(
replace and source.compilation != target.compilation
]
if any(conditionals) and self._write_compilation(track=target, dry_run=dry_run):
return [i for i, c in enumerate(conditionals) if c][0]
return next(i for i, c in enumerate(conditionals) if c)

def _write_compilation(self, track: Track, dry_run: bool = True) -> bool:
"""
Expand Down Expand Up @@ -559,7 +559,7 @@ def write_comments(self, source: Track, target: Track, replace: bool = False, dr
replace and source.comments != target.comments
]
if any(conditionals) and self._write_comments(track=target, dry_run=dry_run):
return [i for i, c in enumerate(conditionals) if c][0]
return next(i for i, c in enumerate(conditionals) if c)

def _write_comments(self, track: Track, dry_run: bool = True) -> bool:
"""
Expand Down Expand Up @@ -589,7 +589,7 @@ def write_uri(self, source: Track, target: Track, replace: bool = False, dry_run
conditionals = [source.uri != target.uri or source.has_uri != target.has_uri]

if any(conditionals) and self._write_uri(track=target, dry_run=dry_run):
return [i for i, c in enumerate(conditionals) if c][0]
return next(i for i, c in enumerate(conditionals) if c)

def _write_uri(self, track: Track, dry_run: bool = True) -> bool:
"""
Expand Down Expand Up @@ -617,7 +617,7 @@ def write_images(self, source: Track, target: Track, replace: bool = False, dry_
conditionals = [source.has_image is False and bool(target.image_links), replace and bool(target.image_links)]

if any(conditionals) and self._write_images(track=target, dry_run=dry_run):
return [i for i, c in enumerate(conditionals) if c][0]
return next(i for i, c in enumerate(conditionals) if c)

@abstractmethod
def _write_images(self, track: Track, dry_run: bool = True) -> bool:
Expand Down
7 changes: 4 additions & 3 deletions musify/libraries/remote/spotify/api/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ async def _get_result(i: int, id_: str | list[str]) -> dict[str, Any]:
raise APIError(f"Given key {key!r} not found in response keys: {list(response.keys())}")

if key: # ensure identifiers are on each response so results can be sorted after execution
for i, r in zip(id_, response[key]):
for i, r in zip(id_, response[key], strict=True):
self._enrich_with_identifiers(response=r, id_=i, href=f"{url}/{i}")
else:
self._enrich_with_identifiers(response=response, id_=id_, href=f"{url}/{id_}")
Expand Down Expand Up @@ -497,8 +497,9 @@ async def _get_result(kind: str, url: str, key: str, batch: bool) -> dict[str, l
for result_map in await bar:
for key, responses in result_map.items():
responses.sort(key=lambda response: id_list.index(response[self.id_key]))
responses = [{self.id_key: response[self.id_key], key: response} for response in responses]
results = responses if not results else [rs | rp for rs, rp in zip(results, responses)]
responses = ({self.id_key: response[self.id_key], key: response} for response in responses)
results = list(responses) if not results \
else [rs | rp for rs, rp in zip(results, responses, strict=True)]

self._merge_results_to_input(original=values, responses=results, ordered=False, clear=False)
self._refresh_responses(responses=values, skip_checks=False)
Expand Down
3 changes: 1 addition & 2 deletions tests/libraries/local/playlist/test_xautopf.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,7 @@ async def test_parse_comparer(self, path: Path):

assert parser._get_xml_from_comparer() is not None # default value is given

assert len(comparers) == len(conditions)
for comparer, condition in zip(comparers, conditions):
for comparer, condition in zip(comparers, conditions, strict=True):
assert parser._get_xml_from_comparer(comparer) == condition

###########################################################################
Expand Down
4 changes: 2 additions & 2 deletions tests/libraries/remote/spotify/api/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ async def test_get_items_many_response(
factory({k: v for k, v in response.items() if k not in self.update_keys[object_type]}, skip_checks=True)
for response in responses.values()
]
for orig, ts in zip(original, test):
for orig, ts in zip(original, test, strict=True):
self.assert_different(orig.response, ts.response, key)

results = await api.get_items(values=test)
Expand All @@ -597,7 +597,7 @@ async def test_get_items_many_response(
object_type=object_type
)
if object_type in api.collection_item_map:
for orig, ts in zip(original, test):
for orig, ts in zip(original, test, strict=True):
self.assert_response_extended(actual=ts, expected=orig)

###########################################################################
Expand Down

0 comments on commit f3cdf9e

Please sign in to comment.