Repository: scikit-hep/pylhe

Issue:
Can't use readNumEvents with zipped LHE-files
https://github.com/scikit-hep/pylhe/blob/9b06dbde1937735e910697acefdf2ddb2cfeedaa/src/pylhe/__init__.py#L255

Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/scikit-hep/pylhe/pull/80?src=pr&el=h1) Report
> Merging [#80](https://codecov.io/gh/scikit-hep/pylhe/pull/80?src=pr&el=desc) (5c76b7b) into [master](https://codecov.io/gh/scikit-hep/pylhe/commit/9b06dbde1937735e910697acefdf2ddb2cfeedaa?el=desc) (9b06dbd) will **increase** coverage by `0.21%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/scikit-hep/pylhe/pull/80/graphs/tree.svg?width=650&height=150&src=pr&token=I5YnvSyPCo)](https://codecov.io/gh/scikit-hep/pylhe/pull/80?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master      #80      +/-   ##
==========================================
+ Coverage   65.40%   65.62%   +0.21%     
==========================================
  Files           1        1              
  Lines         159      160       +1     
  Branches       30       30              
==========================================
+ Hits          104      105       +1     
  Misses         48       48              
  Partials        7        7              
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `65.62% <100.00%> (+0.21%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/scikit-hep/pylhe/pull/80?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/pylhe/\_\_init\_\_.py](https://codecov.io/gh/scikit-hep/pylhe/pull/80/diff?src=pr&el=tree#diff-c3JjL3B5bGhlL19faW5pdF9fLnB5) | `65.62% <100.00%> (+0.21%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/scikit-hep/pylhe/pull/80?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/scikit-hep/pylhe/pull/80?src=pr&el=footer). Last update [9b06dbd...5c76b7b](https://codecov.io/gh/scikit-hep/pylhe/pull/80?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/src/pylhe/__init__.py b/src/pylhe/__init__.py
index bacd65d..4049113 100644
--- a/src/pylhe/__init__.py
+++ b/src/pylhe/__init__.py
@@ -247,13 +247,15 @@ def readLHEWithAttributes(filepath):
         return
 
 
-def readNumEvents(file):
+def readNumEvents(filepath):
     """
     Moderately efficent way to get the number of events stored in file.
     """
-    return sum(
-        element.tag == "event" for event, element in ET.iterparse(file, events=["end"])
-    )
+    with _extract_fileobj(filepath) as fileobj:
+        return sum(
+            element.tag == "event"
+            for event, element in ET.iterparse(fileobj, events=["end"])
+        )
 
 
 def visualize(event, outputname):


Test patch:
diff --git a/tests/test_lhe_reader.py b/tests/test_lhe_reader.py
index aff4674..fd24bcc 100644
--- a/tests/test_lhe_reader.py
+++ b/tests/test_lhe_reader.py
@@ -42,15 +42,15 @@ def test_gzip_open(tmpdir, testdata_gzip_file):
     assert isinstance(pylhe._extract_fileobj(testdata_gzip_file), gzip.GzipFile)
     assert isinstance(pylhe._extract_fileobj(Path(testdata_gzip_file)), gzip.GzipFile)
 
-    # Verify uncompressed and compressed both work
-    assert pylhe.readLHEInit(TEST_FILE) == pylhe.readLHEInit(testdata_gzip_file)
-
 
-def test_event_count():
+def test_event_count(testdata_gzip_file):
     assert pylhe.readNumEvents(TEST_FILE) == 791
+    assert pylhe.readNumEvents(TEST_FILE) == pylhe.readNumEvents(testdata_gzip_file)
 
 
-def test_lhe_init():
+def test_lhe_init(testdata_gzip_file):
+    assert pylhe.readLHEInit(TEST_FILE) == pylhe.readLHEInit(testdata_gzip_file)
+
     init_data = pylhe.readLHEInit(TEST_FILE)
     init_info = init_data["initInfo"]
     assert init_info["beamA"] == pytest.approx(1.0)