X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=grab_bag.py;h=10272da7acb37a64f4a281377af677a5f7b1807e;hb=HEAD;hp=1620da209c298820fe164f56bc02f4a2520dca5b;hpb=c06bfef53f70551e7920bc4facce27f47b89e2ba;p=kiosk.git diff --git a/grab_bag.py b/grab_bag.py index 1620da2..10272da 100644 --- a/grab_bag.py +++ b/grab_bag.py @@ -1,12 +1,16 @@ #!/usr/bin/env python3 +import logging import random -from typing import Iterable, List +from typing import Iterable, List, Optional, Set + + +logger = logging.getLogger(__name__) class grab_bag(object): def __init__(self) -> None: - self.contents = set() + self.contents: Set[str] = set() def clear(self) -> None: self.contents.clear() @@ -19,16 +23,15 @@ class grab_bag(object): for x in collection: self.add(x) - def subset(self, count: int) -> List[str]: + def subset(self, count: int) -> Optional[List[str]]: if len(self.contents) < count: return None - subset = random.sample(self.contents, count) - return subset + return random.sample(list(self.contents), count) def size(self) -> int: return len(self.contents) # x = grab_bag() -# x.add_all([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) -# print x.subset(3) +# x.add_all(["oneA", "two", "three", "oneB", "four", "five", "oneC", "oneD"]) +# print(x.subset(3))