From 37fa42773041347cf0ce8e3d6f962708dfe4202a Mon Sep 17 00:00:00 2001 From: Mike Taves Date: Wed, 18 Oct 2023 12:19:00 +1300 Subject: [PATCH] Run pyupgrade --- cdsapi/__init__.py | 1 - cdsapi/api.py | 37 +++++++++---------- examples/example-era5-update.py | 4 +- example-era5.py => examples/example-era5.py | 0 .../example-glaciers.py | 0 5 files changed, 19 insertions(+), 23 deletions(-) rename example-era5.py => examples/example-era5.py (100%) rename example-glaciers.py => examples/example-glaciers.py (100%) diff --git a/cdsapi/__init__.py b/cdsapi/__init__.py index c9e9d4e..f025e33 100644 --- a/cdsapi/__init__.py +++ b/cdsapi/__init__.py @@ -16,7 +16,6 @@ # granted to it by virtue of its status as an intergovernmental organisation nor # does it submit to any jurisdiction. -from __future__ import absolute_import, division, print_function, unicode_literals from . import api diff --git a/cdsapi/api.py b/cdsapi/api.py index bb0138f..4a11e26 100644 --- a/cdsapi/api.py +++ b/cdsapi/api.py @@ -6,7 +6,6 @@ # granted to it by virtue of its status as an intergovernmental organisation nor # does it submit to any jurisdiction. -from __future__ import absolute_import, division, print_function, unicode_literals import json import logging @@ -31,7 +30,7 @@ def bytes_to_string(n): while n >= 1024: n /= 1024.0 i += 1 - return "%g%s" % (int(n * 10 + 0.5) / 10.0, u[i]) + return f"{int(n * 10 + 0.5) / 10.0:g}{u[i]}" def read_config(path): @@ -62,7 +61,7 @@ def toJSON(obj): return obj -class Result(object): +class Result: def __init__(self, client, reply): self.reply = reply @@ -135,17 +134,15 @@ def _download(self, url, size, target): pbar.update(len(chunk)) except requests.exceptions.ConnectionError as e: - self.error("Download interupted: %s" % (e,)) + self.error(f"Download interupted: {e}") finally: r.close() if total >= size: break - self.error( - "Download incomplete, downloaded %s byte(s) out of %s" % (total, size) - ) - self.warning("Sleeping %s seconds" % (sleep,)) + self.error(f"Download incomplete, downloaded {total} byte(s) out of {size}") + self.warning(f"Sleeping {sleep} seconds") time.sleep(sleep) mode = "ab" total = os.path.getsize(target) @@ -154,11 +151,11 @@ def _download(self, url, size, target): sleep = self.sleep_max headers = {"Range": "bytes=%d-" % total} tries += 1 - self.warning("Resuming download at byte %s" % (total,)) + self.warning(f"Resuming download at byte {total}") if total != size: raise Exception( - "Download failed: downloaded %s byte(s) out of %s" % (total, size) + f"Download failed: downloaded {total} byte(s) out of {size}" ) elapsed = time.time() - start @@ -183,7 +180,7 @@ def content_type(self): return self.reply["content_type"] def __repr__(self): - return "Result(content_length=%s,content_type=%s,location=%s)" % ( + return "Result(content_length={},content_type={},location={})".format( self.content_length, self.content_type, self.location, @@ -201,7 +198,7 @@ def check(self): def update(self, request_id=None): if request_id is None: request_id = self.reply["request_id"] - task_url = "%s/tasks/%s" % (self._url, request_id) + task_url = f"{self._url}/tasks/{request_id}" self.debug("GET %s", task_url) result = self.robust(self.session.get)( @@ -217,7 +214,7 @@ def delete(self): if "request_id" in self.reply: rid = self.reply["request_id"] - task_url = "%s/tasks/%s" % (self._url, rid) + task_url = f"{self._url}/tasks/{rid}" self.debug("DELETE %s", task_url) delete = self.session.delete( @@ -245,7 +242,7 @@ def __del__(self): print(e) -class Client(object): +class Client: logger = logging.getLogger("cdsapi") def __init__( @@ -361,7 +358,7 @@ def __init__( ) def retrieve(self, name, request, target=None): - result = self._api("%s/resources/%s" % (self.url, name), request, "POST") + result = self._api(f"{self.url}/resources/{name}", request, "POST") if target is not None: result.download(target) return result @@ -380,7 +377,7 @@ def service(self, name, *args, **kwargs): request["_cds_metadata"] = self.metadata request = toJSON(request) result = self._api( - "%s/tasks/services/%s/clientid-%s" % (self.url, name, uuid.uuid4().hex), + f"{self.url}/tasks/services/{name}/clientid-{uuid.uuid4().hex}", request, "PUT", ) @@ -392,7 +389,7 @@ def workflow(self, code, *args, **kwargs): return self.service("tool.toolbox.orchestrator.run_workflow", params) def status(self, context=None): - url = "%s/status.json" % (self.url,) + url = f"{self.url}/status.json" r = self.session.get(url, verify=self.verify, timeout=self.timeout) r.raise_for_status() return r.json() @@ -474,7 +471,7 @@ def _api(self, url, request, method): self.debug("REPLY %s", reply) if reply["state"] != self.last_state: - self.info("Request is %s" % (reply["state"],)) + self.info(f"Request is {reply['state']}") self.last_state = reply["state"] if reply["state"] == "completed": @@ -494,7 +491,7 @@ def _api(self, url, request, method): if sleep > self.sleep_max: sleep = self.sleep_max - task_url = "%s/tasks/%s" % (self.url, rid) + task_url = f"{self.url}/tasks/{rid}" self.debug("GET %s", task_url) result = self.robust(session.get)( @@ -521,7 +518,7 @@ def _api(self, url, request, method): % (reply["error"].get("message"), reply["error"].get("reason")) ) - raise Exception("Unknown API state [%s]" % (reply["state"],)) + raise Exception(f"Unknown API state [{reply['state']}]") def info(self, *args, **kwargs): if self.info_callback: diff --git a/examples/example-era5-update.py b/examples/example-era5-update.py index 5d10c85..6f2a8e6 100755 --- a/examples/example-era5-update.py +++ b/examples/example-era5-update.py @@ -29,7 +29,7 @@ while True: r.update() reply = r.reply - r.info("Request ID: %s, state: %s" % (reply["request_id"], reply["state"])) + r.info(f"Request ID: {reply['request_id']}, state: {reply['state']}") if reply["state"] == "completed": break @@ -46,7 +46,7 @@ break r.error(" %s", n) raise Exception( - "%s. %s." % (reply["error"].get("message"), reply["error"].get("reason")) + f"{reply['error'].get('message')}. {reply['error'].get('reason')}." ) r.download("test.nc") diff --git a/example-era5.py b/examples/example-era5.py similarity index 100% rename from example-era5.py rename to examples/example-era5.py diff --git a/example-glaciers.py b/examples/example-glaciers.py similarity index 100% rename from example-glaciers.py rename to examples/example-glaciers.py