Skip to content

Commit 9f4b0b5

Browse files
author
Nick Manganelli
committed
permit slice in limit_files
1 parent dc4e822 commit 9f4b0b5

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/coffea/dataset_tools/filespec.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -216,20 +216,20 @@ def limit_steps(self, max_steps: int | slice | None, per_file: bool = False) ->
216216
new_dict[k]["steps"] = steps_by_file[k]
217217
return type(self)(new_dict)
218218

219-
def limit_files(self, max_files: int | None) -> Self:
219+
def limit_files(self, max_files: int | slice | None) -> Self:
220220
"""Limit the number of files."""
221-
if max_files is None:
221+
if max_files is None or (isinstance(max_files, int) and max_files >= len(self)):
222222
return self
223-
if not isinstance(max_files, int) or max_files < 0:
224-
raise ValueError("max_files must be a non-negative integer")
225-
if max_files >= len(self):
226-
return self
227-
new_dict = {}
228-
for i, (k, v) in enumerate(self.items()):
229-
if i < max_files:
230-
new_dict[k] = v
231-
else:
232-
break
223+
if isinstance(max_files, slice):
224+
valid_keys = list(self.keys())[max_files]
225+
new_dict = {k: v for k, v in self.items() if k in valid_keys}
226+
else:
227+
new_dict = {}
228+
for i, (k, v) in enumerate(self.items()):
229+
if i < max_files:
230+
new_dict[k] = v
231+
else:
232+
break
233233
return type(self)(new_dict)
234234

235235
def filter_files(
@@ -466,7 +466,7 @@ def limit_steps(self, max_steps: int | slice, per_file: bool = False) -> Self:
466466
spec["files"] = self.files.limit_steps(max_steps, per_file=per_file)
467467
return type(self)(**spec)
468468

469-
def limit_files(self, max_files: int | None) -> Self:
469+
def limit_files(self, max_files: int | slice | None) -> Self:
470470
"""Limit the number of files."""
471471
spec = self.model_dump()
472472
spec["files"] = self.files.limit_files(max_files)
@@ -533,7 +533,7 @@ def limit_steps(
533533
spec[k] = v.limit_steps(max_steps, per_file=per_file)
534534
return type(self)(spec)
535535

536-
def limit_files(self, max_files: int | None, per_dataset: bool = True) -> Self:
536+
def limit_files(self, max_files: int | slice | None, per_dataset: bool = True) -> Self:
537537
"""Limit the number of files."""
538538
spec = copy.deepcopy(self)
539539
if per_dataset:

0 commit comments

Comments
 (0)