Skip to content

Commit

Permalink
fix: python<=3.9 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
APN-Pucky committed Feb 20, 2024
1 parent ec337d5 commit 8ee70b7
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/pylhe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,25 @@ def mothers(self):
]


def _indent(elem, level=0):
"""
XML indentation helper from https://stackoverflow.com/a/33956544.
"""
i = "\n" + level * " "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
_indent(elem, level + 1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i


class LHEInit(dict):
"""Store the <init> block as dict."""

Expand All @@ -252,7 +271,7 @@ def tolhe(self):
weightgroup_elem, "weight", **value["attrib"]
)
weight_elem.text = value["name"]
ET.indent(root, " ")
_indent(root)
sweightgroups = ET.tostring(root, encoding="unicode", method="xml")

return (
Expand All @@ -262,7 +281,6 @@ def tolhe(self):
+ "\n".join([p.tolhe() for p in self["procInfo"]])
+ "\n"
+ f"{sweightgroups}"
+ "\n"
# + f" <LesHouchesEvents version='{self['LHEVersion']}'>\n"
+ "</init>"
)
Expand Down

0 comments on commit 8ee70b7

Please sign in to comment.