util - Utilities

pyshell.util.force_dir_path(path)[source]

Force the input path to be a directory.

Paths are normalized, and then it is ensured that the path ends with a /.

Converts /some//path/here to /some/path/here/ and /some//path/here// to /some/path/here/

pyshell.util.collapseuser(path)[source]

Collapse the username from a path.

pyshell.util.check_exists(path)[source]

Check whether the given directory exists.

pyshell.util.warn_exists(path, name=u'path', exists=True)[source]

Warn if the file path does or does not exist.

Parameters:
  • path – The path to search for.
  • name – The textual name to use in output.
  • exists – Whether the filepath should exist or not.
pyshell.util.is_remote_path(path)[source]

Path looks like an SSH or other URL compatible path?

pyshell.util.semiabstractmethod(txt)[source]

Convert semi-abstract-methods into raisers for NotImplementedErrors

>>> @semiabstractmethod
... def myfunc():
...     print "Inside myfunc"
>>> myfunc()
NotImplementedError
pyshell.util.deprecatedmethod(message=None, version=None, replacement=None)[source]

Mark a method as deprecated.

Parameters:
  • message – The warning message to display, or None.
  • version – The deprication version. Will be appended to the message.
  • replacement – A string describing the text to replace.

The final DepricationWarning message is formatted as follows:

"Method {method} will be depricated in version {version}, please use {replacement} instead."

Setting the message argument replaces the string "Method {method} will be depricated".

pyshell.util.func_lineno(func)[source]

Get the line number of a function. First looks for compat_co_firstlineno, then func_code.co_first_lineno.

pyshell.util.ipydb()[source]

Try to use the iPython debugger on program failures.

pyshell.util.is_type_factory(ttype)[source]

Return a function which checks if an object can be cast as a given type. Basic usage allows for checking string-casting to a specific type.

Parameters:ttype – Usually a type but really, any function which takes one argument and which will raise a ValueError if that one argument can’t be cast correctly.
pyshell.util.query_yes_no(question, default=u'yes')[source]

Ask a yes/no question via raw_input() and return their answer.

Parameters:
  • question – A string presented to the user
  • default – The answer if the user hits <Enter> with no input. It must be “yes” (the default), “no” or None (meaning an answer is required of the user).
  • output – The output stream.
Returns:

True or False for “yes” or “no” respectively.

pyshell.util.query_string(question, default=None, validate=None, output=<open file '<stdout>', mode 'w' at 0x104f5b150>)[source]

Ask a question via raw_input, and return the answer as a string.

Parameters:
  • question – A string presented to the user
  • default – The answer if the user hits <Enter> with no input.
  • validate – A function to validate responses. To validate that the answer is a specific type, use is_type_factory().
  • output – The output stream.
Returns:

A string with the user’s answer or the default if no answer.

pyshell.util.query_select(iterable, labels=None, default=None, before=u'Select from:', question=u'Select an item', output=<open file '<stdout>', mode 'w' at 0x104f5b150>)[source]

A simple CLI UI to let the user choose an item from a list of items.

Parameters:
  • iterable – The list from which to choose.
  • labels – The labels for the list from which to choose.
  • default – The index of the default item in iterable
  • before – The string to print before the choice list.
  • question – The question to use as the prompt.
  • output – The output stream.

Previous topic

loggers - Logging made Easy

Next topic

postmortem – A postmortem debugging tool for nosetests.

This Page