casagui.utils

General utility functions used by the casagui tools and applications.

Package Contents

Functions

path_to_url(path)

Convert a single filesystem path to a URL.

find_ws_address([address])

Find free port on address network and return a tuple with address and port number

partition(pred, iterable)

Split iterable into two lists based on pred predicate.

error_msg(*args, **kwargs)

standard method for reporting errors which do not result in aborting out of python

warn_import(package)

standard method for reporting (optional) package import failure

have_network()

check to see if an active network with general internet connectivity

ranges(iterable[, order, key])

collect elements of iterable into tuple ranges where each tuple represents

contiguous_ranges(iterable[, order, key])

split iterable into contiguous index sequence, i.e. reduce consecutive index runs

expand_range_incl(r)

expand the tuple supplied as r into a range which includes the first

index_to_stokes(index)

Convert stokes axis index to alphabetic value.

convert_masks(→ list)

Convert masks in standard format (as defined by CubeMask.jsmask_to_raw) into

set_attributes(obj, **kw)

Given an object and a set of keyword arguments, set the attributes

casagui.utils.path_to_url(path)

Convert a single filesystem path to a URL.

If the string specified in the path parameter exists. It is turned into a fully qualified path and converted to a URL and returned. If path does not exist, path is returned unchanged.

Parameters:

path (str) – path to be checked and expanded

Returns:

path converted to a URL if path exists, otherwise path unchanged

Return type:

str

casagui.utils.find_ws_address(address='127.0.0.1')

Find free port on address network and return a tuple with address and port number

This function uses the low level socket function to find a free port and return a tuple representing the address plus port number.

Parameters:

address (str) – network to be probed for an available port

Returns:

network address (str) and port number (int)

Return type:

tuple of str and int

casagui.utils.partition(pred, iterable)

Split iterable into two lists based on pred predicate.

casagui.utils.error_msg(*args, **kwargs)

standard method for reporting errors which do not result in aborting out of python

This function takes the standard set of arguments that the python print function takes. The primary difference is that the output will go to stderr and perhaps other error logs.

casagui.utils.warn_import(package)

standard method for reporting (optional) package import failure

Parameters:

package (str) – name of a package whose attempted import failed

casagui.utils.have_network()

check to see if an active network with general internet connectivity is available. returns True if we have internet connectivity and False if we do not.

casagui.utils.ranges(iterable, order=sorted, key=lambda x: ...)

collect elements of iterable into tuple ranges where each tuple represents a concesecutive range within the iterable. key can be used to provide ranges for other objects where key(element) returns the key to be used for sorting into ranges

casagui.utils.contiguous_ranges(iterable, order=sorted, key=lambda x: ...)

split iterable into contiguous index sequence, i.e. reduce consecutive index runs to a tuple containing the first and last. iterable can be a sequence of values that can be subtracted or a list of more complex values where the function key returns the key to be used for ordering. iterable is ordered by order which by default sorts iterable.

casagui.utils.expand_range_incl(r)

expand the tuple supplied as r into a range which includes the first and last element r

casagui.utils.index_to_stokes(index: int)

Convert stokes axis index to alphabetic value.

Parameters:

index (int) – enumerated index defining stokes value.

Returns:

String indicating stokes value.

Return type:

str

casagui.utils.convert_masks(masks: dict, coord='pixel', cdesc=None) list

Convert masks in standard format (as defined by CubeMask.jsmask_to_raw) into other formats like list of CRTF, single region, etc.

Parameters:
  • masks (dict) – Dictionary containing masks and polys keys. The values for masks are the polygon references for each channel and the values for polys contain the points making up each polygon.

  • coord (str) – Coordinate system that should be used in the returned masks. Allowed values are ‘pixel’.

  • cdesc (dict) – Dictionary containing csys and shape which describes the coordinate system to be used for creating world coordinate coordinates. The shape is required along with the coordinate system for coordinate conversion.

casagui.utils.set_attributes(obj, **kw)

Given an object and a set of keyword arguments, set the attributes in the object that correspond to the keywords to the specified values.

Parameters:
  • obj (object) – Object whose attributes should be set

  • kw (keyword and object) – Attributes to be set

Returns:

obj parameter

Return type:

object