Utility functions

Utility functions and classes used by nose internally.


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

nose.util.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.


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.


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

nose.util.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).


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')
>>> getpackage('biff/baf.py')
>>> getpackage('nose/util.py')

Works for directories too.

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

And __init__ files stuck onto directories

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

Absolute paths also work.

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

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


Is this path a package directory?

>>> ispackage('nose')
>>> ispackage('unit_tests')
>>> ispackage('nose/plugins')
>>> ispackage('nose/loader.py')

Is this a property?

>>> class Foo:
...     def got(self):
...         return 2
...     def get(self):
...         return 1
...     get = property(get)
>>> isproperty(Foo.got)
>>> isproperty(Foo.get)

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

>>> ln('hello there')
'---------------------------- hello there -----------------------------'
class nose.util.odict(*arg, **kw)

Simple ordered dict implementation, based on:



Sort key function factory that puts items that match a regular expression last.

>>> from nose.config import Config
>>> from nose.pyversion import sort_list
>>> c = Config()
>>> regex = c.testMatch
>>> entries = ['.', '..', 'a_test', 'src', 'lib', 'test', 'foo.py']
>>> sort_list(entries, regex_last_key(regex))
>>> entries
['.', '..', 'foo.py', 'lib', 'src', 'a_test', 'test']
nose.util.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') 
<module 'nose.util' from...>
>>> resolve_name('nose.util.resolve_name') 
<function resolve_name at...>

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:


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


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.


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


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")
>>> tolist("separate,values, with, commas,  spaces , are    ,ok")
['separate', 'values', 'with', 'commas', 'spaces', 'are', 'ok']
nose.util.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__
>>> Nf = transplant_class(Failure, __name__)
>>> Nf.__module__
>>> Nf.__name__
nose.util.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__
>>> pp = transplant_func(pprint, __name__)
>>> pp.__module__

The original function is not modified.

>>> pprint.__module__

Calling the transplanted function calls the original.

>>> pp([1, 2])
[1, 2]
>>> pprint([1,2])
[1, 2]
nose.util.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.