minsci package

Subpackages

Submodules

minsci.exceptions module

Custom exceptions for minsci module

exception minsci.exceptions.MinSciException[source]

Bases: exceptions.Exception

Root exception. Used only to except any error, never raised.

exception minsci.exceptions.PathError[source]

Bases: minsci.exceptions.MinSciException

Called when pull request on DeepDict fails to resolve

exception minsci.exceptions.RowMismatch[source]

Bases: minsci.exceptions.MinSciException

Called when columns in table have different numbers of rows

exception minsci.exceptions.TaxonNotFound[source]

Bases: minsci.exceptions.MinSciException

Called when columns in table have different numbers of rows

minsci.helpers module

Helper functions used throughout the minsci module

minsci.helpers.add_article(val)[source]

Prepend the appropriate indefinite article to a string

Parameters:val (str) – string to which to add a/an
Returns:String with indefinite article prepended
minsci.helpers.base2int(i, base)[source]

Converts integer in specified base to base 10

minsci.helpers.catnum_keyer(catnum)[source]

Create sortable key for a catalog number by zero-padding each component

Parameters:catnum (str or dict) – the catalog number to key
Returns:Sortable catalog number
minsci.helpers.cprint(obj, show=True)[source]

Conditionally pretty print an object

Parameters:
  • obj (mixed) – the object to print
  • show (bool) – print the object if true
minsci.helpers.dedupe(lst, lower=True)[source]

Dedupes a list while maintaining order and case

Parameters:list (list) – a list of strings
Returns:Deduplicated copy of the original list
minsci.helpers.dict_from_odbc(cursor, tbl, row_id=None, cols=None, where=None, encoding='cp1252')[source]

Creates a :param cursor: :type cursor: pyodbc.Cursor :param tbl: name of table to query. For Excel, table name must be

formatted as [tbl$].
Parameters:
  • row_id (list) – name of field(s) to use as key for dictionary
  • col (list) – list of columns to return. If None, will return all.
  • where (str) – formatted where clause
  • encoding (str) – encoding of source file
Returns:

Dictionary keyed to row_id

minsci.helpers.format_catnum(parsed, code=True, div=False)[source]

Formats parsed catalog number to a string

Parameters:
  • parsed (dict) – parsed catalog number
  • code (bool) – include museum code in catnum if True
  • div (bool) – include div abbreviation in catnum if True
Returns:

Catalog number formatted as a string, like ‘G3551-00’. Use format_catnums to process a list of parsed catalog numbers.

minsci.helpers.format_catnums(parsed, code=True, div=False)[source]

Converts a list of parsed catalog numbers into strings

Parameters:
  • parsed (list) – list of dicts containing parsed catnums
  • code (bool) – include museum code in catnum if True
  • div (bool) – include div abbreviation in catnum if True
Returns:

[‘G3551-00’]

Return type:

List of catalog numbers formatted as strings

minsci.helpers.fxrange(start, stop, step)[source]

Mimics functionality of xrange for floats

From http://stackoverflow.com/questions/477486/

Parameters:
  • start (int or float) – first value in range (inclusive)
  • stop (int or float) – last value in range (exclusive)
  • step (float) – value by which to increment start
minsci.helpers.init_odbc(fn)[source]

Opens ODBC connection based on database type

Parameters:fn (string) – filename (or path)
Returns:pyodbc.Connection object
minsci.helpers.int2base(i, base)[source]

Converts base 10 integer to specified base

minsci.helpers.lcfirst(val)[source]

Lowercase first letter of string while leaving the rest alone

Parameters:val (str) – string to capitalize
Returns:Capitalized string
minsci.helpers.localize_datetime(timestamp, timezone_id='US/Eastern', mask='%Y-%m-%dT%H:%M:%S')[source]

Loclize timestamp to specified timezone

Returns:Localize datetime as string formatted according to the mask
minsci.helpers.oxford_comma(lst, lowercase=False)[source]

Formats list as comma-delimited string

Parameters:
  • lst (list) – list of strings
  • lowercase (bool) – if true, convert the first letter in each value in the list to lowercase
Returns:

Comma-delimited string

minsci.helpers.parse_catnum(val, attrs=None, default_suffix='', min_suffix_length=0, strip_suffix=False, prefixed_only=False)[source]

Find and parse catalog numbers in a string

Parameters:
  • s (str) – string containing catalog numbers or range
  • attrs (dict) – additional parameters keyed to EMu field
  • default_suffix (str) – default suffix to add if none present
  • strip_suffx (bool) – strip leading zeroes from suffix if True
  • prefixed_only (bool) – find only those catalog numbers that are prefixed by a valid museum code (NMNH or USNM)
Returns:

List of dicts containing catalog numbers parsed into prefix, number, and suffix: {‘CatPrefix’: ‘G’, ‘CatNumber’: ‘3551’, ‘CatSuffix’: 00}. Pass to format_catnums to convert to strings.

minsci.helpers.parse_catnums(vals, **kwargs)[source]

Parse a list of strings containing catalog numbers

See parse_catnums() for a description of the available arguments.

Returns:A list of parsed catnums
minsci.helpers.parse_names(name_string, last_name_first=False)[source]

Parses name strings into components using nameparser

minsci.helpers.plural(val)[source]

Converts string to plural

Parameters:s (str) – a string
Returns:The plural form of the original string
minsci.helpers.prompt(text, validator, confirm=False, helptext='No help text provided', errortext='Invalid response!')[source]

Prompts for and validates user input

Parameters:
  • text (str) – the prompt to present to the user
  • validator (mixed) – the dict, list, or string used to validate the repsonse
  • confirm (bool) – if true, user will be prompted to confirm value
  • helptext (str) – text to show if user response is “?”
  • errortext (str) – text to return if user response does not validate
Returns:

Validated response to prompt

minsci.helpers.read_file(path, success, error=None)[source]

Process file at given path using success callback

minsci.helpers.read_unicode_text(fp, encoding='utf-16', skiplines=0)[source]

Read a unicode text file generated by Excel

minsci.helpers.rprint(obj, show=True)[source]

Pretty print object, then pause execution

Parameters:
  • obj (mixed) – the object to print
  • show (bool) – print the object if true
minsci.helpers.singular(val)[source]

Converts string to singular

Parameters:s (str) – a string
Returns:The singular form of the original string
minsci.helpers.sort_by_reference(lst, order)[source]

Reorder list to match order of another list

minsci.helpers.sort_catnums(catnums)[source]

Sort a list of catalog numbers

Parameters:catnums (list) – list of catalog numbers, either as strings or parsed into dicts
Returns:Sorted list of catalog numebrs. Catalog numbers are formatted in the same way as they were in the original list.
minsci.helpers.std(val, aggressive=False)[source]

Standardizes the given value for string comparisons

Parameters:aggressive (bool) – if True, strips everything except letters and numbers
Returns:Standardized value as unicode
minsci.helpers.ucfirst(val)[source]

Capitalize first letter of string while leaving the rest alone

Parameters:val (str) – string to capitalize
Returns:Capitalized string
minsci.helpers.utflatten(val)[source]

Converts diacritcs in string to their to an ascii equivalents

Modified to use the unidecode module, but kept alias so older scripts will still work.

Module contents

Provides helper functions used throughout the minsci module