nose: nose.util
Utility functions and classes used by nose internally.
Classes
Highlighted methods are defined in this class.
Simple ordered dict implementation, based on:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/107747
Methods
x.__cmp__(y) <==> cmp(x,y)
D.__contains__(k) -> True if D has a key k, else False
x.__eq__(y) <==> x==y
x.__ge__(y) <==> x>=y
x.__getitem__(y) <==> x[y]
x.__gt__(y) <==> x>y
x.__iter__() <==> iter(x)
x.__le__(y) <==> x<=y
x.__len__() <==> len(x)
x.__lt__(y) <==> x<y
x.__ne__(y) <==> x!=y
dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v. v defaults to None.
D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.
D.has_key(k) -> True if D has a key k, else False
D.iteritems() -> an iterator over the (key, value) items of D
D.iterkeys() -> an iterator over the keys of D
D.itervalues() -> an iterator over the values of D
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
D.popitem() -> (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty
Functions
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.
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']
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 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.
Get the line number of a function. First looks for compat_co_firstlineno, then func_code.co_first_lineno.
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']
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.
Draw a 70-char-wide divider, with label in the middle.
>>> ln('hello there')
'---------------------------- hello there -----------------------------'
Return absolute, normalized path to directory, if it exists; None otherwise.
Find the test address for a test, which may be a module, filename, class, method or function.
Find the python source file for a package, relative to a particular directory (defaults to current working directory if not given).
Is obj a class? inspect's isclass is too liberal and returns True for objects that can't be subclasses of anything.
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...>
Find the python source file for a .pyc or .pyo file. Returns the filename provided if it is not a python source file.
Is this path a package directory?
>>> ispackage('nose')
True
>>> ispackage('unit_tests')
False
>>> ispackage('nose/plugins')
True
>>> ispackage('nose/loader.py')
False
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'
Compare functions by their line numbers.
>>> cmp_lineno(isgenerator, ispackage)
-1
>>> cmp_lineno(ispackage, isgenerator)
1
>>> cmp_lineno(isgenerator, isgenerator)
0
Attributes
Default value: <logging.Logger instance>
Default value: (<type 'classobj'>, <type 'type'>)
Default value: 32
Default value: <_sre.SRE_Pattern object>