MCPcopy Index your code
hub / github.com/python/cpython / rand_aligned_slices

Function rand_aligned_slices

Lib/test/test_buffer.py:596–637  ·  view source on GitHub ↗

Create (lshape, rshape, tuple(lslices), tuple(rslices)) such that shapeof(x[lslices]) == shapeof(y[rslices]), where x is an array with shape 'lshape' and y is an array with shape 'rshape'.

(maxdim=5, maxshape=16)

Source from the content-addressed store, hash-verified

594 return tuple(lslices), tuple(rslices)
595
596def rand_aligned_slices(maxdim=5, maxshape=16):
597 """Create (lshape, rshape, tuple(lslices), tuple(rslices)) such that
598 shapeof(x[lslices]) == shapeof(y[rslices]), where x is an array
599 with shape 'lshape' and y is an array with shape 'rshape'."""
600 ndim = randrange(1, maxdim+1)
601 minshape = 2
602 n = randrange(100)
603 if n >= 95:
604 minshape = 0
605 elif n >= 90:
606 minshape = 1
607 all_random = True if randrange(100) >= 80 else False
608 lshape = [0]*ndim; rshape = [0]*ndim
609 lslices = [0]*ndim; rslices = [0]*ndim
610
611 for n in range(ndim):
612 small = randrange(minshape, maxshape+1)
613 big = randrange(minshape, maxshape+1)
614 if big < small:
615 big, small = small, big
616
617 # Create a slice that fits the smaller value.
618 if all_random:
619 start = randrange(-small, small+1)
620 stop = randrange(-small, small+1)
621 step = (1,-1)[randrange(2)] * randrange(1, small+2)
622 s_small = slice(start, stop, step)
623 _, _, _, slicelen = slice_indices(s_small, small)
624 else:
625 slicelen = randrange(1, small+1) if small > 0 else 0
626 s_small = randslice_from_slicelen(slicelen, small)
627
628 # Create a slice of the same length for the bigger value.
629 s_big = randslice_from_slicelen(slicelen, big)
630 if randrange(2) == 0:
631 rshape[n], lshape[n] = big, small
632 rslices[n], lslices[n] = s_big, s_small
633 else:
634 rshape[n], lshape[n] = small, big
635 rslices[n], lslices[n] = s_small, s_big
636
637 return lshape, rshape, tuple(lslices), tuple(rslices)
638
639def randitems_from_structure(fmt, t):
640 """Return a list of random items for structure 't' with format

Callers 1

Calls 3

sliceClass · 0.85
slice_indicesFunction · 0.85
randslice_from_slicelenFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…