nose: nose.util

Utility functions and classes used by nose internally.

Classes

Highlighted methods are defined in this class.

odict (dict)

Simple ordered dict implementation, based on:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/107747

Methods

__cmp__(...)(inherited from dict)

x.__cmp__(y) <==> cmp(x,y)

__contains__(...)(inherited from dict)

D.__contains__(k) -> True if D has a key k, else False

__delitem__(self, key)
__eq__(...)(inherited from dict)

x.__eq__(y) <==> x==y

__ge__(...)(inherited from dict)

x.__ge__(y) <==> x>=y

__getitem__(...)(inherited from dict)

x.__getitem__(y) <==> x[y]

__gt__(...)(inherited from dict)

x.__gt__(y) <==> x>y

__init__(self, *arg, **kw)
__iter__(...)(inherited from dict)

x.__iter__() <==> iter(x)

__le__(...)(inherited from dict)

x.__le__(y) <==> x<=y

__len__(...)(inherited from dict)

x.__len__() <==> len(x)

__lt__(...)(inherited from dict)

x.__lt__(y) <==> x<y

__ne__(...)(inherited from dict)

x.__ne__(y) <==> x!=y

__setitem__(self, key, item)
clear(self)
copy(self)
fromkeys(...)(inherited from dict)

dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v. v defaults to None.

get(...)(inherited from dict)

D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.

has_key(...)(inherited from dict)

D.has_key(k) -> True if D has a key k, else False

items(self)
iteritems(...)(inherited from dict)

D.iteritems() -> an iterator over the (key, value) items of D

iterkeys(...)(inherited from dict)

D.iterkeys() -> an iterator over the keys of D

itervalues(...)(inherited from dict)

D.itervalues() -> an iterator over the values of D

keys(self)
pop(...)(inherited from dict)

D.pop(k[,d]) -> v, remove specified key and return the corresponding value If key is not found, d is returned if given, otherwise KeyError is raised

popitem(...)(inherited from dict)

D.popitem() -> (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty

setdefault(self, key, failobj=None)
update(self, dict)
values(self)

Functions

absfile(path, where=None)

Return absolute, normalized path to file (optionally in directory where), or None if the file can't be found either in where or the current working directory.

tolist(val)

Convert a value that may be a list or a (possibly comma-separated) string into a list. The exception: None is returned as None, not [None].

>>> tolist(["one", "two"])
['one', 'two']
>>> tolist("hello")
['hello']
>>> tolist("separate,values, with, commas,  spaces , are    ,ok")
['separate', 'values', 'with', 'commas', 'spaces', 'are', 'ok']
ls_tree(dir_path='', skip_pattern='(?:\\.svn)|(?:[^.]+\\.py[co])|(?:.*~)|(?:.*\\$py\\.class)', indent='|-- ', branch_indent='| ', last_indent='`-- ', last_branch_indent=' ')
try_run(obj, names)

Given a list of possible method names, try to run them with the provided object. Keep going until something works. Used to run setup/teardown methods for module, package, and function tests.

split_test_name(test)

Split a test name into a 3-tuple containing file, module, and callable names, any of which (but not all) may be blank.

Test names are in the form:

file_or_module:callable

Either side of the : may be dotted. To change the splitting behavior, you can alter nose.util.split_test_re.

func_lineno(func)

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

match_last(a, b, regex)

Sort compare function that puts items that match a regular expression last.

>>> from nose.config import Config
>>> c = Config()
>>> regex = c.testMatch
>>> entries = ['.', '..', 'a_test', 'src', 'lib', 'test', 'foo.py']
>>> entries.sort(lambda a, b: match_last(a, b, regex))
>>> entries
['.', '..', 'foo.py', 'lib', 'src', 'a_test', 'test']
file_like(name)

A name is file-like if it is a path that exists, or it has a directory part, or it ends in .py, or it isn't a legal python identifier.

ln(label)

Draw a 70-char-wide divider, with label in the middle.

>>> ln('hello there')
'---------------------------- hello there -----------------------------'
is_generator(func)
absdir(path)

Return absolute, normalized path to directory, if it exists; None otherwise.

isgenerator(func)
anyp(predicate, iterable)
test_address(test)

Find the test address for a test, which may be a module, filename, class, method or function.

getfilename(package, relativeTo=None)

Find the python source file for a package, relative to a particular directory (defaults to current working directory if not given).

isclass(obj)

Is obj a class? inspect's isclass is too liberal and returns True for objects that can't be subclasses of anything.

resolve_name(name, module=None)

Resolve a dotted name to a module and its parts. This is stolen wholesale from unittest.TestLoader.loadTestByName.

>>> resolve_name('nose.util') #doctest: +ELLIPSIS
<module 'nose.util' from...>
>>> resolve_name('nose.util.resolve_name') #doctest: +ELLIPSIS
<function resolve_name at...>
src(filename)

Find the python source file for a .pyc, .pyo or $py.class file on jython. Returns the filename provided if it is not a python source file.

transplant_class(cls, module)

Make a class appear to reside in module, rather than the module in which it is actually defined.

>>> from nose.failure import Failure
>>> Failure.__module__
'nose.failure'
>>> Nf = transplant_class(Failure, __name__)
>>> Nf.__module__
'nose.util'
>>> Nf.__name__
'Failure'
ispackage(path)

Is this path a package directory?

>>> ispackage('nose')
True
>>> ispackage('unit_tests')
False
>>> ispackage('nose/plugins')
True
>>> ispackage('nose/loader.py')
False
getpackage(filename)

Find the full dotted package name for a given python source file name. Returns None if the file is not a python source file.

>>> getpackage('foo.py')
'foo'
>>> getpackage('biff/baf.py')
'baf'
>>> getpackage('nose/util.py')
'nose.util'

Works for directories too.

>>> getpackage('nose')
'nose'
>>> getpackage('nose/plugins')
'nose.plugins'

And __init__ files stuck onto directories

>>> getpackage('nose/plugins/__init__.py')
'nose.plugins'

Absolute paths also work.

>>> path = os.path.abspath(os.path.join('nose', 'plugins'))
>>> getpackage(path)
'nose.plugins'
cmp_lineno(a, b)

Compare functions by their line numbers.

>>> cmp_lineno(isgenerator, ispackage)
-1
>>> cmp_lineno(ispackage, isgenerator)
1
>>> cmp_lineno(isgenerator, isgenerator)
0
transplant_func(func, module)

Make a function imported from module A appear as if it is located in module B.

>>> from pprint import pprint
>>> pprint.__module__
'pprint'
>>> pp = transplant_func(pprint, __name__)
>>> pp.__module__
'nose.util'

The original function is not modified

>>> pprint.__module__
'pprint'

Calling the transplanted function calls the original.

>>> pp([1, 2])
[1, 2]
>>> pprint([1,2])
[1, 2]

Attributes

log
Default value: <logging.Logger instance>
skip_pattern
Default value: (?:\.svn)|(?:[^.]+\.py[co])|(?:.*~)|(?:.*\$py\.class)
class_types
Default value: (<type 'classobj'>, <type 'type'>)
CO_GENERATOR
Default value: 32
ident_re
Default value: <_sre.SRE_Pattern object>