Skip to content

Commit 47bf08d

Browse files
committed
updates to generator and also add test cases
1 parent 0ac7ee8 commit 47bf08d

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

labelbox/data/generator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def fill_queue(self):
7070
self.queue.put(value)
7171
except Exception as e:
7272
self.queue.put(
73-
ValueError("Unexpected exception while filling queue. %r", e))
73+
ValueError(f"Unexpected exception while filling queue: {e}"))
7474
finally:
7575
self.queue.put(None)
7676

@@ -92,8 +92,8 @@ def __next__(self) -> Any:
9292
thread.join()
9393
raise StopIteration
9494
value = self.queue.get()
95-
if isinstance(value, Exception):
96-
raise value
95+
if isinstance(value, Exception):
96+
raise value
9797
else:
9898
value = self._process(next(self._data))
9999
return value

tests/data/test_prefetch_generator.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import pytest
2+
from labelbox.data.generator import PrefetchGenerator
3+
from random import random
4+
5+
6+
class ChildClassGenerator(PrefetchGenerator):
7+
8+
def __init__(self, examples, num_executors=1):
9+
super().__init__(data=examples, num_executors=num_executors)
10+
11+
def _process(self, value):
12+
num = random()
13+
if num < .2:
14+
raise ValueError("Randomized value error")
15+
return value
16+
17+
18+
amount = (i for i in range(50))
19+
20+
21+
def test_single_thread_generator():
22+
generator = ChildClassGenerator(amount, num_executors=1)
23+
24+
with pytest.raises(ValueError):
25+
for _ in range(51):
26+
next(generator)
27+
28+
29+
def test_multi_thread_generator():
30+
generator = ChildClassGenerator(amount, num_executors=4)
31+
32+
with pytest.raises(ValueError):
33+
for _ in range(51):
34+
next(generator)

0 commit comments

Comments
 (0)