944,144 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 3303
  • Python RSS
Jun 1st, 2005
0

How to prevent statement execution when importing modules?

Expand Post »
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:

Python Syntax (Toggle Plain Text)
  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:

Python Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 41
Solved Threads: 31
Junior Poster
G-Do is offline Offline
146 posts
since Jun 2005
Jun 1st, 2005
0

Re: How to prevent statement execution when importing modules?

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.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jun 2nd, 2005
0

Re: How to prevent statement execution when importing modules?

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!
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jun 3rd, 2005
0

Re: How to prevent statement execution when importing modules?

Aha!!

Thank you very much, vegaseat. That:

Python Syntax (Toggle Plain Text)
  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!
Reputation Points: 41
Solved Threads: 31
Junior Poster
G-Do is offline Offline
146 posts
since Jun 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: cgi help !
Next Thread in Python Forum Timeline: Text Editors ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC