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