Utility Module

hedwig.util

class hedwig.util.FormattedLogger(logger)

Logger wrapper, similar to logging.LoggerAdapter, which uses string formatting.

debug(msg, *args, **kwargs)
info(msg, *args, **kwargs)
warning(msg, *args, **kwargs)
error(msg, *args, **kwargs)
exception(msg, *args, **kwargs)
critical(msg, *args, **kwargs)
log(level, msg, *args, **kwargs)
hedwig.util.get_logger(name)

Get a “FormattedLogger” instance which uses string formatting.

hedwig.util.is_list_like(value)

Returns true if the value is a list-like object such as a list or a tuple.

Currently just returns true if it is a list or tuple or set.

hedwig.util.item_combinations(dictionary, function, combine, filter_combination=<function <lambda>>)

Create a dictionary of combinations of items from the given dictionary. Each item has function applied to it and each pair is then combined with combine. The resulting dictionary has keys which are pairs of keys from that given. Only combinations for which filter_combination returns a true value are included.

hedwig.util._item_combinations(iterator, function, combine, filter_combination)
hedwig.util.list_in_blocks(iterable, block_size)

Given an iterable object (e.g. a list) yield a sequence of lists each containing up to block_size items.

hedwig.util.list_in_ranges(iterable, min_range_size=3)

Given an iterable object (e.g. a list) of integers, return a pair containing ranges and individual items.

Items in ranges less than min_range_size are returned as individual items.

Returns:

([(range_min, range_max), …], [individual_item, …])

hedwig.util.lower_except_abbr(value)

Converts a string to lower case, except for abbreviations.

Abbreviations are curently identifed as sequences of 2 or more upper case letters.

hedwig.util.matches_constraint(value, constraint)

Check whether a value matches a constraint.

  • If the constraint is None, returns True.

  • If the constraint appears like-like, return True if the value is in it.

  • Otherwise return True if the value is equal to the constraint.

class hedwig.util.ClosingMultiple

Context manager object for handling closing multile file objects.

class hedwig.util.FormatMaxDP(digits=3)

Class for formatting numbers to a maximum number of decimal places.

Formats numbers to a certain number of decimal places, removing any trailing zeros and decimal point.

Instances of this class are intended to work like format strings, in that they have a format method which is called with the number to be formatted.

trailing = re.compile('\\.?0+$')
format(value)
class hedwig.util.FormatSigFig(digits=3, digits_min=0, digits_max=9)

Class for formatting numbers to a given number of significant figures.

Formats numbers to a certain number of significant figures, but without using exponent notation. Only the number of decimal places is controlled – any number with the requested number of figures, or more, will be simply be formatted with zero decimal places.

Instances of this class are intended to work like format strings, in that they have a format method which is called with the number to be formatted.

format(value)