How to use list_and_index method in hypothesis

Best Python code snippet using hypothesis

player.py

Source:player.py Github

copy

Full Screen

1import cards2class Player:3 """A class to represent a player.4 Four lists to manage: deck, hand, discard pile, and cards in play."""5 def __init__(self, name=''):6 # initialize name, score, and card lists7 self.name = name8 self.actions = 09 self.gold = 010 self.buys = 011 self._deck = cards.Cards()12 self._hand = cards.Cards()13 self._discard_pile = cards.Cards()14 self._cards_in_play = cards.Cards()15 self._card_dict = {'hand': self._hand,16 'deck': self._deck, 17 'discard': self._discard_pile,18 'in play': self._cards_in_play,}19 # a player starts with three Estates and seven Coppers in deck20 estates = [cards.Card('Estate', 'Victory', 2) for amount in range(3)]21 coppers = [cards.Card('Copper', 'Treasure', 0) for amount in range(7)]22 self.add_cards('deck', estates)23 self.add_cards('deck', coppers)24 25 # shuffle deck and draw five cards26 self.shuffle()27 self.draw_hand()28 def __repr__(self):29 # returns a string with the name, type, and cost of each card 30 # owned by the player31 ret = []32 def create_string(card_string, card_list):33 nonlocal ret34 ret.append(card_string)35 ret.append(repr(card_list))36 37 create_string('Cards in hand:', self._hand)38 create_string('\nCards in play:', self._cards_in_play)39 create_string('\nCards in discard pile:', self._discard_pile)40 create_string('\nCards in deck:', self._deck)41 return '\n'.join(ret)42 def __str__(self):43 # returns a string with the name of each card owned by the player44 ret = []45 def create_string(card_string, card_list):46 nonlocal ret47 ret.append(card_string)48 ret.append(str(card_list))49 create_string('Cards in hand:', self._hand)50 create_string('\nCards in play:', self._cards_in_play)51 create_string('\nCards in discard pile:', self._discard_pile)52 create_string('\nCards in deck:', self._deck)53 return '\n'.join(ret)54 def __len__(self, card_list):55 # returns the total number of cards owned by the player56 return sum([len(self._deck),57 len(self._hand), 58 len(self._discard_pile),59 len(self._cards_in_play)])60 def __contains__(self, card):61 # returns True if a given card is in the player's hand62 return card in self._hand63 def __iter__(self):64 # yields all cards in the player's hand65 for card in self._hand: yield card66 def __getitem__(self, list_and_index):67 # returns the card in the specified list given the index68 card_list, idx = list_and_index69 try:70 card_list = self._card_dict[card_list]71 except KeyError:72 print('KeyError in __getitem__')73 return74 else:75 return card_list[idx]76 def draw_card(self):77 # draw 1 card from the deck and place it in hand78 if self.is_empty('deck') == False:79 self.add_cards('hand', self.remove_card('deck'))80 else:81 if self.is_empty('discard') == True:82 print('The deck and discard pile are empty. Cannot draw a card.')83 return84 # the discard pile is not empty85 # transfer all cards from discard pile to deck, shuffle, and draw.86 self.transfer_cards('discard', 'deck')87 self.shuffle()88 self.draw_card()89 def draw_hand(self):90 # draw 5 cards from the deck and place in hand91 for amount in range(5):92 if self.is_empty('deck') and self.is_empty('discard'):93 return94 self.draw_card()95 def end_turn(self):96 # place all cards in hand and in play into the discard pile97 # draw hand for next turn98 self.transfer_cards('hand', 'discard')99 self.transfer_cards('in play', 'discard')100 self.draw_hand()101 def shuffle(self):102 # shuffles the player's deck103 self._deck.shuffle()104 def display_hand(self):105 # print the cards in hand106 print('Cards in hand:\n', self._hand, sep='')107 def display_stats(self):108 # prints the number of actions, buys, and gold a player has109 print('Actions: {} Buys: {} Gold: {}'.format(110 self.actions, self.buys, self.gold))111 def transfer_cards(self, cards_from, cards_to):112 # transfers all of the cards from one pile to another113 try:114 cards_from = self._card_dict[cards_from]115 cards_to = self._card_dict[cards_to]116 except KeyError:117 print('KeyError in transfer_cards()')118 return119 else:120 cards_to.add_cards(cards_from.remove_all_cards())121 def is_empty(self, card_list):122 # return True if the given card list is empty123 try:124 card_list = self._card_dict[card_list]125 except KeyError:126 print('KeyError in is_empty()')127 return128 else:129 return card_list.is_empty()130 def add_cards(self, card_list, cards_to_add):131 # add cards to the one of the lists132 try:133 card_list = self._card_dict[card_list]134 except KeyError:135 print('KeyError in add_cards')136 return137 else:138 card_list.add_cards(cards_to_add)139 def remove_card(self, card_list, idx=-1):140 # remove a single card from one of the lists and return it141 try:142 card_list = self._card_dict[card_list]143 except KeyError:144 print('KeyError in remove_card')145 return146 else:147 return card_list.remove_card(idx)148 def remove_all_cards(self, card_list):149 # removes all cards from one of the lists and returns them150 try:151 card_list = self._card_dict[card_list]152 except KeyError:153 print('KeyError in remove_all_cards()')154 return155 else:156 return card_list.remove_all_cards()157 def length(self, card_list):158 # returns the total number of cards in the given list159 try:160 card_list = self._card_dict[card_list]161 except KeyError:162 print('KeyError in length()')163 return164 else:165 return len(card_list)166 def calc_score(self):167 # calculates the final score for the player once the game ends168 self.transfer_cards('hand', 'deck')169 self.transfer_cards('discard', 'deck')170 self.transfer_cards('in play', 'deck')171 def card_count(card): return self._deck.card_count(card)172 # estates are worth 1 point, duchies are worth 3, and provinces are worth 6173 score = 0174 score += 1 * card_count(cards.Card('Estate', 'Victory', 2))175 score += 3 * card_count(cards.Card('Duchy', 'Victory', 5))176 score += 6 * card_count(cards.Card('Province', 'Victory', 8))177 178 # gardens are worth 1 point for every 10 cards in the deck179 gardens_count = card_count(cards.Card('Gardens', 'Victory', 4))180 score += (len(self._deck) // 10) * gardens_count...

Full Screen

Full Screen

detect_repetition.py

Source:detect_repetition.py Github

copy

Full Screen

...37@given(st.integers().filter(lambda x: x % 2 == 0))38def test_even_integers(i):39 event("i mod 3 = %d" % (i % 3,))40@st.composite41def list_and_index(draw, elements=st.integers()):42 xs = draw(st.lists(elements, min_size=1))43 i = draw(st.integers(min_value=0, max_value=len(xs) - 1))44 second(xs,i)45 return (xs, i)46def second(xs,i):47 print('xs')48 print(xs)49 print('i')50 print(i)51import shutil52import tempfile53from collections import defaultdict54import hypothesis.strategies as st55from hypothesis.database import DirectoryBasedExampleDatabase...

Full Screen

Full Screen

test_bisect.py

Source:test_bisect.py Github

copy

Full Screen

2from hypothesis import given3from hypothesis.strategies import composite, integers, lists4from comptSort import binary_search5@composite6def list_and_index(draw, elements=integers()):7 xs = draw(lists(elements, min_size=1))8 i = draw(integers(min_value=0, max_value=len(xs) - 1))9 return (xs, i)10@given(list_and_index())11def test_bisect(list_index):12 """Compare the results of custom binary search function against bisect"""...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run hypothesis automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful