Skip to content

Commit

Permalink
replace .format() with f strings
Browse files Browse the repository at this point in the history
  • Loading branch information
dieser-niko committed Dec 13, 2024
1 parent 0fba1a1 commit 4b486f8
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 45 deletions.
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class SpotifyClientCredentials that can be used to authenticate requests like so
playlists = sp.user_playlists('spotify')
while playlists:
for i, playlist in enumerate(playlists['items']):
print("{:4d} {} {}".format(i + 1 + playlists['offset'], playlist['uri'], playlist['name']))
print(f"{i + 1 + playlists['offset']:4d} {playlist['uri']} {playlist['name']}")
if playlists['next']:
playlists = sp.next(playlists)
else:
Expand Down
2 changes: 1 addition & 1 deletion examples/artist_discography.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def show_artist(artist):
logger.info(f'===={artist["name"]}====')
logger.info(f'Popularity: {artist["popularity"]}')
if len(artist['genres']) > 0:
logger.info('Genres: {}'.format(','.join(artist['genres'])))
logger.info(f"Genres: {', '.join(artist['genres'])}")


def main():
Expand Down
7 changes: 2 additions & 5 deletions examples/track_recommendations.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,5 @@

# Display the recommendations
for i, track in enumerate(recommendations['tracks']):
print(
"{}. {} by {}"
.format(i+1, track['name'], ', '
.join([artist['name'] for artist in track['artists']]))
)
print(f"{i+1}. {track['name']} by "
f"{', '.join([artist['name'] for artist in track['artists']])}")
19 changes: 7 additions & 12 deletions spotipy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ def _internal_call(self, method, url, payload, params):
if self.language is not None:
headers["Accept-Language"] = self.language

logger.debug('Sending {} to {} with Params: {} Headers: {} and Body: {!r} '.format(
method, url, args.get("params"), headers, args.get('data')))
logger.debug(f"Sending {method} to {url} with Params: "
f"{args.get('params')} Headers: {headers} and Body: {args.get('data')!r}")

try:
response = self._session.request(
Expand All @@ -289,11 +289,8 @@ def _internal_call(self, method, url, payload, params):
msg = response.text or None
reason = None

logger.error(
'HTTP Error for {} to {} with Params: {} returned {} due to {}'.format(
method, url, args.get("params"), response.status_code, msg
)
)
logger.error(f"HTTP Error for {method} to {url} with Params: "
f"{args.get('params')} returned {response.status_code} due to {msg}")

raise SpotifyException(
response.status_code,
Expand Down Expand Up @@ -2035,11 +2032,9 @@ def _is_uri(self, uri):
def _search_multiple_markets(self, q, limit, offset, type, markets, total):
if total and limit > total:
limit = total
warnings.warn(
"limit was auto-adjusted to equal {} as it must not be higher than total".format(
total),
UserWarning,
)
warnings.warn(f"limit was auto-adjusted to equal {total} "
f"as it must not be higher than total",
UserWarning)

results = defaultdict(dict)
item_types = [item_type + "s" for item_type in type.split(",")]
Expand Down
5 changes: 3 additions & 2 deletions spotipy/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ def __init__(self, http_status, code, msg, reason=None, headers=None):
self.headers = headers

def __str__(self):
return 'http status: {}, code:{} - {}, reason: {}'.format(
self.http_status, self.code, self.msg, self.reason)
return (f"http status: {self.http_status}, "
f"code: {self.code} - {self.msg}, "
f"reason: {self.reason}")


class SpotifyOauthError(SpotifyBaseException):
Expand Down
36 changes: 12 additions & 24 deletions spotipy/oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,8 @@ def _request_access_token(self):
self.client_id, self.client_secret
)

logger.debug(
"Sending POST request to {} with Headers: {} and Body: {!r}".format(
self.OAUTH_TOKEN_URL, headers, payload)
)
logger.debug(f"Sending POST request to {self.OAUTH_TOKEN_URL} with Headers: "
f"{headers} and Body: {payload}")

try:
response = self._session.post(
Expand Down Expand Up @@ -509,10 +507,8 @@ def get_access_token(self, code=None, as_dict=True, check_cache=True):

headers = self._make_authorization_headers()

logger.debug(
"Sending POST request to {} with Headers: {} and Body: {!r}".format(
self.OAUTH_TOKEN_URL, headers, payload)
)
logger.debug(f"Sending POST request to {self.OAUTH_TOKEN_URL} with Headers: "
f"{headers} and Body: {payload}")

try:
response = self._session.post(
Expand All @@ -539,10 +535,8 @@ def refresh_access_token(self, refresh_token):

headers = self._make_authorization_headers()

logger.debug(
"Sending POST request to {} with Headers: {} and Body: {!r}".format(
self.OAUTH_TOKEN_URL, headers, payload)
)
logger.debug(f"Sending POST request to {self.OAUTH_TOKEN_URL} with Headers: "
f"{headers} and Body: {payload}")

try:
response = self._session.post(
Expand Down Expand Up @@ -786,10 +780,8 @@ def _get_auth_response_interactive(self, open_browser=False):
prompt = "Enter the URL you were redirected to: "
else:
url = self.get_authorize_url()
prompt = (
"Go to the following URL: {}\n"
"Enter the URL you were redirected to: ".format(url)
)
prompt = (f"Go to the following URL: {url}\n"
f"Enter the URL you were redirected to: ")
response = self._get_user_input(prompt)
state, code = self.parse_auth_response_url(response)
if self.state is not None and self.state != state:
Expand Down Expand Up @@ -865,10 +857,8 @@ def get_access_token(self, code=None, check_cache=True):

headers = {"Content-Type": "application/x-www-form-urlencoded"}

logger.debug(
"Sending POST request to {} with Headers: {} and Body: {!r}".format(
self.OAUTH_TOKEN_URL, headers, payload)
)
logger.debug(f"Sending POST request to {self.OAUTH_TOKEN_URL} with Headers: "
f"{headers} and Body: {payload}")

try:
response = self._session.post(
Expand Down Expand Up @@ -896,10 +886,8 @@ def refresh_access_token(self, refresh_token):

headers = {"Content-Type": "application/x-www-form-urlencoded"}

logger.debug(
"Sending POST request to {} with Headers: {} and Body: {!r}".format(
self.OAUTH_TOKEN_URL, headers, payload)
)
logger.debug(f"Sending POST request to {self.OAUTH_TOKEN_URL} with Headers: "
f"{headers} and Body: {payload}")

try:
response = self._session.post(
Expand Down

0 comments on commit 4b486f8

Please sign in to comment.