Best Python code snippet using hypothesis
collections.py
Source:collections.py
...47 def do_draw(self, data):48 return self.newtuple(49 data.draw(e) for e in self.element_strategies50 )51 def calc_is_empty(self, recur):52 return any(recur(e) for e in self.element_strategies)53TERMINATOR = hbytes(b'\0')54class ListStrategy(SearchStrategy):55 """A strategy for lists which takes an intended average length and a56 strategy for each of its element types and generates lists containing any57 of those element types.58 The conditional distribution of the length is geometric, and the59 conditional distribution of each parameter is whatever their60 strategies define.61 """62 def __init__(63 self,64 strategies, average_length=50.0, min_size=0, max_size=float('inf')65 ):66 SearchStrategy.__init__(self)67 assert average_length > 068 self.average_length = average_length69 strategies = tuple(strategies)70 self.min_size = min_size or 071 self.max_size = max_size or float('inf')72 self.element_strategy = one_of_strategies(strategies)73 def do_validate(self):74 self.element_strategy.validate()75 if self.is_empty:76 raise InvalidArgument((77 'Cannot create non-empty lists with elements drawn from '78 'strategy %r because it has no values.') % (79 self.element_strategy,))80 def calc_is_empty(self, recur):81 if self.min_size == 0:82 return False83 else:84 return recur(self.element_strategy)85 def do_draw(self, data):86 if self.element_strategy.is_empty:87 assert self.min_size == 088 return []89 elements = cu.many(90 data,91 min_size=self.min_size, max_size=self.max_size,92 average_size=self.average_length93 )94 result = []95 while elements.more():96 result.append(data.draw(self.element_strategy))97 return result98 def __repr__(self):99 return (100 'ListStrategy(%r, min_size=%r, average_size=%r, max_size=%r)'101 ) % (102 self.element_strategy, self.min_size, self.average_length,103 self.max_size104 )105class UniqueListStrategy(SearchStrategy):106 def __init__(107 self,108 elements, min_size, max_size, average_size,109 key110 ):111 super(UniqueListStrategy, self).__init__()112 assert min_size <= average_size <= max_size113 self.min_size = min_size114 self.max_size = max_size115 self.average_size = average_size116 self.element_strategy = elements117 self.key = key118 def do_validate(self):119 self.element_strategy.validate()120 if self.is_empty:121 raise InvalidArgument((122 'Cannot create non-empty lists with elements drawn from '123 'strategy %r because it has no values.') % (124 self.element_strategy,))125 def calc_is_empty(self, recur):126 if self.min_size == 0:127 return False128 else:129 return recur(self.element_strategy)130 def do_draw(self, data):131 if self.element_strategy.is_empty:132 assert self.min_size == 0133 return []134 elements = cu.many(135 data,136 min_size=self.min_size, max_size=self.max_size,137 average_size=self.average_size138 )139 seen = set()140 result = []141 while elements.more():142 value = data.draw(self.element_strategy)143 k = self.key(value)144 if k in seen:145 elements.reject()146 else:147 seen.add(k)148 result.append(value)149 assert self.max_size >= len(result) >= self.min_size150 return result151class FixedKeysDictStrategy(MappedSearchStrategy):152 """A strategy which produces dicts with a fixed set of keys, given a153 strategy for each of their equivalent values.154 e.g. {'foo' : some_int_strategy} would155 generate dicts with the single key 'foo' mapping to some integer.156 """157 def __init__(self, strategy_dict):158 self.dict_type = type(strategy_dict)159 if isinstance(strategy_dict, OrderedDict):160 self.keys = tuple(strategy_dict.keys())161 else:162 try:163 self.keys = tuple(sorted(164 strategy_dict.keys(),165 ))166 except TypeError:167 self.keys = tuple(sorted(168 strategy_dict.keys(), key=repr,169 ))170 super(FixedKeysDictStrategy, self).__init__(171 strategy=TupleStrategy(172 (strategy_dict[k] for k in self.keys), tuple173 )174 )175 def calc_is_empty(self, recur):176 return recur(self.mapped_strategy)177 def __repr__(self):178 return 'FixedKeysDictStrategy(%r, %r)' % (179 self.keys, self.mapped_strategy)180 def pack(self, value):...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!