473,320 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Doctests for nested functions

Recently I have posted this same question on it.comp.lang.python, maybe
there aren't solutions, but I'd like to know what you think.

Can doctests be added to nested functions too? (This can be useful to
me, I use nested function when I don't have attributes that I have to
remember, but I want to split the logic in some subparts anyway).
Example:

def foo():
"""
>>foo()
21
"""
def bar():
"""
>>bar()
11
"""
print 10
print 20
import doctest
doctest.testmod()

Bye,
bearophile

Sep 23 '06 #1
2 1657
In article <11**********************@i42g2000cwa.googlegroups .com>,
be************@lycos.com wrote:
Can doctests be added to nested functions too? (This can be useful to
me, I use nested function when I don't have attributes that I have to
remember, but I want to split the logic in some subparts anyway).
I think we had that discussion before, but that's not what nested
functions are for (in Python). Use modules for that. Also solves your
doctest problem nicely.

Just
Sep 23 '06 #2
Just wrote:
In article <11**********************@i42g2000cwa.googlegroups .com>,
be************@lycos.com wrote:
Can doctests be added to nested functions too? (This can be useful to
me, I use nested function when I don't have attributes that I have to
remember, but I want to split the logic in some subparts anyway).

I think we had that discussion before, but that's not what nested
functions are for (in Python). Use modules for that. Also solves your
doctest problem nicely.

Just
Just is right.
But...
I struggled with coming up with something. The problem is that bar does
not exist in the right form unlee you are executing the function so:

def foo(_test = False):
r"""
>>foo()
21
>>foo(_test = True)
20
"""
def bar():
"""
>>bar()
11
"""
print 10
print 20

if _test:
_locals = locals()

import doctest

g = globals().copy()
g.update(_locals)
g['__test__'] = {}

_totest = {}
for loc in _locals.values():
try:
if loc.__doc__ and loc.__name__ and (loc.__name__
not in g['__test__']):
_totest[loc.__name__] = loc
except:
pass
for _name, _testing in sorted(_totest.items()):
doctest.run_docstring_examples(_testing, g,
name = "foo:" + _name)

import doctest
doctest.testmod()
The output is:

Trying:
foo()
Expecting:
21
************************************************** ********************
File "__main__", line 3, in __main__.foo
Failed example:
foo()
Expected:
21
Got:
20
Trying:
foo(_test = True)
Expecting:
20
************************************************** ********************
File "__main__", line 5, in __main__.foo
Failed example:
foo(_test = True)
Expected:
20
Got:
20

************************************************** ********************
File "__main__", line 10, in foo:bar
Failed example:
bar()
Expected:
11
Got:
10
1 items had no tests:
__main__
************************************************** ********************
1 items had failures:
2 of 2 in __main__.foo
2 tests in 2 items.
0 passed and 2 failed.
***Test Failed*** 2 failures.
*** DocTestRunner.merge: '__main__.foo' in both testers; summing
outcomes.
*** DocTestRunner.merge: '__main__' in both testers; summing outcomes.
- Paddy.
(but use a module instead)!

Sep 23 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Andy Baker | last post by:
Hi there, I'm learning Python at the moment and trying to grok the thinking behind it's scoping and nesting rules. I was googling for nested functions and found this Guido quote:...
6
by: A | last post by:
Hi, How do you make use of nested functions in C++? I realize in C++ that everything must be declared first in a header file before implementation in a .cpp file. I tried to nest a method...
7
by: block111 | last post by:
Hello, code like this: int f1(int x){ int f2(int y){ return y*y; } if(x > 0) return f2(x);
10
by: nimmi_srivastav | last post by:
Below you will see an example of a nested conditional expression that this colleague of mine loves. He claims that it is more efficient that a multi-level if-else-if structure. Moreover, our...
9
by: Gregory Petrosyan | last post by:
I often make helper functions nested, like this: def f(): def helper(): ... ... is it a good practice or not? What about performance of such constructs?
2
by: Brian van den Broek | last post by:
Hi all, I have a module of classes for getting input from the user that satisfies various constraints. For instance, one class is created with a menu of option, presents them to the user, and...
4
by: Wolfgang Draxinger | last post by:
If you know languages like Python or D you know, that nested functions can be really handy. Though some compilers (looking at GCC) provide the extension of nested functions, I wonder, how one...
2
by: Johannes Bauer | last post by:
Nick Keighley schrieb: Why is there actually a *need* for nested functions? If functionality of subfunctions which are only locally visible is desired, why not put the nesting function parent...
9
by: Gabriel Rossetti | last post by:
Hello, I can't get getattr() to return nested functions, I tried this : .... def titi(): .... pass .... f = getattr(toto, "titi") .... print str(f) .... Traceback...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.