X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=grab_bag.py;h=78fee37a2c5ceb7bf677cfb661609b15fc9d9413;hb=addd4980077f6e3857c5c035b49784dc3ceca49a;hp=a427256ca0f1b1530a0e757391dc027b1391099a;hpb=5e241dc47e497c547463cecc07946ea6882835a7;p=kiosk.git diff --git a/grab_bag.py b/grab_bag.py index a427256..78fee37 100644 --- a/grab_bag.py +++ b/grab_bag.py @@ -1,31 +1,37 @@ +#!/usr/bin/env python3 + +import logging import random +from typing import Iterable, List, Optional, Set + + +logger = logging.getLogger(__file__) class grab_bag(object): - def __init__(self): - self.contents = set() + def __init__(self) -> None: + self.contents: Set[str] = set() - def clear(self): + def clear(self) -> None: self.contents.clear() - def add(self, item): + def add(self, item: str) -> None: if item not in self.contents: self.contents.add(item) - def add_all(self, collection): + def add_all(self, collection: Iterable[str]) -> None: for x in collection: self.add(x) - def subset(self, count): + 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(self.contents, count) - def size(self): + 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))