misc: Miscellaneous functionality

Miscellaneous functionality

  • Search: nearest_element, nearest_unsorted, nearest_index

  • Sort: sort_natural, zipsort, issorted

  • Namedtuples: namedtupled_addcolumn, namedtuple_extend, etc.

  • Open files: open_with_standard_app, wait_for_file_modified, open_with

  • Unit conversions: cm_to_pixels, page_dinsize_to_mm, etc.

  • Other: singleton

Functions

allequal(xs)

Return True if all elements in xs are equal

asnumber(obj[, accept_fractions, accept_expon])

Return obj as number, or None of it cannot be converted to a number

assert_type(x, *types)

Returns True if x is of the given type, raises TypeError otherwise

astype(type_[, obj, factory])

Return obj as type.

can_be_pickled(obj)

Return True if obj can be pickled

cm_to_pixels(cm[, dpi])

convert a distance in cm to pixels

deepupdate(orig, updatewith)

recursively update orig with updatewith

dictmerge(dict1, dict2)

Merge the contents of the two dicts.

distribute_in_zones(x, split_points[, side])

Returns the index of a "zone" where to place x.

dumpobj(obj)

Return all 'public' attributes of this object

duplicates(seq[, mincount])

Find all elements in seq which are present at least mincount times

fig2data(fig)

Convert a Matplotlib figure to a 4D numpy array with RGBA channels

find_attrs(obj[, excludeprefix])

Iterate over all attributes of objects.

first_existing_path(*paths[, default])

Returns the first path in paths which exists

firstval(*values[, sentinel])

Get the first value in values which is not sentinel.

fractional_slice(seq, step[, start, end])

Given a list of elements, take a slice similar to seq[start:end:step], but allows step to be a fraction

html_table(rows, headers[, maxwidths, ...])

Create a html table

inches_to_pixels(inches[, dpi])

Convert inches to pixels

isgeneratorlike(obj)

Does obj behave like a generator? (it can be iterated but has no length)

isiterable(obj[, exceptions])

rtype:

bool

issorted(seq[, key])

Returns True if seq is sorted

istype(obj, *types)

Examples.

moses(pred, seq)

Divides seq into two lists: filter(pred, seq), filter(not pred, seq)

namedtuple_addcolumn(namedtuples, seq, ...)

Add a column to a sequence of named tuples

namedtuple_extend(name, orig, columns)

Create a new namedtuple constructor with the added columns

namedtuples_renamecolumn(namedtuples, ...[, ...])

Rename the column of a seq of namedtuples

nearest_element(item, seq)

Find the nearest element (the element, not the index) in seq

nearest_index(item, seq)

Return the index of the nearest element in seq to item

nearest_unsorted(x, seq)

Find nearest item in an unsorted sequence

open_with_app(path[, app, wait, shell, ...])

Open a given file with a given app.

page_dinsize_to_mm(pagesize, pagelayout)

Return the (height, width) for a given DIN size and page orientation

parse_time(t)

Parse a time string HH:MM:SS.mmm and convert it to seconds

pick_regularly(seq, numitems[, start_idx, ...])

Given a sequence, pick numitems from it at regular intervals

pixels_to_cm(pixels[, dpi])

Convert a distance in pixels to cm

pixels_to_inches(pixels[, dpi])

Convert pixels to inches

print_table(rows[, headers, tablefmt, ...])

Print rows as table

public(f)

decorator - keeps __all__ updated

remove_duplicates(seq)

Remove all duplicates in seq while keeping its order If order is not important, use list(set(seq))

remove_last_matching(seq, func)

Remove last element of seq matching the given condition, in place

replace_sigint_handler(handler)

Replace current SIGINT hanler with the given one, return the old one

returns_tuple(names[, recname])

Decorator - Makes the function return a namedtuple with the given names.

returns_tuples(names, recname)

Decorator to make a function return a RecordList instead of a list of tuples

reverse_recursive(seq)

Reverse seq recursively

rgb_to_hex(r, g, b)

Convert a color in rgb to its hex representation

sec2str(seconds[, msdigits])

Convert seconds to a suitable string representation

seq_contains(seq, subseq)

Returns the (start, end) indexes if seq contains subseq, or None

simplify_breakpoints(bps, coordsfunc[, ...])

Simplify breakpoints in a breakpoint function

singleton(cls)

A class decorator to create a singleton class

snap_array(X, tick[, offset, out, nearest])

Snap the values of X to the nearest slot in a grid

snap_to_grid(x, tick[, offset, nearest])

Find the nearest slot in a grid

snap_to_grids(x, ticks[, offsets, mode])

Snap x to the nearest slot within multiple overlapping grids

some(x[, otherwise])

This allows code like.

sort_natural(seq[, key])

sort a string sequence naturally

sort_natural_dict(d[, recursive])

sort dict d naturally and recursively

str_is_number(s[, accept_exp, accept_fractions])

Returns True if the given string represents a number

sumlist(seq)

Concatenate multiple lists to one big list

type_error_msg(x, *expected_types)

To be used when raising a TypeError

wait_for_file_modified(path[, timeout])

Wait until file is modified.

wrap_by_sizes(flatseq, packsizes)

Wrap a flat seq using the given sizes

zipsort(a, b[, key, reverse])

Sort a and keep b in sync

Classes

ReprMixin()

Mixin class to provide automatic __repr__

runonce(func)

To be used as decorator.

temporary_sigint_handler(handler)

Context manager to install a temporary sigint handler