Skip to content

Commit

Permalink
Cleanup write function
Browse files Browse the repository at this point in the history
  • Loading branch information
APN-Pucky committed Oct 13, 2024
1 parent fde178f commit 60281ae
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/pylhe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,12 @@ def write_lhe_string(lheinit, lheevents, rwgt=True, weights=False):
)


def _open_write_file(filepath: str, gz: bool = False):
if filepath.endswith((".gz", ".gzip")) or gz:
return gzip.open(filepath, "wt")
return open(filepath, "w")


def write_lhe_file_path(
lhefile: LHEFile,
filepath: str,
Expand All @@ -625,12 +631,8 @@ def write_lhe_file_path(
Write the LHE file.
"""
# if filepath suffix is gz, write as gz
if filepath.endswith((".gz", ".gzip")) or gz:
with gzip.open(filepath, "wt") as f:
lhefile.write(f, rwgt=rwgt, weights=weights)
else:
with open(filepath, "w") as f:
lhefile.write(f, rwgt=rwgt, weights=weights)
with _open_write_file(filepath, gz=gz) as f:
lhefile.write(f, rwgt=rwgt, weights=weights)


def write_lhe_file(lheinit, lheevents, filepath, gz=False, rwgt=True, weights=False):
Expand Down

0 comments on commit 60281ae

Please sign in to comment.