uw package

Subpackages

Submodules

uw.color module

Color-manipulating utility routines.

Convenient functions for converting between RGB and HSV color spaces, formatting colors for use in HTML/CSS, and generating a series of (relatively) contrasting hues.

uw.color.hsv2rgb(hsv)[source]

Convert HSV color to RGB.

Parameters

hsv – a 3-tuple containing the H, S, and V values.

Returns

the RGB representation of the color as a 3-tuple.

uw.color.html_row(i, hsv)[source]

Create a table row to demonstrate a hue.

Parameters
  • i (int) – the number of the row within the table, for inclusion in the first column.

  • hsv – the color to demonstrate.

Returns

an HTML <tr> element showing the color, as well as related colors with 70% and 40% of the saturation of the given color.

For use by html_sample().

uw.color.html_sample(n)[source]

Create a sample HTML page showing hues generated by hues().

Parameters

n (int) – the number of hues to generate.

Returns

an HTML document containing a table with one row per hue.

uw.color.hues(start=0)[source]

Generate a sequence of hues intended to be contrasting.

Parameters

start – the starting point in the sequence of colors.

Returns

a generator which returns successive hue values.

The hues start out with red (assuming the default start value is used) and rotate around the spectrum spaced out so that the set of hues returned up to any point is evenly distributed according to hue values. No attempt is made to optimize the distribution for human color perception, i.e., filling in some parts of the spectrum more or less densely to improve the human-visible contrast.

uw.color.rgb2hsv(rgb)[source]

Convert RGB color to HSV.

Parameters

rgb – a 3-tuple containing the R, G, and B values.

Returns

the HSV representation of the color as a 3-tuple.

uw.color.rgb2html(rgb)[source]

Write an RGB color in HTML representation.

Parameters

rgb – a 3-tuple containing the R, G, and B values.

Returns

HTML/CSS attribute value representing the color.

uw.dbtools module

General-purpose utility routines related to the database.

This module is for general-purpose utility routines that relate to the Postgres database.

uw.dbtools.copy_to_csv(cursor, query, header=True)[source]

Run a query and return the result as a CSV document.

Parameters
  • cursor – DB connection cursor.

  • query – SQL query to run.

uw.emailutils module

Email-related utilities.

Contains a class that provides a simple extension of some Python standard email.utils routines.

class uw.emailutils.emailaddr[source]

Bases: tuple

Simple class to represent an email address.

Parameters
  • realname – the realname to be displayed with the email address, or empty for no real name.

  • email – the email address itself.

Instances are always 2-tuples. The two elements are the realname and address. This mostly just wraps procedures in email.utils.

property email

The email address itself.

static parse(fieldvalues)[source]

Parse email addresses using email.utils.getaddresses().

static parse1(address)[source]

Parse a single email address using email.utils.parseaddr().

property realname

The realname associated with the email address.

property valid

Perform a simple check for validity.

Runs this address through email.utils.getaddresses() and verifies that the result is a single address whose address component contains an ‘@’.

uw.stringtools module

String-handling utilities.

Contains a procedure to facilitate multiple string substitutions, and a slight enhancement of the Python-standard str.strip().

uw.stringtools.multiReplace(changes)[source]

Create a procedure to perform multiple string substitutions.

Parameters

changes (dict) – dictionary of substitutions; keys will be replaced by values.

The returned procedure expects a string and returns a string. The result string consists of the input string with the specified substitutions performed.

The changes are a dictionary of find/replace values. The “find” values (dictionary keys) are simultaneously found and each occurrence is replaced by the corresponding “replace” value (dictionary value).

Works by constructing a regular expression corresponding to the alternation of the keys of the dictionary. In the event that “find” strings overlap, behaviour is determined by the re library.

A special case allows for correct handling of the “empty dictionary” case of no replacements.

uw.stringtools.normalize_table_whitespace(grid)[source]

Normalize whitespace for a 2 dimensional array of strings.

Parameters

grid – An iterable of lists of strings.

Returns

An iterator of lists of strings.

Apply strip_string() to each element of the provided 2D array of strings; also use None values to extend each element of the outer array to the length of the longest element.

uw.stringtools.parse_tabular_file(columns, request, header_skip=0, line_field=None)[source]

Parse a file of tabular data according to the specified columns.

Parameters
  • columns (list) – A list of column specifications. Each one must be a 3-tuple consisting of the heading in the file which will identify the column, the fieldname in the result of the parse, and a parser to process values from the column.

  • request – A 2D array of values representing the input. Each value is either a string or None; None values will not be parsed but just passed through as None to the output.

  • header_skip (int) – The number of initial header lines to skip.

  • line_field (str) – The field name in the output to set to the line number, or None for no such field name.

Returns

A 2-tuple of result rows and errors.

The first non-skipped line is interpreted as column headings. These are matched with the first elements of the provided columns to identify which columns of the file will be parsed. Additional columns not given in columns are permitted, but all columns given in columns must be present in the headings line.

The remaining rows are parsed. Each element that corresponds to one of the provided columns is parsed using the corresponding parser. Each row becomes a namedtuple.

uw.stringtools.split_upload_text(text)[source]

Split field content text into a rectangular array of field values.

Parameters

text (str) – The field content text.

Returns

A list of lists of field values.

The field content is split into lines. Then each line is divided either on tabs (if every line has a tab), or according to CSV rules (otherwise). Finally, the result is normalized using normalize_table_whitespace().

uw.stringtools.strip_string(s)[source]

Trim whitespace off a string, and recognize empty strings.

Parameters

s (str) – the string to strip.

This is intended for processing text form fields. It strips whitespace off the ends of the string; if an empty string is all that remains, None is returned. None is also returned if s is None.

Module contents

Top-level package for UW-written Python code.

This is the top-level package for locally-written Python code.

Code that is actually specific to UW applications belongs in uw.local, while code that could be useful outside of UW and is designed without local assumptions goes outside of uw.local.