# HG changeset patch # User Matti Hamalainen # Date 1621932054 -10800 # Node ID d77ab8a300b116e7241565ebf22f01d8a9678ccb # Parent 420f13925f20de1f49564cd021902377e9890b8b Use Python 3.6/3.7 format strings where we can. diff -r 420f13925f20 -r d77ab8a300b1 lxmldump.py --- a/lxmldump.py Tue May 25 11:39:36 2021 +0300 +++ b/lxmldump.py Tue May 25 11:40:54 2021 +0300 @@ -108,9 +108,10 @@ ## Format "Ptr" node as text def pkk_ptr_to_text(pnode): - return "{}".format( - pnode.attrib["{http://www.w3.org/TR/xlink}href"], - ("".join(pnode.itertext())).strip()) + phref = pnode.attrib["{http://www.w3.org/TR/xlink}href"] + ptext = ("".join(pnode.itertext())).strip() + + return f"{ptext}" ## Get text inside a given node @@ -133,21 +134,21 @@ def pkk_dump_recursive(indent, lnode): if lnode.tag in ["Example"]: stmp = pkk_node_to_text(lnode) - pkk_printi(indent, "{} \"{}\"\n".format(lnode.tag, stmp)) + pkk_printi(indent, f"{lnode.tag} \"{stmp}\"\n") else: if isinstance(lnode.text, str): - stmp = pkk_str_clean(lnode.text).strip() - if stmp != "": - stmp = " \""+ stmp +"\"" + textstr = pkk_str_clean(lnode.text).strip() + if textstr != "": + textstr = " \""+ textstr +"\"" else: - stmp = "" + textstr = "" if len(lnode.attrib) > 0: - atmp = " "+ str(lnode.attrib) + attrstr = " "+ str(lnode.attrib) else: - atmp = "" + attrstr = "" - pkk_printi(indent, "{}{}{}\n".format(lnode.tag, atmp, stmp)) + pkk_printi(indent, f"{lnode.tag}{attrstr}{textstr}\n") for qnode in lnode.findall("./*"): pkk_dump_recursive(indent + 1, qnode) @@ -155,11 +156,11 @@ ## Output item(s) under given node with given format string def pkk_output_subs_fmt(indent, dnode, dsub, dname, dfmt): for qnode in dnode.findall(dsub): - pkk_printi(indent, dfmt.format(dname, pkk_node_to_text(qnode))) + pkk_printi(indent, dfmt.format(nname=dname, ntext=pkk_node_to_text(qnode))) ## Output item(s) under given node with a prefixed name string def pkk_output_subs_prefix(indent, dnode, dsub, dname): - pkk_output_subs_fmt(indent, dnode, dsub, dname, "{0} \"{1}\"\n") + pkk_output_subs_fmt(indent, dnode, dsub, dname, "{nname} \"{ntext}\"\n") ## Output a main "Headword" or "Sense" node under it @@ -200,11 +201,12 @@ tmpl.sort(reverse=False, key=lambda attr: (attr, len(attr))) # Print the headword and attributes if any - pkk_output_subs_fmt(indent, wnode, "./Headword", "", "\"{1}\"") + pkk_output_subs_fmt(indent, wnode, "./Headword", "", "\"{ntext}\"") + if len(tmpl) > 0: - pkk_print(" ({})\n".format(" ; ".join(tmpl))) - else: - pkk_print("\n") + pkk_print(" ({nlist})".format(nlist=" ; ".join(tmpl))) + + pkk_print("\n") # Print main "sense" pkk_output_sense(indent + 1, wnode) @@ -212,7 +214,7 @@ # Print any other "senses" index = 1 for wnode in dnode.findall("./SenseGrp"): - pkk_printi(indent + 1, "sense #{}\n".format(index)) + pkk_printi(indent + 1, f"sense #{index}\n") pkk_output_sense(indent + 2, wnode) index += 1