473,804 Members | 3,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Doctests for nested functions

Recently I have posted this same question on it.comp.lang.py thon, 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 1688
In article <11************ **********@i42g 2000cwa.googleg roups.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************ **********@i42g 2000cwa.googleg roups.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(_local s)
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_doc string_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.m erge: '__main__.foo' in both testers; summing
outcomes.
*** DocTestRunner.m erge: '__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
2571
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: (http://www.python.org/search/hypermail/python-1993/0343.html) "This is because nested function definitions don't have access to the local variables of the surrounding block -- only to the globals of the
6
12205
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 prototype in another prototype but seems pointless. Can someone please write a short, simple, and concise skeleton code of how to use nested functions? class Foo {
7
2255
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
3248
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 complexity analyzer tool supposedly does not pick it up. Is it really more efficient? Personally I find this coding style extremely cryptic, misleading and error-prone. I believe that I have removed all traces of proprietary-ness from this coding...
9
2857
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
1968
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 rejects any input other than a menu option; another ensures that the user's input is interpretable as an integer between specified bounds, etc. It is a module I wrote up when in the early stages of learning Python. It's a bit ugly :-) So, I am...
4
2317
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 could implement an equivalent behaviour with plain C (in this case I'm thinking of the language I'm developing, which shall be converted into C for target compilation). So far I didn't touch the topic "nested functions", since I just don't see an...
2
1667
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 and its nested children all in one module, declare the children static - voila. Std-C. Regards, Johannes
9
3993
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 (most recent call last):
0
9584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10337
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3822
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2995
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.