from collections.abc import Iterable from itertools import cycle from more_itertools import random_permutation from ._cardcollection import CardCollection class Deck(CardCollection): def draw(self): return self.pop() def shuffle(self): shuffled = list(random_permutation(self)) self.clear() self.extend(shuffled) return self def deal(self, card_collections, cards_per_rotation=1, max_rotations=None): if not issubclass(card_collections, Iterable) or not all([issubclass(CardCollection, collection) for collection in card_collections]): raise TypeError("You can only deal cards to an iterable containing CardCollections") if max_rotations is None: rotation = cycle(card_collections) elif isinstance(max_rotations, int): rotation = card_collections * max_rotations else: raise TypeError("Max rotations should be a positive integer") for card_collection in rotation: try: for _ in range(cards_per_rotation): card_collection.append(self.draw()) except IndexError: break