util

This module contains the inevitable collection of miscellaneous functions essential for polymr’s proper working.


polymr.util.avg(l)

Compute the arithmetic mean on a list of numbers.

Returns:float
polymr.util.cat()

chain.from_iterable(iterable) –> chain object

Alternate chain() contructor taking a single iterable argument that evaluates lazily.

polymr.util.jaccard(a, b)

Compute the Jaccard distance between two strings.

Returns:float
polymr.util.merge_to_range(ls)

Compact an iterable of integer lists into a series of ranges, where a range is encoded as a list of stop and end ints: [stop, int]. The function also returns an indicator describing whether any compaction was applied.

>>> polymr.util.merge_to_range([[1,3,4,7], [2,5,8]])
([[1, 5], [7, 8]], True)
polymr.util.ngrams(s, k=3, step=1)

Generate a list of ngrams by sliding window. An example is probably best:

>>> polymr.util.ngrams('bon jovi', 3, 1)
['bon', 'on ', 'n j', ' jo', 'jov', 'ovi']
Parameters:
  • s (str) – The string to break into ngrams
  • k (int) – The size of each ngram to make, also known as window length or kmer size.
  • step (int) – The step size: how many character positions to jump forward after each ngram.
Returns:

list of str

polymr.util.openfile(filename_or_handle, mode='r')

Open a filename, or, if given a file handle, return the handle as-is.

Parameters:
  • filename_or_handle (str or file handle) – The file or handle to open
  • mode (str) – The file mode c.f. Python’s built-in open function.
Returns:

open file handle