Tools#

MACPie aims to standardize a set of Python tools useful for programmers at the MAC in particular, and the data science community in general. The tools are divided into the following various modules:

datetimetools#

Utility functions related to dates and times.

current_datetime_str([fmt, ms, ms_prefix])

Get the current datetime with second precision with default format YYYYMMDD_HHMMSS

datetime_ms(dt)

Return milliseconds of the datetime

reformat_datetime_str(arg[, format])

Reformat a datetime string into specified format.

itertools#

duplicate_indices(iterable)

Make an iterator that returns duplicate items from the seq along with the the indices of that item. ::.

filter_get_index(predicate, iterable)

Make an iterator that yields items of iterable for which predicate returns true, along with the index of that item.

filter_get_message(predicates, iterable[, ...])

Make an iterator from items of iterable for which one or more predicates returns true, along with the index of that item and optional message information.

first_true(iterable[, default, pred])

Returns the first true value in the iterable.

overlay(bottom, top[, predicate, ...])

Overlay elements from top over bottom.

lltools#

chunks(seq[, chunk_size])

Make an iterator returning successive chunks of size chunk_size from seq. ::.

common_members(a, b)

difference(a, b)

Return list of items in a that are not in b, like a - b, while preserving order. ::.

filter_seq(seq[, items, like, regex, pred, ...])

Filter sequence of strings.

filter_seq_pair(left, right[, intersection, ...])

Filter pair of sequences of strings.

is_disjoint(a, b)

Check if two lists are disjoint (i.e. have no element in common). ::.

is_list_like(obj[, allow_sets])

Whether obj is a tuple or list

list_like_str_equal(a, b[, case_sensitive])

Whether list of strings in a is equal to the list of strings in b.

make_list_if_list_like(obj[, allow_sets])

make_same_length(*seqs[, fillvalue])

Make sequences the same length, filling any shorter lists with fillvalue to make them equal-sized.

make_tuple_if_list_like(obj[, allow_sets])

maybe_make_list(obj[, allow_sets])

If obj is not list-like, return as a single item list.

maybe_make_tuple(obj[, allow_sets])

If obj is not list-like, return as a single item list.

move_item_to(l, item, item_to_move_to[, offset])

Move an item in a list to the just before the position of another item. ::.

remove_duplicates(seq[, preserve_order])

Remove duplicates from a sequence while preserving order.

rtrim(seq[, predicate])

Remove trailing elements from sequence as long as predicate is true.

rtrim_longest(*seqs[, predicate, fillvalue])

Remove trailing elements from each sequence as long as predicate is true.

openpyxltools#

autofit_column_width(ws)

Autoadjust the column widths of a Worksheet

file_to_dataframe(filepath[, sheet_name, ...])

get_column_index(ws[, col_header])

Get column index of the col_header, returning -1 if not found

get_sheet_names(filepath_or_buffer)

Get all sheet names from an Excel file.

highlight_row(ws, row[, color])

Highlight row a certain color

is_row_empty(ws, row_index[, delete_if_empty])

Determine if a row is empty.

iter_rows_with_column_value(ws, column, value)

to_tablib_dataset(wb[, sheet_name, headers, ...])

Return a Tablib Dataset from an Excel file.

worksheet_to_dataframe(ws[, num_header, num_idx])

Converts an Excel worksheet to a pandas.DataFrame.

pathtools#

Path utilities

create_dir_with_datetime([dir_name_prefix, ...])

Create a new directory with the current datetime appended.

create_subdir(subdir_name[, where, exists_ok])

Creates a sub directory with the current date/time appended to the directory name.

get_files_from_dir(d)

Get all files only from directory

has_csv_extension(filepath)

Return True if filepath has an extension compatabile with csv files.

has_excel_extension(filepath)

Return True if filepath has an extension compatible with Excel files.

validate_paths(ps[, allowed_path, recurse, ...])

shelltools#

copy_file_same_dir(filepath[, new_file_name])

Copy a file into the same directory, by default appending "_copy" to filename, unless new_file_name is specified.

strtools#

add_suffix(s, suffix[, max_length])

Add a suffix to a string, optionally specifying a maximum string length and giving priority to the suffix if maximum string length is reached. ::.

add_suffixes(s, suffixes[, max_length])

Add a list of suffixes to a string, optionally specifying a maximum string length and giving priority to the suffix if maximum string length is reached.

add_suffixes_with_base(base[, suffixes, ...])

Adds a list of suffixes to a specified base string.

make_unique(seq[, suffs_iter, suffs_prefix, ...])

Make sequence of string elements unique by adding a differentiating suffix.

seq_contains(s, seq[, case_sensitive])

Does sequence contain string.

str_equals(a, b[, case_sensitive])

Are strings equal.

str_startswith(s, prefix[, case_sensitive])

Does string start with a prefix.

strip_suffix(s, suffix)

Remove a suffix from a string.

tablibtools#

Tablib is an format-agnostic tabular dataset library. It allows you to import, export, and manipulate tabular data sets. Advanced features include segregation, dynamic columns, tags & filtering, and seamless format import & export.

MacpieTablibDataset(*args, **kwargs)

Extend tablib.Dataset with extra functionality.

DictLikeTablibDataset(*args, **kwargs)

Tabular representation of basic information using two columns only: a Key column and a Value column, using a macpie.tablibtools.MacpieTablibDataset.

read_excel(filepath[, sheet_name, headers, ...])

validatortools#

validate_bool_kwarg(value, arg_name)

Ensures that argument passed in arg_name is of type bool.

xlsxwritertools#

tlset_sheet(tlset, ws, format_bold, ...[, ...])

Completes given worksheet from given Dataset.

XlsxWriterAutofitColumnsWorksheet()

Simulates AutoFit columns in Excel.