From ad367a6e59e2de81f540b1e4bae36e020a841faf Mon Sep 17 00:00:00 2001 From: Rotzbua Date: Sat, 18 Jul 2026 20:23:05 +0200 Subject: [PATCH] chore: use f-string --- extruct/_extruct.py | 4 ++-- extruct/tool.py | 2 +- tests/test_jsonld.py | 10 ++++------ tests/test_microdata.py | 28 ++++++++++++---------------- tests/test_rdfa.py | 6 +++--- tests/test_uniform.py | 12 ++++-------- 6 files changed, 26 insertions(+), 36 deletions(-) diff --git a/extruct/_extruct.py b/extruct/_extruct.py index 51da7f49..16b4966f 100644 --- a/extruct/_extruct.py +++ b/extruct/_extruct.py @@ -74,7 +74,7 @@ def extract( if errors == "ignore": return {} if errors == "log": - logger.exception("Failed to parse html, raises {}".format(e)) + logger.exception(f"Failed to parse html, raises {e}") return {} if errors == "strict": raise @@ -131,7 +131,7 @@ def extract( output[syntax] = list(extract(document, base_url=base_url)) except Exception as e: if errors == "log": - logger.exception("Failed to extract {}, raises {}".format(syntax, e)) + logger.exception(f"Failed to extract {syntax}, raises {e}") if errors == "ignore": pass if errors == "strict": diff --git a/extruct/tool.py b/extruct/tool.py index feaf213b..18987b68 100644 --- a/extruct/tool.py +++ b/extruct/tool.py @@ -20,7 +20,7 @@ def metadata_from_url( resp = requests.get(url, timeout=30) result: dict[str, Any] = { "url": url, - "status": "{} {}".format(resp.status_code, resp.reason), + "status": f"{resp.status_code} {resp.reason}", } try: resp.raise_for_status() diff --git a/tests/test_jsonld.py b/tests/test_jsonld.py index 178b3229..53c69840 100644 --- a/tests/test_jsonld.py +++ b/tests/test_jsonld.py @@ -44,8 +44,8 @@ def assertJsonLdCorrect(self, folder, page): self._check_jsonld(body, expected) def _get_body_expected(self, folder, page): - body = get_testdata(folder, "{}.html".format(page)) - expected = get_testdata(folder, "{}.jsonld".format(page)) + body = get_testdata(folder, f"{page}.html") + expected = get_testdata(folder, f"{page}.jsonld") return body, json.loads(expected.decode("utf8")) def _check_jsonld(self, body, expected): @@ -55,10 +55,8 @@ def _check_jsonld(self, body, expected): def test_null(self): page = "null_ld_mock" - body = get_testdata("misc", "{}.html".format(page)) - expected = json.loads( - get_testdata("misc", "{}.jsonld".format(page)).decode("UTF-8") - ) + body = get_testdata("misc", f"{page}.html") + expected = json.loads(get_testdata("misc", f"{page}.jsonld").decode("UTF-8")) jsonlde = JsonLdExtractor() data = jsonlde.extract(body) diff --git a/tests/test_microdata.py b/tests/test_microdata.py index eda2d26c..3310e255 100644 --- a/tests/test_microdata.py +++ b/tests/test_microdata.py @@ -23,11 +23,9 @@ def _test_schemaorg(self, schema, indexes=None): def test_schemaorg_CreativeWork(self): for i in [1]: - body = get_testdata("schema.org", "CreativeWork.{:03d}.html".format(i)) + body = get_testdata("schema.org", f"CreativeWork.{i:03d}.html") expected = json.loads( - get_testdata("schema.org", "CreativeWork.{:03d}.json".format(i)).decode( - "UTF-8" - ) + get_testdata("schema.org", f"CreativeWork.{i:03d}.json").decode("UTF-8") ) mde = MicrodataExtractor() @@ -36,11 +34,11 @@ def test_schemaorg_CreativeWork(self): def test_schemaorg_LocalBusiness(self): for i in [2, 3]: - body = get_testdata("schema.org", "LocalBusiness.{:03d}.html".format(i)) + body = get_testdata("schema.org", f"LocalBusiness.{i:03d}.html") expected = json.loads( - get_testdata( - "schema.org", "LocalBusiness.{:03d}.json".format(i) - ).decode("UTF-8") + get_testdata("schema.org", f"LocalBusiness.{i:03d}.json").decode( + "UTF-8" + ) ) mde = MicrodataExtractor() @@ -49,11 +47,11 @@ def test_schemaorg_LocalBusiness(self): def test_schemaorg_MusicRecording(self): for i in [1]: - body = get_testdata("schema.org", "MusicRecording.{:03d}.html".format(i)) + body = get_testdata("schema.org", f"MusicRecording.{i:03d}.html") expected = json.loads( - get_testdata( - "schema.org", "MusicRecording.{:03d}.json".format(i) - ).decode("UTF-8") + get_testdata("schema.org", f"MusicRecording.{i:03d}.json").decode( + "UTF-8" + ) ) mde = MicrodataExtractor() @@ -62,11 +60,9 @@ def test_schemaorg_MusicRecording(self): def test_schemaorg_Event(self): for i in [1, 2, 3, 4, 8]: - body = get_testdata("schema.org", "Event.{:03d}.html".format(i)) + body = get_testdata("schema.org", f"Event.{i:03d}.html") expected = json.loads( - get_testdata("schema.org", "Event.{:03d}.json".format(i)).decode( - "UTF-8" - ) + get_testdata("schema.org", f"Event.{i:03d}.json").decode("UTF-8") ) mde = MicrodataExtractor() diff --git a/tests/test_rdfa.py b/tests/test_rdfa.py index b3d81a7e..44757807 100644 --- a/tests/test_rdfa.py +++ b/tests/test_rdfa.py @@ -55,7 +55,7 @@ def prettify(self, a, normalize_bnode_ids=True): def test_w3c_rdfalite(self): for i in [3, 4, 5]: - fileprefix = "w3c.rdfalite.example{:03d}".format(i) + fileprefix = f"w3c.rdfalite.example{i:03d}" body = get_testdata("w3crdfa", fileprefix + ".html") expected = json.loads( get_testdata("w3crdfa", fileprefix + ".expanded.json").decode("UTF-8") @@ -67,7 +67,7 @@ def test_w3c_rdfalite(self): def test_w3c_rdf11primer(self): for i in [14]: - fileprefix = "w3c.rdf11primer.example{:03d}".format(i) + fileprefix = f"w3c.rdf11primer.example{i:03d}" body = get_testdata("w3crdfa", fileprefix + ".html") expected = json.loads( get_testdata("w3crdfa", fileprefix + ".expanded.json").decode("UTF-8") @@ -79,7 +79,7 @@ def test_w3c_rdf11primer(self): def test_w3c_rdfaprimer(self): for i in [5, 6, 7, 8, 9, 10, 11, 15]: - fileprefix = "w3c.rdfaprimer.example{:03d}".format(i) + fileprefix = f"w3c.rdfaprimer.example{i:03d}" print(fileprefix) body = get_testdata("w3crdfa", fileprefix + ".html") expected = json.loads( diff --git a/tests/test_uniform.py b/tests/test_uniform.py index 81d92849..d5819ea8 100644 --- a/tests/test_uniform.py +++ b/tests/test_uniform.py @@ -63,16 +63,14 @@ def test_uopengraph_duplicated_priorities(self): [ { "properties": [ - ("prop_{}".format(k), "value_{}".format(v)) - for k in range(5) - for v in range(5) + (f"prop_{k}", f"value_{v}") for k in range(5) for v in range(5) ], "namespace": "namespace", } ] ) for k in range(5): - assert data[0]["prop_{}".format(k)] == "value_0" + assert data[0][f"prop_{k}"] == "value_0" # Ensures that empty is not returned if a property contains any # non empty value @@ -104,9 +102,7 @@ def test_uopengraph_duplicated_with_og_array(self): [ { "properties": [ - ("prop_{}".format(k), "value_{}".format(v)) - for k in range(5) - for v in range(5) + (f"prop_{k}", f"value_{v}") for k in range(5) for v in range(5) ], "namespace": "namespace", } @@ -114,7 +110,7 @@ def test_uopengraph_duplicated_with_og_array(self): with_og_array=True, ) for k in range(5): - assert data[0]["prop_{}".format(k)] == [ + assert data[0][f"prop_{k}"] == [ "value_0", "value_1", "value_2",