segpy.util module

class segpy.util.SortSense

Bases: enum.Enum

An enumeration.

ascending = 0
descending = 1
segpy.util.all_equal(iterable)

Returns True if all the elements are equal to each other

segpy.util.almost_equal(x, y, epsilon=2.220446049250313e-16)
segpy.util.batched(iterable, batch_size, padding=<object object>)

Batch an iterable series into equal sized batches.

Parameters:
  • iterable – The series to be batched.
  • batch_size – The size of the batch. Must be at least one.
  • padding – Optional value used to pad the final batch to batch_size. If omitted, the final batch may be smaller than batch_size.
Returns:

A series of lists, each containing batch_size items from iterable.

Raises:

ValueError - If batch_size is less than one.

segpy.util.cmp(x, y)

Compare the two objects x and y and return an integer according to the outcome.

Parameters:
  • x – The first number to compare.
  • y – The second number to compare.
Returns:

A negative value if x < y, zero if x == y and positive if x > y.

segpy.util.collect_attributes(derived_class, base_class, predicate)
Returns:A generator of items containing the (class, attribute_name)
segpy.util.complementary_intervals(intervals, start=None, stop=None)

Compute a complementary set of intervals which alternate with given intervals to form a contiguous range.

Given,

Start Stop
[—–) [—–) [—-)

produces,

[–) [—-) [-) [—)
Parameters:
  • intervals – An sequence of at least one existing slices or ranges. The type of the first interval (slice or range) is used as the result type.
  • start – An optional start index, defaults to the start of the first slice.
  • stop – An optional one-beyond-the-end index, defaults to the stop attribute of the last slice.
Returns:

A complementary series of slices which alternate with the supplied slices. The number of returned slices will always be len(slices) + 1 since both leading and trailing slices will always be returned. Note the some of the returned slices may be ‘empty’ (having zero length).

segpy.util.compress_sorted_sequence_to_range(sorted_sequence)

Attempt to represent the supplied sequence as a range.

Useful for reducing the size of large stored integer sequences.

Parameters:sorted_sequence – A sequence of integers which may be ordered in an ascending or descending sense.
Returns:An ordered sequence which may be a range or may be the unaltered argument.
segpy.util.contains_duplicates(sorted_iterable)

Determine if an iterable series contains duplicates.

Parameters:sorted_iterable – Any iterable series which must be sorted in either ascending or descending order.
Returns:True if sorted_iterable contains duplicates, otherwise False.
segpy.util.file_length(fh)

Determine the length of a file-like object in bytes.

Parameters:fh – A seekable file-like-object.
Returns:An integer length in bytes.
segpy.util.filename_from_handle(fh)

Determine the name of the file underlying a file-like object.

Parameters:fh – A file-like object.
Returns:A string containing the file name, or UNKNOWN_FILENAME if it could not be determined.
segpy.util.first(iterable)
segpy.util.first_sentence(s)
segpy.util.four_bytes(byte_str)
segpy.util.hash_for_file(fh, *args)

Compute the SHA1 hash for file combined with any stringified additional args.

The resulting hash is based on both the contents and length of the supplied file- like object.

Parameters:
  • fh – A file-like object opened in binary mode.
  • *args – The stringified values of ny additional arguments with be combined with the file data used to compute the hash.
Returns:

A string containing the hexadecimal digest.

segpy.util.identity(x)
segpy.util.intervals_are_contiguous(intervals)

Determine whether a series of intervals are contiguous.

Parameters:intervals – An iterable series of intervals where each interval is either a range or slice object.
Returns:True if the intervals are in order, contiguous and non-overlapping, otherwise False.
segpy.util.intervals_partially_overlap(interval_a, interval_b)

Determine whether two intervals partially overlap.

Parameters:
  • interval_a – A range or slice object.
  • interval_b – A range or slice object.
Returns:

True if interval_a partially overlaps interval_b, otherwise False if the intervals are either disjoint or exactly coincident.

segpy.util.is_magic_name(name)
segpy.util.is_sorted(iterable, key=None, reverse=False, distinct=False)
segpy.util.last(iterable)
segpy.util.lower_first(s)

Lower case the first character of a string.

segpy.util.make_sorted_distinct_sequence(iterable, sense=<SortSense.ascending: 0>)

Create a sorted immutable sequence from an iterable series.

The resulting collected will be sorted ascending.

Parameters:
  • iterable – An iterable series of comparable values.
  • sense – If None, the any original sense of the data is preserved, so ascending data remains ascending, and descending data remains descending. If the original data was unsorted, the result will be ascending. Force a particular sense by specifying SortSense.ascending or SortSense.descending.
Returns:

An immutable collection which supports the Sized, Iterable, Container and Sequence protocols.

segpy.util.measure_stride(iterable)

Determine whether successive numeric items differ by a constant amount.

Parameters:iterable – An iterable series of numeric values.
Returns:The difference between successive values (e.g. item[1] - item[0]) if that difference is the same between all successive pairs, otherwise None.
segpy.util.minmax(iterable)

Return the minimum and maximum of an iterable series.

This function requires only a single pass over the data.

Parameters:iterable – An iterable series for which to determine the minimum and maximum values.
Returns:A 2-tuple containing the minimum and maximum values.
segpy.util.now_millis()
segpy.util.pad(iterable, padding=None, size=None)
segpy.util.pairwise(iterable)
segpy.util.restored_position_seek(fh, pos)
segpy.util.reversed_range(r)

Given a range object produce the reversed range.

Parameters:r – A range object.
Returns:The reversed range.
segpy.util.round_up(integer, multiple)

Round up to the nearest multiple

segpy.util.roundrobin(*iterables)

Take items from each iterable in turn until all iterables are exhausted.

roundrobin(‘ABC’, ‘D’, ‘EF’) –> A D E B F C

segpy.util.sgn(x)

The sign of a number.

Parameters:x – The number for which to compute the sign.
Returns:+1 is x is positive, -1 if x is negative, 0 if x is zero.
segpy.util.single_item_range(item)

Construct a range object which generates a single value.

segpy.util.super_class(cls)

Return the next class in the MRO of cls, or object.

segpy.util.underscores_to_camelcase(s)

Convert text_in_this_style to TextInThisStyle.