MCPcopy Index your code
hub / github.com/RustPython/RustPython / rand_aligned_slices

Function rand_aligned_slices

Lib/test/test_buffer.py:584–625  ·  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

582 return tuple(lslices), tuple(rslices)
583
584def rand_aligned_slices(maxdim=5, maxshape=16):
585 """Create (lshape, rshape, tuple(lslices), tuple(rslices)) such that
586 shapeof(x[lslices]) == shapeof(y[rslices]), where x is an array
587 with shape 'lshape' and y is an array with shape 'rshape'."""
588 ndim = randrange(1, maxdim+1)
589 minshape = 2
590 n = randrange(100)
591 if n >= 95:
592 minshape = 0
593 elif n >= 90:
594 minshape = 1
595 all_random = True if randrange(100) >= 80 else False
596 lshape = [0]*ndim; rshape = [0]*ndim
597 lslices = [0]*ndim; rslices = [0]*ndim
598
599 for n in range(ndim):
600 small = randrange(minshape, maxshape+1)
601 big = randrange(minshape, maxshape+1)
602 if big < small:
603 big, small = small, big
604
605 # Create a slice that fits the smaller value.
606 if all_random:
607 start = randrange(-small, small+1)
608 stop = randrange(-small, small+1)
609 step = (1,-1)[randrange(2)] * randrange(1, small+2)
610 s_small = slice(start, stop, step)
611 _, _, _, slicelen = slice_indices(s_small, small)
612 else:
613 slicelen = randrange(1, small+1) if small > 0 else 0
614 s_small = randslice_from_slicelen(slicelen, small)
615
616 # Create a slice of the same length for the bigger value.
617 s_big = randslice_from_slicelen(slicelen, big)
618 if randrange(2) == 0:
619 rshape[n], lshape[n] = big, small
620 rslices[n], lslices[n] = s_big, s_small
621 else:
622 rshape[n], lshape[n] = small, big
623 rslices[n], lslices[n] = s_small, s_big
624
625 return lshape, rshape, tuple(lslices), tuple(rslices)
626
627def randitems_from_structure(fmt, t):
628 """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