473,327 Members | 1,997 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,327 software developers and data experts.

How can a function know what module it's in?

I've been using docstring to exercise each of my modules, with code
like this:

def _test():
import doctest
doctest.testmod()

if __name__ == "__main__":
_test()
This works great when I execute each module by itself. However, if I
want to call mymodule._test() from somewhere else, it doesn't work,
because doctest.testmod() tests the __main__ module instead of
mymodule. And of course changing the call to this doesn't work either:

doctest.testmod(mymodule)

This actually works fine if I'm importing the module (with the
standard name) somewhere else, but not when I'm executing it directly,
or (I would guess) if the module is imported under a different name.

What I want to express is "doctest THIS module, right here, the one
this code is in!" But I can't find a clean way to do that.

I noticed that a function object has a __module__ attribute, that is a
reference to the module the function is in. But that just pushes the
problem back a step: how can a function get a reference to itself?

I'm sure there is a magic identifier somewhere that lets a code get a
reference to its own module, but I haven't been able to find it. Can
someone share a clue?

Thanks,
- Joe

Nov 12 '08 #1
1 1667
Joe Strout <jo*@strout.netwrites:
I've been using docstring to exercise each of my modules, with code
like this:

def _test():
import doctest
doctest.testmod()

if __name__ == "__main__":
_test()
This works great when I execute each module by itself. However, if I
want to call mymodule._test() from somewhere else, it doesn't work,
because doctest.testmod() tests the __main__ module instead of
mymodule. And of course changing the call to this doesn't work
either:

doctest.testmod(mymodule)

This actually works fine if I'm importing the module (with the
standard name) somewhere else, but not when I'm executing it directly,
or (I would guess) if the module is imported under a different name.

What I want to express is "doctest THIS module, right here, the one
this code is in!" But I can't find a clean way to do that.

I noticed that a function object has a __module__ attribute, that is a
reference to the module the function is in. But that just pushes the
problem back a step: how can a function get a reference to itself?

I'm sure there is a magic identifier somewhere that lets a code get a
reference to its own module, but I haven't been able to find it. Can
someone share a clue?
There isn't. There was a proposal for one but it was rejected.
Nevertheless I think you can achieve this very easily.

In mymodule.py
--------------

def _test():
import doctest
import mymodule
doctest.testmod(mymodule)
That's it!

--
Arnaud
Nov 12 '08 #2

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

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
9
by: Sean | last post by:
Is there any way I could have the following work? First I would have a module define a function to do something like print some data. ----- module_name.py ----- def print_this(data):
6
by: Dennis | last post by:
In my program I have defined a global vector with //=======prototypes section ==================== float select(const int k, std::vector<float> &myArr, int iStart, int iEnd); ..... //====Global...
5
by: Bruce Lawrence | last post by:
I'm running Access 97 and my modules are looping if someone puts an invalid value in. The setup: 3 macros - get_clock_num, verify_clocknum, append_to_history 3 functions. each in their own...
5
by: Ian Bicking | last post by:
I got a puzzler for y'all. I want to allow the editing of functions in-place. I won't go into the reason (it's for HTConsole -- http://blog.ianbicking.org/introducing-htconsole.html), except that...
6
by: rh0dium | last post by:
Hi Experts!! I am trying to get the following little snippet to push my data to the function func(). What I would expect to happen is it to print out the contents of a and loglevel. But it's...
7
by: bambam | last post by:
import works in the main section of the module, but does not work as I hoped when run inside a function. That is, the modules import correctly, but are not visible to the enclosing (global)...
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
0
by: Chadrik | last post by:
i know how to programmatically create python functions: module = __import__( '__main__') def factory( val ): def f( arg=val ): print arg, val f.__name__ = 'function%s' % x return f for x...
6
by: Steven D'Aprano | last post by:
I have a function that needs a reference to the module object it is defined in. (For the reason why, if you care, see the thread "doctest not seeing any of my doc tests" from a week ago.) I know of...
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
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.