473,780 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.testm od 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.__m odule__)

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 1836
On Nov 12, 11:17*am, Joe Strout <j...@strout.ne twrote:
Some corrections, to highlight the depth of my confusion...

On Nov 11, 2008, at 9:10 PM, Joe Strout wrote:
* *doctest.testmo d(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.test mod 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.__m odule__)

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
4964
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 webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
9
1634
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
2738
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 Vectors ================================== std::vector<float> gTmpArr(8000,0); In Main, I would like to pass this vector to a function such as:
5
1803
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 module - get_clocks(), verify_clocks(), append() Each macro runs a function from in its corresponding module. Here is my code.
5
1981
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 I really want to edit it all in-process and in-memory. So I want the identity of the function to remain the same, even as I edit the body and hopefully the signature too. Well, the reason is that I want to edit any function object, without...
6
1352
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 not working. Can someone please help me out. ---------<snip>-------------- #!/usr/bin/env python
7
2155
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) scope. Questions: (1) Where can I read an explanation of this? (2) Is there a work around?
6
5931
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 or without parms. I know that any function that is passed to Eval() must be declared Public. It can be a Sub or Function, as long as it's Public. I even have it where the "function" evaluated by Eval can be in a form (class) module or in a standard...
0
789
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 in :
6
1116
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 two ways to deal with this problem, both of which feel unsatisfactory to me. Assume the name of the module is "Mod", then I can do either of these: def foo(): import Mod process(Mod)
0
9636
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
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
10139
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
8961
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7485
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
5373
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
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2869
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.