How to prevent statement execution when importing modules?

Thread Solved

Join Date: Jun 2005
Posts: 146
Reputation: G-Do is an unknown quantity at this point 
Solved Threads: 28
G-Do's Avatar
G-Do G-Do is offline Offline
Junior Poster

How to prevent statement execution when importing modules?

 
0
  #1
Jun 1st, 2005
Hi,
I am a Java programmer dabbling in Python for the first time. Mostly, the transition has been pretty smooth, but this week something stumped me. Please forgive me if the answer to this is completely obvious - I've read van Rossum's tutorial and can't figure it out.
I am working on a Python project composed of one main module and several smaller helper modules. Each module is currently in its own file, which (I believe) is the convention Python uses. The main module is called MainProgram.py and one of the helper modules is called Helper.py. MainProgram.py has to import a method called somefunc() from Helper.py.
Now, one of the conventions I use when designing OO systems in Java is this: all of my helper classes have diagnostic code in their main methods, so that when you invoke the class's main from the command line, you get to see whether the methods of that class work the way they are supposed to. Unfortunately, Python modules have no main method to speak of, so I did what I thought was the next best thing. In my Helper module:

  1. #!/usr/bin/python2.3
  2. # Helper.py
  3.  
  4. def somefunc(a, b, c):
  5. # code for this function
  6.  
  7. # Code that tests somefunc
  8. somefunc('A', 'B', 'C')
The idea is that somefunc() executes with arbitrary arguments of 'A', 'B', and 'C' when Helper.py is invoked on its own. Then, in the MainProgram module:

  1. #!/usr/bin/python2.3
  2. # MainProgram.py
  3.  
  4. from Helper import somefunc
  5. somefunc('Actual', 'data', 'here')
  6. # Other code which does other stuff
Right? Unfortunately, the import statement also triggers that diagnostic invocation of somefunc() at the bottom of Helper.py - this is neither trivial nor good in the context of my actual project. It's also very confusing, since you'd think that importing a method from a module wouldn't call for any execution at all.

Can anyone tell me if there's a way to do what I'm describing in Python? Again, I'm sorry if this is a total n00b question - that's what I am at the moment.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,971
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 920
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: How to prevent statement execution when importing modules?

 
0
  #2
Jun 1st, 2005
In your Python folder there is a folder \lib\test\ that should give you an idea how the Pythonians do their testing of modules. It looks like you might need to write a simple test program. Also IDEs like IDLE have a check module option.

Hope someone else knows more about that subject. I started using Python more seriously about 5 months ago and have great fun with it, but have not touched every corner of its many capabilities.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,971
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 920
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: How to prevent statement execution when importing modules?

 
0
  #3
Jun 2nd, 2005
I knew I stumbled upon this subject before, there is a nice article on Test-Driven Development in Python on:
http://www.onlamp.com/pub/a/python/2...dd_pyunit.html

Hope that helps!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 146
Reputation: G-Do is an unknown quantity at this point 
Solved Threads: 28
G-Do's Avatar
G-Do G-Do is offline Offline
Junior Poster

Re: How to prevent statement execution when importing modules?

 
0
  #4
Jun 3rd, 2005
Aha!!

Thank you very much, vegaseat. That:

  1. if __name__ == '__main__':
  2. # Test code here
...was just what I needed. And the article was very informative - I knew about doctest and unittest, but not enough. Thanks!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC