MOAL.helpers package

Submodules

MOAL.helpers.adts module

MOAL.helpers.adts.dict_fill(length, fill=0)[source]

Fill a dict with integer keys and a custom fill value.

class MOAL.helpers.adts.intlist(*args)[source]

Bases: MOAL.helpers.adts.strictlist

__init__(*args)
add(addend)
div(divisor)
fact()
mod(base)
mul(multiplicand)
sub(subtrahend)
MOAL.helpers.adts.list_fill(length, fill=0)[source]

Fill a list with default values and a custom fill value.

MOAL.helpers.adts.matrix_fill(w, h, fill=0)[source]

Generates a two-dimensional matrix of W x H, filled with zeroes or a custom fill value.

e.g. [[0, 0, 0],
[0, 0, 0], [0, 0, 0]]
class MOAL.helpers.adts.strictlist(items, valid_type=None)[source]

Bases: list

A list that only allows certain primitive types (when sub-classed), and adds some useful helper methods that are applied to all members in the list, for that primitive.

__getitem__(index)[source]
__init__(items, valid_type=None)[source]

Instantiate a new strictlist object

Args:
items (list): a list of potential items for to be filtered.
Kwargs:
valid_type (type): an example type this list must validate against.
>>> print strictlist([1, 2, 'foo'], valid_type=int)
[1, 2]
__iter__()[source]

Returns an iterator of the embedded items.

__len__()[source]

Returns the length of the embedded items.

__repr__()[source]
__setitem__(index, value)[source]

Set new items in the list.

Args:

index (int) - the index to add/update an item for. value (mixed) - the item to add; this will be checked against

the type specific upon class creation.

Don’t allow incorrect types to be added, but do so gracefully to allow for composition/flow operations.

__str__()[source]
_valid(val)[source]

Determines if the given value matches the type.

The correct type associated with this instance upon creation. See __init__ for more.

Args:
val (mixed) - the value to check
do(func, val=None)[source]
funcmaker(func, baseval)[source]

Creates a closure that can be used later with map()’s value(s)

class MOAL.helpers.adts.strlist(*args)[source]

Bases: MOAL.helpers.adts.strictlist

__init__(*args)
add(chars)
copy(times)
lo()
reverse()
shuffle()
split()

Take each key and convert its chars into a sub array for each key

up()

MOAL.helpers.datamaker module

MOAL.helpers.datamaker.make_sparselist(vals, total, filler=0)[source]

Generate a sparse list with a given set of values.

Args:
vals (dict) - the list of indices and values,
where the key value represents the index to fill in
total (int) - the total length of the list. A length less than the
length of len(keys.values()) will throw a ValueError
Returns:
sparselist (list) - the sparselist.
MOAL.helpers.datamaker.make_sparsematrix(vals, total, rows=4, cols=4)[source]
MOAL.helpers.datamaker.random_binary_matrix(**kwargs)[source]

Generate a matrix filled with random 0’s or 1’s.

>>> random_binary_matrix(rows=2, columns=2)
[[0, 1], [1, 0]]
MOAL.helpers.datamaker.random_dna(max=4)[source]

Generate a random string of DNA.

Kwargs:
max (int) - The maximum length of the string.
>>> random_dna(max=4)
'TCGA'
MOAL.helpers.datamaker.random_graph(max_edges=10)[source]
MOAL.helpers.datamaker.random_inverse_binary_matrix(**kwargs)[source]

Generate a matrix filled with random 0’s, -1’s or 1’s.

>>> random_binary_matrix(rows=2, columns=2)
[[0, 1, -1], [-1, 1, 0]]
MOAL.helpers.datamaker.random_matrix(rows=4, columns=4, min=1, max=100, choices=None)[source]

Generate a matrix filled with random numbers.

Kwargs:

rows (int) - The number of rows. columns (int) - The number of columns for each row. min (int) - The minimum number to use in randrange max (int) - The maximum number to use in randrange choices (list) - The choices to choose from, if specified.

If none specified, randrange(min, max) will be used.
>>> random_matrix(rows=3, columns=3, min=1, max=2)
[[94, 23, 50], [57, 28, 52], [94, 5, 45]]
MOAL.helpers.datamaker.random_node(vals, nums, max_edges)[source]
MOAL.helpers.datamaker.random_person()[source]
MOAL.helpers.datamaker.random_uniform_matrix(size, **kwargs)[source]
MOAL.helpers.datamaker.subnet_mask()[source]

MOAL.helpers.display module

class MOAL.helpers.display.Section(content)[source]

Provides a context manager for printing ‘sections’ of text. Prints a top and bottom section to visually separate different blocks of code/results.

__enter__()[source]
__exit__(exception_type, exception_value, traceback)[source]
__init__(content)[source]
prnt(prefix, newlines=True)[source]
MOAL.helpers.display._func_or_print(result, func)[source]

Private function to either print or call a function on result

MOAL.helpers.display._heading(title, divider, desc='')[source]
MOAL.helpers.display._make_padded_char(word, padding=5)[source]

Create a string format token with padding based on the length of the given word * padding; e.g. ‘cat’ -> {:<3}

MOAL.helpers.display._uncase_x(string, seperator)[source]

Split by seperator and join the string; e.g. ‘foo_bar’ => foo bar

MOAL.helpers.display.annotate(func, *args, **kwargs)[source]

A decorate to annotate with the function name when it’s called.

MOAL.helpers.display.cmd_title(msg, newlines=True)[source]

Print a command type message (e.g [COMMAND]) in red.

MOAL.helpers.display.divider(atom='_', newline=True)[source]

Print a divider. Optionally override the unit of text to use (e.g —, ...., ###)

MOAL.helpers.display.firstcaps(string)[source]
MOAL.helpers.display.make_padded_chars(words, seperator=' ')[source]

Call _make_padding_char on a list of words. For example, to create a new format string to pad a list of values. (e.g. {:<3} {<:6} {<:9}

MOAL.helpers.display.print_error(msg, prefix='[ERROR]')[source]

Print error-type text in red with a prefix

MOAL.helpers.display.print_h1(title, desc='')[source]

Print a heading with a very bold underline

MOAL.helpers.display.print_h2(title, desc='')[source]

Print a heading with a bold underline

MOAL.helpers.display.print_h3(title, desc='')[source]

Print a heading with a moderate underline

MOAL.helpers.display.print_h4(title, desc='')[source]

Print a heading with a subdued underline

MOAL.helpers.display.print_info(msg, prefix='[INFO]')[source]

Print info-type text in red with a prefix

MOAL.helpers.display.print_nl(title, pos='top')[source]

Print a title with a newline. The newline can be either on bottom (default) or top, if specified.

MOAL.helpers.display.print_simple(words, result, func=<function pprint>, newline=True)[source]

Print a heading with data. The content can optionally be formatted by a given func. No styling is done to the text.

MOAL.helpers.display.print_subdued(msg)[source]

Print info-type text in dark.

MOAL.helpers.display.print_success(msg, prefix='[YAY]')[source]

Print success-type text in red with a prefix

MOAL.helpers.display.print_table(rows, formatter=None, striped=True)[source]

Print a table that is uniform and aligned.

MOAL.helpers.display.print_vars(vars, upper=False, convert=False)[source]

Single line print with variable and a title, as well as some optional kwargs to transform the data.

MOAL.helpers.display.print_warning(msg, prefix='[WARN]')[source]

Print warning-type text in red with a prefix

MOAL.helpers.display.prnt(title, result, func=None, newlines=False)[source]

A more useful default print function for titles and accompanying content. Shows stylized title, with content below, and newlines. The content can optionally be formatted by a given func.

MOAL.helpers.display.title_case(string, seperator='_')[source]

Format a string into title case - e.g. ‘some_word’ => Some Word

MOAL.helpers.display.uncase_hyphen(string)[source]
MOAL.helpers.display.uncase_period(string)[source]
MOAL.helpers.display.uncase_snake(string)[source]
MOAL.helpers.display.uncase_snake_upper(string)[source]

MOAL.helpers.generic module

MOAL.helpers.generic.interleave(*lists)[source]

Interleave N lists into each other; e.g. [1, 2], [‘A’ ‘B’] => [1, ‘A’, 2, ‘B’]

MOAL.helpers.generic.powerset_list(main_list, side='left')[source]

Return staggered offsets of lists of a given list. e.g. [1, 2, 3] => [[1, 2, 3], [1, 2], [1]] side determines which way to offset: left, or right.

Switching sides would be the same as reversing the list beforehand.

MOAL.helpers.generic.powerset_tree(lst, initial=True, terminator='')[source]

Creates nested powersets of the word list for each key – the result is effectively a suffix tree. terminator is a string value that can be passed in to terminate each parent element in a list – indices can be used to denote the same thing, but it gives a visual option if you prefer.

MOAL.helpers.generic.random_number_set(min_rand=0, max_rand=9999, max_range=100)[source]

Generated a list of random number sets.

MOAL.helpers.generic.random_number_sets(sets=2, max_random=50)[source]

A mutli-valued version of random_number_set

MOAL.helpers.generic.slice_dict(ref_dict, start, end)[source]

Create a subsection (slice) of a dictionary, like a list.

MOAL.helpers.generic.subdivide_groups(items, divisions=2)[source]

Divides a list of items up into subdivisions based on divisions. For example, [1, 2, 3, 4] with 2 divisions

becomes [[1, 2], [3, 4]]
or, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] with 5 divisions
becomes [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]].

Numbers that cannot be distributed equally will have the remainder added to the last group.

MOAL.helpers.generic.substring_dict(string)[source]

Return all substrings of a given string as a dict.

MOAL.helpers.generic.substring_list(string)[source]

Return all substrings of a given string as a list.

MOAL.helpers.generic.swap_item(items, a, b)[source]

Swap an item a with b, in a list. Used primarily for sorting algorithms.

MOAL.helpers.generic.take_choice(options, maximum)[source]

Return an array of random choices. Choices are seeded from options from 0 until maximum.

MOAL.helpers.text module

MOAL.helpers.text._rand_bitstring(count)[source]
MOAL.helpers.text.gibberish(length=10)[source]
MOAL.helpers.text.gibberish2(length=3)[source]

Returns somewhat normal looking gibberish

MOAL.helpers.text.gibberish3(length=3)[source]

Returns somewhat normal looking gibberish

MOAL.helpers.text.randchars(c)[source]
MOAL.helpers.text.random_binary(bits)[source]
MOAL.helpers.text.random_byte()[source]
MOAL.helpers.text.random_nibble()[source]
MOAL.helpers.text.uniqchars(count)[source]
MOAL.helpers.text.words_unix_dict(min_length=8)[source]

MOAL.helpers.trials module

MOAL.helpers.trials.run_sorting_trials(sorting_func, magnitudes=[10, 100, 1000], test_output=True)[source]

Runs a bunch of trials of various magnitudes with a given func, using randomly generated numbers. Returns a dict of results for later inspection. Tailored specifically for sorting functions, by generating randomly mixed sequences of numbers.

MOAL.helpers.trials.run_trials(func, trials=3)[source]

Runs a set of trials given a function and trial number. The func in question is expected to use the test_speed decorator to do the actual profiling.

MOAL.helpers.trials.test_speed(func, *args, **kwargs)[source]

Decorator that wraps a function and provides a timer + results output for execution profiling.