Utility functions

Utility functions and classes used by nose internally.

nose.util.absdir(path)

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.

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

nose.util.func_lineno(func)

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

nose.util.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'
nose.util.isclass(obj)

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

nose.util.ispackage(path)

Is this path a package directory?

>>> ispackage('nose')
True
>>> ispackage('unit_tests')
False
>>> ispackage('nose/plugins')
True
>>> ispackage('nose/loader.py')
False
nose.util.isproperty(obj)

Is this a property?

>>> class Foo:
...     def got(self):
...         return 2
...     def get(self):
...         return 1
...     get = property(get)
>>> isproperty(Foo.got)
False
>>> isproperty(Foo.get)
True
nose.util.ln(label)

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:

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

nose.util.regex_last_key(regex)

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

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

nose.util.test_address(test)

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

nose.util.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']
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__
'nose.failure'
>>> Nf = transplant_class(Failure, __name__)
>>> Nf.__module__
'nose.util'
>>> Nf.__name__
'Failure'
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__
'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]
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.