How to use array_for method in hypothesis

Best Python code snippet using hypothesis

persistent_vector.py

Source:persistent_vector.py Github

copy

Full Screen

...29 def tailoff(self):30 if self._cnt < 32:31 return 032 return ((self._cnt - 1) >> 5) << 533 def array_for(self, i):34 if 0 <= i < self._cnt:35 if i >= self.tailoff():36 return self._tail37 node = self._root38 level = self._shift39 while level > 0:40 assert isinstance(node, Node)41 node = node._array[(i >> level) & 0x01f]42 level -= 543 assert isinstance(node, Node)44 return node._array45 affirm(False, u"Index out of Range")46 def nth(self, i, not_found=None):47 if 0 <= i < self._cnt:48 node = self.array_for(r_uint(i))49 return node[i & 0x01f]50 if not_found is None:51 affirm(False, u"Index out of Range")52 else:53 return not_found54 def conj(self, val):55 assert self._cnt < r_uint(0xFFFFFFFF)56 if self._cnt - self.tailoff() < 32:57 new_tail = self._tail[:]58 new_tail.append(val)59 return PersistentVector(self._meta, self._cnt + 1, self._shift, self._root, new_tail)60 root = self._root61 assert isinstance(root, Node)62 tail_node = Node(root._edit, self._tail)63 new_shift = self._shift64 if (self._cnt >> 5) > (r_uint(1) << self._shift):65 root = self._root66 assert isinstance(root, Node)67 new_root = Node(root._edit)68 new_root._array[0] = self._root69 root = self._root70 assert isinstance(root, Node)71 new_root._array[1] = new_path(root._edit, self._shift, tail_node)72 new_shift += 573 else:74 new_root = self.push_tail(self._shift, self._root, tail_node)75 return PersistentVector(self._meta, self._cnt + 1, new_shift, new_root, [val])76 def push_tail(self, level, parent, tail_node):77 subidx = ((self._cnt - 1) >> level) & 0x01f78 assert isinstance(parent, Node)79 ret = Node(parent._edit, parent._array[:])80 root = self._root81 assert isinstance(root, Node)82 if (level == 5):83 node_to_insert = tail_node84 else:85 child = parent._array[subidx]86 if child is not None:87 node_to_insert = self.push_tail(level - 5, child, tail_node)88 else:89 node_to_insert = new_path(root._edit, level - 5, tail_node)90 ret._array[subidx] = node_to_insert91 return ret92 def pop(self):93 affirm(self._cnt != 0, u"Can't pop an empty vector")94 if self._cnt == 1:95 return EMPTY96 if self._cnt - self.tailoff() > 1:97 size = len(self._tail) - 198 assert size >= 0 # for translation99 new_tail = self._tail[:size]100 return PersistentVector(self._meta, self._cnt - 1, self._shift, self._root, new_tail)101 new_tail = self.array_for(self._cnt - 2)102 new_root = self.pop_tail(self._shift, self._root)103 new_shift = self._shift104 if new_root is None:105 new_root = EMPTY_NODE106 if self._shift > 5 and new_root._array[1] is None:107 new_root = new_root._array[0]108 new_shift -= 5109 return PersistentVector(self._meta, self._cnt - 1, new_shift, new_root, new_tail)110 def pop_tail(self, level, node):111 sub_idx = ((self._cnt - 1) >> level) & 0x01f112 if level > 5:113 assert isinstance(node, Node)114 new_child = self.pop_tail(level - 5, node._array[sub_idx])115 if new_child is None or sub_idx == 0:116 return None117 else:118 root = self._root119 assert isinstance(root, Node)120 ret = Node(root._edit, node._array[:])121 ret._array[sub_idx] = new_child122 return ret123 elif sub_idx == 0:124 return None125 else:126 root = self._root127 assert isinstance(root, Node)128 assert isinstance(node, Node)129 ret = Node(root._edit, node._array[:])130 ret._array[sub_idx] = None131 return ret132 def assoc_at(self, idx, val):133 if idx >= 0 and idx < self._cnt:134 if idx >= self.tailoff():135 new_tail = self._tail[:]136 new_tail[idx & 0x01f] = val137 return PersistentVector(self._meta, self._cnt, self._shift, self._root, new_tail)138 return PersistentVector(self._meta, self._cnt, self._shift, do_assoc(self._shift, self._root, idx, val), self._tail)139 if idx == self._cnt:140 return self.conj(val)141 else:142 object.runtime_error(u"index out of range",143 u"pixie.stdlib/OutOfRangeException")144def do_assoc(lvl, node, idx, val):145 assert isinstance(node, Node)146 ret = Node(node._edit, node._array[:])147 if lvl == 0:148 ret._array[idx & 0x01f] = val149 else:150 subidx = (idx >> lvl) & 0x01f151 ret._array[subidx] = do_assoc(lvl - 5, node._array[subidx], idx, val)152 return ret153def new_path(edit, level, node):154 if level == 0:155 return node156 ret = Node(edit)157 ret._array[0] = new_path(edit, level - 5, node)158 return ret159edited = u"edited"160class TransientVector(object.Object):161 _type = object.Type(u"pixie.stdlib.TransientVector")162 def __init__(self, cnt, shift, root, tail):163 self._cnt = cnt164 self._shift = shift165 self._root = root166 self._tail = tail167 @staticmethod168 def editable_root(node):169 assert isinstance(node, Node)170 return Node(edited, node._array[:])171 def ensure_editable(self):172 root = self._root173 assert isinstance(root, Node)174 affirm(root._edit is not None, u"Transient used after call to persist!")175 def ensure_node_editable(self, node):176 assert isinstance(node, Node)177 root = self._root178 assert isinstance(root, Node)179 if node._edit is root._edit:180 return node181 root = self._root182 assert isinstance(root, Node)183 return Node(root._edit, node._array[:])184 def tailoff(self):185 if self._cnt < 32:186 return 0187 return ((self._cnt - 1) >> 5) << 5188 def persistent(self):189 self.ensure_editable()190 root = self._root191 assert isinstance(root, Node)192 root._edit = None193 trimmed = [None] * (self._cnt - self.tailoff())194 list_copy(self._tail, 0, trimmed, 0, len(trimmed))195 return PersistentVector(nil, self._cnt, self._shift, self._root, trimmed)196 @staticmethod197 def editable_tail(tl):198 ret = [None] * 32199 list_copy(tl, 0, ret, 0, len(tl))200 return ret201 def conj(self, val):202 self.ensure_editable()203 i = self._cnt204 if i - self.tailoff() < 32:205 self._tail[i & 0x01f] = val206 self._cnt += 1207 return self208 root = self._root209 assert isinstance(root, Node)210 tail_node = Node(root._edit, self._tail)211 self._tail = [None] * 32212 self._tail[0] = val213 new_shift = self._shift214 if (self._cnt >> 5) > (r_uint(1) << self._shift):215 new_root = Node(root._edit)216 new_root._array[0] = self._root217 new_root._array[1] = new_path(root._edit, self._shift, tail_node)218 new_shift += 5219 else:220 new_root = self.push_tail(self._shift, self._root, tail_node)221 self._root = new_root222 self._shift = new_shift223 self._cnt += 1224 return self225 def push_tail(self, level, parent, tail_node):226 parent = self.ensure_node_editable(parent)227 root = self._root228 assert isinstance(root, Node)229 sub_idx = ((self._cnt - 1) >> level) & 0x01f230 ret = parent231 if level == 5:232 node_to_insert = tail_node233 else:234 child = parent._array[sub_idx]235 if child is not None:236 node_to_insert = self.push_tail(level - 5, child, tail_node)237 else:238 node_to_insert = new_path(root._edit, level - 5, tail_node)239 ret._array[sub_idx] = node_to_insert240 return ret241 def array_for(self, i):242 if 0 <= i < self._cnt:243 if i >= self.tailoff():244 return self._tail245 node = self._root246 level = self._shift247 while level > 0:248 assert isinstance(node, Node)249 node = node._array[(i >> level) & 0x01f]250 level -= 5251 return node._array252 affirm(False, u"Index out of Range")253 def editable_array_for(self, i):254 if i >= 0 and i < self._cnt:255 if i >= self.tailoff():256 return self._tail257 node = self._root258 level = self._shift259 while level > 0:260 node = self.ensure_node_editable(node._array[(i >> level) & 0x1f])261 level -= 5262 return node._array263 affirm(False, u"Index out of bounds")264 def nth(self, i, not_found=nil):265 self.ensure_editable()266 if 0 <= i < self._cnt:267 node = self.array_for(r_uint(i))268 return node[i & 0x01f]269 return not_found270 def pop(self):271 self.ensure_editable()272 affirm(self._cnt != 0, u"Can't pop an empty vector")273 i = self._cnt - 1274 if i & 0x01f > 0:275 self._cnt -= 1276 return self277 new_tail = self.editable_array_for(self._cnt - 1)278 new_root = self.pop_tail(self._shift, self._root)279 new_shift = self._shift280 root = self._root281 assert isinstance(root, Node)282 if new_root is None:283 new_root = Node(root._edit)284 if self._shift > 5 and new_root._array[1] is None:285 new_root = self.ensure_node_editable(new_root._array[0])286 new_shift -= 5287 self._root = new_root288 self._shift = new_shift289 self._cnt -= 1290 self._tail = new_tail291 return self292 def pop_tail(self, level, node):293 node = self.ensure_node_editable(node)294 sub_idx = ((self._cnt - 2) >> level) & 0x01f295 if level > 5:296 new_child = self.pop_tail(level - 5, node._array[sub_idx])297 if new_child is None and sub_idx == 0:298 return None299 else:300 ret = node301 ret._array[sub_idx] = new_child302 return ret303 elif sub_idx == 0:304 return None305 else:306 ret = node307 ret._array[sub_idx] = None308 return ret309@jit.unroll_safe310def list_copy(from_lst, from_loc, to_list, to_loc, count):311 from_loc = r_uint(from_loc)312 to_loc = r_uint(to_loc)313 count = r_uint(count)314 i = r_uint(0)315 while i < count:316 to_list[to_loc + i] = from_lst[from_loc + i]317 i += 1318 return to_list319@extend(proto._count, PersistentVector)320def _count(self):321 assert isinstance(self, PersistentVector)322 return rt.wrap(intmask(self._cnt))323@extend(proto._nth, PersistentVector)324def _nth(self, idx):325 assert isinstance(self, PersistentVector)326 return self.nth(idx.int_val())327@extend(proto._nth_not_found, PersistentVector)328def _nth_not_found(self, idx, not_found):329 assert isinstance(self, PersistentVector)330 return self.nth(idx.int_val(), not_found)331@extend(proto._val_at, PersistentVector)332def _val_at(self, key, not_found):333 assert isinstance(self, PersistentVector)334 if isinstance(key, Integer):335 return self.nth(key.int_val(), not_found)336 else:337 return not_found338@extend(proto._eq, PersistentVector)339def _eq(self, obj):340 assert isinstance(self, PersistentVector)341 if self is obj:342 return true343 elif isinstance(obj, PersistentVector):344 if self._cnt != obj._cnt:345 return false346 for i in range(0, intmask(self._cnt)):347 if not rt.eq(self.nth(i), obj.nth(i)):348 return false349 return true350 else:351 if obj is nil or not rt._satisfies_QMARK_(proto.ISeqable, obj):352 return false353 seq = rt.seq(obj)354 for i in range(0, intmask(self._cnt)):355 if seq is nil or not rt.eq(self.nth(i), rt.first(seq)):356 return false357 seq = rt.next(seq)358 if seq is not nil:359 return false360 return true361@extend(proto._contains_key, PersistentVector)362def _contains_key(self, key):363 assert isinstance(self, PersistentVector)364 if not isinstance(key, Integer):365 return false366 else:367 return true if key.int_val() >= 0 and key.int_val() < intmask(self._cnt) else false368@extend(proto._conj, PersistentVector)369def _conj(self, v):370 assert isinstance(self, PersistentVector)371 return self.conj(v)372@extend(proto._push, PersistentVector)373def _push(self, v):374 assert isinstance(self, PersistentVector)375 return self.conj(v)376@extend(proto._pop, PersistentVector)377def _pop(self):378 assert isinstance(self, PersistentVector)379 return self.pop()380@extend(proto._assoc, PersistentVector)381def _assoc(self, idx, val):382 assert isinstance(self, PersistentVector)383 affirm(isinstance(idx, Integer), u"key must be an integer")384 return self.assoc_at(r_uint(idx.int_val()), val)385@extend(proto._meta, PersistentVector)386def _meta(self):387 assert isinstance(self, PersistentVector)388 return self.meta()389@extend(proto._with_meta, PersistentVector)390def _with_meta(self, meta):391 assert isinstance(self, PersistentVector)392 return self.with_meta(meta)393_reduce_driver = jit.JitDriver(name="pixie.stdlib.PersistentVector_reduce",394 greens=["f"],395 reds="auto")396@extend(proto._reduce, PersistentVector)397def _reduce(self, f, init):398 assert isinstance(self, PersistentVector)399 i = 0400 while i < self._cnt:401 array = self.array_for(i)402 for j in range(len(array)):403 _reduce_driver.jit_merge_point(f=f)404 init = f.invoke([init, array[j]])405 if rt.reduced_QMARK_(init):406 return rt.deref(init)407 step = len(array)408 i += step409 return init410@as_var("vector")411def vector__args(args):412 acc = rt._transient(EMPTY)413 for x in range(len(args)):414 acc = rt._conj_BANG_(acc, args[x])415 return rt._persistent_BANG_(acc)...

Full Screen

Full Screen

func_map.py

Source:func_map.py Github

copy

Full Screen

...8def f(x):9 # return math.sqrt(x)10 return np.sqrt(x)11vf = np.vectorize(f)12def array_for(x):13 return np.array([f(xi) for xi in x])14def array_map(x):15 return np.array(list(map(f, x)))16def fromiter(x):17 return np.fromiter((f(xi) for xi in x), x.dtype)18def vectorize(x):19 return np.vectorize(f)(x)20def vectorize_without_init(x):21 return vf(x)22perfplot.show(23 setup=lambda n: np.random.rand(n),24 n_range=[2**k for k in range(20)],25 kernels=[26 f,...

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