Skip to content

Commit

Permalink
Add itertools.PickleCache. (#2756)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasper authored Mar 24, 2023
1 parent 3a4755f commit a973a04
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/gluonts/itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@

import itertools
import math
import pickle
import random
import tempfile
from dataclasses import dataclass, field
from pathlib import Path
from typing import (
Callable,
Dict,
Expand Down Expand Up @@ -154,6 +157,41 @@ def __len__(self) -> int:
return len(self.iterable)


@dataclass
class PickleCached:
"""A caching wrapper for ``iterable`` using ``pickle`` to store cached
values on disk.
See :class:`Cached` for more information.
"""

iterable: SizedIterable
cached: bool = False
_path: Path = field(
default_factory=lambda: Path(
tempfile.NamedTemporaryFile(delete=False).name
)
)

def __iter__(self):
if not self.cached:
with open(self._path, "wb") as tmpfile:
for batch in batcher(self.iterable, 16):
pickle.dump(batch, tmpfile)
yield from batch
self.cached = True
else:
with open(self._path, "rb") as tmpfile:
while True:
try:
yield from pickle.load(tmpfile)
except EOFError:
return

def __del__(self):
self._path.unlink()


@dataclass
class PseudoShuffled:
"""
Expand Down
2 changes: 2 additions & 0 deletions test/test_itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from gluonts.itertools import (
batcher,
Cached,
PickleCached,
Cyclic,
IterableSlice,
PseudoShuffled,
Expand Down Expand Up @@ -68,6 +69,7 @@ def test_pseudo_shuffled(data: Iterable) -> None:
"data, expected_elements_per_iteration",
[
(Cached(range(4)), (list(range(4)),) * 5),
(PickleCached(range(4)), (list(range(4)),) * 5),
(batcher(range(10), 3), ([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]], [])),
(IterableSlice(range(10), 3), ([0, 1, 2],) * 5),
(
Expand Down

0 comments on commit a973a04

Please sign in to comment.