views:

75

answers:

3

This code that's in the doctest works when run by itself, but in this doctest it fails in 10 places. I can't figure out why it does though. The following is the entire module:

class requireparams(object):
    """
    >>> @requireparams(['name', 'pass', 'code'])
    >>> def complex_function(params):
    >>>     print(params['name'])
    >>>     print(params['pass'])
    >>>     print(params['code'])
    >>> 
    >>> params = {
    >>>     'name': 'John Doe',
    >>>     'pass': 'OpenSesame',
    >>>     #'code': '1134',
    >>> }
    >>> 
    >>> complex_function(params)
    Traceback (most recent call last):
        ...
    ValueError: Missing from "params" argument: code
    """
    def __init__(self, required):
        self.required = set(required)

    def __call__(self, params):
        def wrapper(params):
            missing = self.required.difference(params)
            if missing:
                raise ValueError('Missing from "params" argument: %s' % ', '.join(sorted(missing)))
        return wrapper

if __name__ == "__main__":
    import doctest
    doctest.testmod()
+4  A: 

doctest requires that you use ... for continuation lines:

>>> @requireparams(['name', 'pass', 'code'])
... def complex_function(params):
...     print(params['name'])
...     print(params['pass'])
...     print(params['code'])
...
>>> params = {
...     'name': 'John Doe',
...     'pass': 'OpenSesame',
...     #'code': '1134',
... }
...
>>> complex_function(params)
Greg Hewgill
Perfect works like magic! Thanks.
orokusaki
+1  A: 

Try pasting the code exactly from the python prompt. That means it will contain some ... as well as >>>. Otherwise the doctest parser won't know when there's a multiline expression.

On preview: What Greg said.

Tobu
A: 

Here's my module after I corrected it (it works now):

class requiresparams(object):
    """

    Used as a decorator with an iterable passed in, this will look for each item
    in the iterable given as a key in the params argument of the function being
    decorated. It was built for a series of PayPal methods that require
    different params, and AOP was the best way to handle it while staying DRY.


    >>> @requiresparams(['name', 'pass', 'code'])
    ... def complex_function(params):
    ...     print(params['name'])
    ...     print(params['pass'])
    ...     print(params['code'])
    >>> 
    >>> params = {
    ...     'name': 'John Doe',
    ...     'pass': 'OpenSesame',
    ...     #'code': '1134',
    ... }
    >>> 
    >>> complex_function(params)
    Traceback (most recent call last):
        ...
    ValueError: Missing from "params" argument: code
    """
    def __init__(self, required):
        self.required = set(required)

    def __call__(self, params):
        def wrapper(params):
            missing = self.required.difference(params)
            if missing:
                raise ValueError('Missing from "params" argument: %s' % ', '.join(sorted(missing)))
        return wrapper

if __name__ == "__main__":
    import doctest
    doctest.testmod()
orokusaki