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

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

Some corrections, to highlight the depth of my confusion...

On Nov 11, 2008, at 9:10 PM, Joe Strout wrote:
doctest.testmod(mymodule)

This actually works fine if I'm importing the module (with the
standard name) somewhere else
Actually, it does not.
I noticed that a function object has a __module__ attribute, that is
a reference to the module the function is in.
And no, it isn't; it's the NAME of the module the function is in. I'm
not sure what good that does me. docstring.testmod does take an
optional "name" parameter, but the documentation (at least in 2.5.2)
does not explain what this parameter is used for. I tried using it
thusly:

doctest.testmod(name=_test.__module__)

but that didn't work; it appears to still be testing the __main__
module. (Perhaps the name parameter is only used to describe the
module in the output, in which case, all I've accomplished here is
getting doctest to lie.)
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?
This question remains open. :)

Thanks,
- Joe

Nov 12 '08 #1
2 1822
On Nov 12, 11:17*am, Joe Strout <j...@strout.netwrote:
Some corrections, to highlight the depth of my confusion...

On Nov 11, 2008, at 9:10 PM, Joe Strout wrote:
* *doctest.testmod(mymodule)
This actually works fine if I'm importing the module (with the *
standard name) somewhere else

Actually, it does not.
I noticed that a function object has a __module__ attribute, that is *
a reference to the module the function is in.

And no, it isn't; it's the NAME of the module the function is in. *I'm *
not sure what good that does me. *docstring.testmod does take an *
optional "name" parameter, but the documentation (at least in 2.5.2) *
does not explain what this parameter is used for. *I tried using it *
thusly:

* * * * doctest.testmod(name=_test.__module__)

but that didn't work; it appears to still be testing the __main__ *
module. *(Perhaps the name parameter is only used to describe the *
module in the output, in which case, all I've accomplished here is *
getting doctest to lie.)
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?

This question remains open. *:)

Thanks,
- Joe
import sys
this_module = sys.modules[__name__]

sys.modules is a dictionary of all modules which have been imported
during the current session. Since the module had to be imported to
access it, it will be in there. '__name__' is available inside
functions because it is in the module scope.

Classes are a little more tricky because doing something like:
this_module = sys.modules[self.__class__.__module__]
will return a different module if the class is inherited in a
different module (since the base class is __class__ now). However,
putting a function at the module level (in the super-class module)
should anchor the results (untested though).

I'm not sure if this is the answer you need with regards to doctest,
but it I think it answers the question in the subject of this thread.

- Rafe
Nov 12 '08 #2
On Nov 11, 2008, at 9:49 PM, Rafe wrote:
>>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.

import sys
this_module = sys.modules[__name__]
Beautiful! Thanks very much. For the archives, here is my standard
module-testing idiom now:

def _test():
import doctest, sys
doctest.testmod(sys.modules[__name__])

if __name__ == "__main__":
_test()

Now, when I execute the module directly, it will test itself; and if I
need to test it from the outside (for example, under pdb) I can import
the module and then run themodule._test().

To whom should I make the suggestion that this go into doctest docs?

Cheers,
- Joe

Nov 12 '08 #3

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...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.