5.1.1.10.10. Slice

This module implements interval arithmetic for flat [1] and line slice.

[1]Flat indexes address characters in a file.
class CodeReview.Tools.Slice.FlatSlice(*args)[source]

Bases: CodeReview.Tools.Slice.Slice

This class defines a flat slice.

class CodeReview.Tools.Slice.LineSlice(*args)[source]

Bases: CodeReview.Tools.Slice.Slice

This class defines a line slice.

class CodeReview.Tools.Slice.Slice(*args)[source]

Bases: object

This class implements a generic slice.

Like for a standard Python slice, a Slice instance is built from two values or an indexable object providing the start and stop value. This class implements an array interface (__getitem__() method) so as to pass a Slice instance to the constructor. Some examples to build a slice:

slice1 = Slice(1, 2)
slice2 = Slice((1, 2))
slice2 = Slice([1, 2])
slice3 = Slice(slice2)

The interval limits of the slice can be accessed using the start, stop, lower and upper read-only attributes, upper is defined as stop -1.

To cast an instance to a standard Python slice use the call:

slice()

To get the length of the slice defined by stop - start use the len() function.

A slice is not empty if it verifies the predicate stop > start, this predicate can be tested using a Boolean evaluation of the instance.

A slice can be scaled by a factor using:

slice // 2
slice //= 2

The union and the intersection of two slices can be computed using:

# union
slice1 |= slice2
slice = slice1 | slice2

# intersection
slice1 &= slice2
slice = slice1 & slice2
_check_arguments(args)[source]

Check the arguments provided to the constructor.

static _intersection(i1, i2)[source]

Compute the intersection of two slices.

static _union(i1, i2)[source]

Compute the union of two slices.

copy()[source]

Return a copy of the slice.

intersect(i1, i2)[source]

Test if the interval intersects with i2.

is_included_in(i1, i2)[source]

Test if the interval is included in i2.

lower

Lower boundary.

map(sub_slice)[source]

Map a sub-slice in the slice domain.

Return a new slice shifted by start.

start

Start boundary.

stop

Stop boundary.

upper

Upper boundary.