Fix wakeword.
[kiosk.git] / grab_bag.py
1 #!/usr/bin/env python3
2
3 import logging
4 import random
5 from typing import Iterable, List, Optional, Set
6
7
8 logger = logging.getLogger(__name__)
9
10
11 class grab_bag(object):
12     def __init__(self) -> None:
13         self.contents: Set[str] = set()
14
15     def clear(self) -> None:
16         self.contents.clear()
17
18     def add(self, item: str) -> None:
19         if item not in self.contents:
20             self.contents.add(item)
21
22     def add_all(self, collection: Iterable[str]) -> None:
23         for x in collection:
24             self.add(x)
25
26     def subset(self, count: int) -> Optional[List[str]]:
27         if len(self.contents) < count:
28             return None
29         return random.sample(self.contents, count)
30
31     def size(self) -> int:
32         return len(self.contents)
33
34
35 # x = grab_bag()
36 # x.add_all(["oneA", "two", "three", "oneB", "four", "five", "oneC", "oneD"])
37 # print(x.subset(3))